@Connector(name = "helloConnector", schemaVersion = "1.0-SNAPSHOT", friendlyName = "Hello")
Installing and Testing Your Connector
Having built a project with Maven and your IDE, you can install the skeletal connector in Studio to confirm its basic functionality before adding more operations to it. This document describes the steps to take to build, then install and test your connector in Mule Studio. The macro steps of this procedure are as follows:
-
Set required @Connector annotations.
-
Add a "say hello" operation to connector.
-
Document the "say hello" operation.
-
Build the connector project with Maven.
-
Install the connector in Studio.
-
Test the connector’s basic functionality.
Assumptions
This document assumes you have created a connector project.
Setting @Connector Annotation Parameters
The @Connector annotation on the Maven-generated class has a required parameter: friendlyName
. (See example below.) This parameter defines a human-readable name for the connector, which appears as the connector’s label in Studio’s palette. Without this parameter, the connector does not appear in the palette in Mule Studio releases after 3.4.0.
Adding an Operation
The connector project you created is immediately ready to build, then install and test in Mule Studio. However, to make testing more interesting, you can add an operation to the connector that receives a name as a string and returns the same name preceded by "Hello". Follow the steps below to add this operation.
-
In your IDE, open the main Java file,
src/main/java/org.mulesoft.hello/HelloConnector.java
-
Paste the following code at the very end of the connector class, before the final closing curly brackets. Use the identifier
uname
because the identifiername
is reserved; you cannot use it for the parameter./** * Custom processor * * {@sample.xml ../../../doc/hello-connector.xml.sample hello:say-hello} * * @param uname Name to be processed * @return Some string */ @Processor public String sayHello(String uname) { return "Hello " + uname; }
-
Confirm that the complete class appears as per the following.
/** * This file was automatically generated by the Mule Development Kit */ package TestConnector; import org.mule.api.annotations.Connector; import org.mule.api.annotations.Connect; import org.mule.api.annotations.ValidateConnection; import org.mule.api.annotations.ConnectionIdentifier; import org.mule.api.annotations.Disconnect; import org.mule.api.annotations.param.ConnectionKey; import org.mule.api.ConnectionException; import org.mule.api.annotations.Configurable; import org.mule.api.annotations.Processor; /** * Cloud Connector * * @author MuleSoft, Inc. */ @Connector(name="helloConnector", schemaVersion="1.0-SNAPSHOT", friendlyName="Hello") public class HelloConnector { /** * Configurable */ @Configurable private String myProperty; /** * Set property * * @param myProperty My property */ public void setMyProperty(String myProperty) { this.myProperty = myProperty; } /** * Get property */ public String getMyProperty() { return this.myProperty; } /** * Connect * * @param username A username * @param password A password * @throws ConnectionException */ @Connect public void connect(@ConnectionKey String username, String password) throws ConnectionException { /** * CODE FOR ESTABLISHING A CONNECTION GOES IN HERE */ } /** * Disconnect */ @Disconnect public void disconnect() { /** * CODE FOR CLOSING A CONNECTION GOES IN HERE */ } /** * Are we connected */ @ValidateConnection public boolean isConnected() { return true; } /** * Connection Identifier */ @ConnectionIdentifier public String connectionId() { return "001"; } /** * Custom processor * * {@sample.xml ../../../doc/MyTest-connector.xml.sample mytest:my-processor} * * @param content Content to be processed * @return Some string */ @Processor public String myProcessor(String content) { /** * MESSAGE PROCESSOR CODE GOES HERE */ return content; } /** * Custom processor * * {@sample.xml ../../../doc/hello-connector.xml.sample hello:say-hello} * * @param uname Name to be processed * @return Some string */ @Processor public String sayHello(String uname) { return "Hello " + uname; } }
Documenting the Operation
Because DevKit enforces proper documentation of all projects, you must provide at least one example of how to use each operation you add to a connector. If you attempt to build the project without documentation, the build fails.
-
In your IDE, open the file
doc/Hello-connector.xml.sample
then paste the following snippet at the end.<!-- BEGIN_INCLUDE(hello:say-hello) --> <hello:say-hello uname="#[payload.uname]" /> <!-- END_INCLUDE(hello:say-hello) -->
-
Confirm that the complete contents of the file appear as per the following.
<!-- BEGIN_INCLUDE(hello:my-processor) --> <hello:my-processor content="#[map-payload:content]" /> <!-- END_INCLUDE(hello:my-processor) --> <!-- BEGIN_INCLUDE(hello:say-hello) --> <hello:say-hello uname="#[payload.uname]" /> <!-- END_INCLUDE(hello:say-hello) -->
Note that the name of the processor in our Java file is DevKit automatically converts the CamelCase name for the processor to a hyphenated name, thus, it does not recognize a processor labeled |
Building the Project with Maven
At this point the connector is ready to be built and used for the first time. Maven automates the process of building the connector, running tests, and packaging it to be used in Mule Studio.
If using Studio as your IDE, note that the Maven support for developing Mule projects does not apply to building DevKit projects. You must, therefore, build connectors using Maven and the command line, or using m2e
menu commands.
-
To compile the project, access the command console, then run the following command in the project directory.
mvn clean package -Ddevkit.studio.package.skip=false
-
The target folder in your IDE now contains a fully functional – albeit, very basic – connector that is ready to be installed in Mule Studio.
Installing the Connector in Mule Studio
-
Under the
Help
menu in Mule Studio, selectInstall New Software
. -
Click Add… next to the Work with field, then enter the following values:
-
Name:
Hello Connector
-
Location: the filepath of your connector’s update-site.zip file (inside the
target
folder) prepended withfile:/
-
-
In the table below the filter field (see image below), use the checkboxes to select your Mule extension (i.e. connector (click to expand the folders to select individual items), then click Next.
-
Review the details of the item you selected, then click Next.
-
Click to accept terms and conditions of the product, then click Finish.
-
Click Restart Now to complete the installation.
-
After Studio restarts, expand the Cloud Connectors palette group to see your new Hello Connector.
Testing the Connector
To see your new connector function, create, then run a Mule application which uses the Hello Connector.
STUDIO Visual Editor
-
Create a simple flow using an HTTP endpoint and your new Hello connector. Use the default values configured in the HTTP endpoint (port:`8081`, host:
localhost
). -
Select the connector to open the properties editor. Notice the parameters Studio makes available to the end user to configure. Configure the parameters according to the table below.
Parameter Value Display Name
Hello
Config Reference
see next step
Operation
Say hello
Uname
#[message.inboundProperties['uname']]
-
Click the plus sign next to the Config Reference field to create a new global element for your connector to use.
-
In the My Property field, enter any string as the value, then click OK.
-
Save, then run the project as a Mule Application (right-click project name, then select Run As > Mule Application).
-
From a browser, navigate to
http://localhost:8081/?uname=Dave
-
The application returns the response in your browser (see below).
Studio XML Editor
-
Create a simple flow using an
http:inbound-endpoint
and your newhello:config
. Configure the parameters of each element according to the code sample below.<mule xmlns:hello="http://www.mulesoft.org/schema/mule/hello" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/hello http://www.mulesoft.org/schema/mule/hello/1.0-SNAPSHOT/mule-hello.xsd"> <hello:config name="Hello" myProperty="asdf" doc:name="Hello"> <hello:connection-pooling-profile initialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW"/> </hello:config> <flow name="Hello-connector-testFlow1" doc:name="Hello-connector-testFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/> <hello:say-hello config-ref="Hello" uname="#[message.inboundProperties['uname']]" doc:name="Hello"/> </flow> </mule>
-
Save, then run the project.
-
From a browser, navigate to
http://localhost:8081/?uname=Dave
-
The application returns the response in your browser.
See Also
-
NEXT STEP: implement authentication on your connector.