@Connector(name = "barn", schemaVersion = "1.0", friendlyName = "Barn", minMuleVersion = "3.5")
Installing and Testing Your Connector
DevKit is compatible only with Studio 6 and Mule 3. To build Mule 4 connectors, see the Mule SDK documentation. |
Having built a project with the Anypoint DevKit plugin, you can install the skeletal connector in Anypoint 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 Anypoint Studio. The macro steps of this procedure are as follows:
-
Set required @Connector annotations.
-
Add a "put in barn" operation to connector.
-
Document the "put in barn" operation.
-
Build the connector project and Install it using the Anypoint DevKit plugin.
-
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 Anypoint Studio. However, to make testing more interesting, you can add an operation to the connector that receives an animal as a string and returns the same animal name followed by "has been placed in the barn". Follow the steps below to add this operation.
-
In Anypoint Studio, open the main Java file,
src/main/java/org.mule.modules.barn/BarnConnector.java
-
Paste the following code at the very end of the connector class, before the final closing curly brackets.
/** * Custom processor that places an animal in the barn. * * {@sample.xml ../../../doc/barn-connector.xml.sample barn:putInBarn} * * @param animal Name of the animal to be place in the barn * @return returns processed message */ @Processor public String putInBarn(String animal) { return animal + " has been placed in the barn"; }
The identifier name
is reserved and cannot be used as a parameter. -
Confirm that the complete class appears as per the following.
package org.mule.modules.barn; 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; import org.mule.api.annotations.display.Password; /** * Anypoint Connector * * @author MuleSoft, Inc. */ @Connector(name="barn", schemaVersion="1.0", friendlyName="Barn", minMuleVersion="3.5") public class BarnConnector { /** * 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, @Password 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; } /** * Are we connected */ @ConnectionIdentifier public String connectionId() { return "001"; } /** * Custom processor * * {@sample.xml ../../../doc/barn-connector.xml.sample barn:myProcessor} * * @param content Content to be processed * @return Some string */ @Processor public String myProcessor(String content) { /* * MESSAGE PROCESSOR CODE GOES HERE */ return content; } /** * Custom processor that places an animal in the barn. * * {@sample.xml ../../../doc/barn-connector.xml.sample barn:putInBarn} * * @param animal Name of the animal to be place in the barn * @return returns processed message */ @Processor public String putInBarn(String animal) { return animal + " has been placed in the barn"; } }
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.
-
In your Anypoint Studio, open the file
doc/barn-connector.xml.sample
then paste the following snippet at the end.<!-- BEGIN_INCLUDE(barn:putInBarn) --> <barn:put-in-barn animal="#[map-payload:animal]" /> <!-- END_INCLUDE(barn:putInBarn) -->
-
Confirm that the complete contents of the file appear as per the following.
<!-- BEGIN_INCLUDE(barn:myProcessor) --> <barn:my-processor content="#[map-payload:content]" /> <!-- END_INCLUDE(barn:myProcessor) --> <!-- BEGIN_INCLUDE(barn:putInBarn) --> <barn:put-in-barn animal="#[map-payload:animal]" /> <!-- END_INCLUDE(barn:putInBarn) -->
Building and Installing the Connector
At this point the connector is ready to be built and used for the first time. The DevKit plugin for Anypoint Studio automates the process of building the connector, running tests, and packaging it to be used in Anypoint Studio.
To compile and install your connector, right click your project and select Anypoint Connector > Install or Update. This is equivalent to running the command below from the command console from within the project folder:
mvn clean package
This command will create an update site that you can use to install the connector in Anypoint Studio. For more information about how to package your connector, check Packaging Your Connector for Release.
The Install or Update feature will also automatically install your connector in Anypoint Studio.
Testing the Connector
To see your new connector function, create, then run a Mule application which uses the Barn Connector.
STUDIO Visual Editor
-
Create a simple flow using an HTTP endpoint and your new Barn 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
Barn
Config Reference
see next step
Operation
Put In Barn
Animal
#[message.inboundProperties['animal']]
-
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, Username and Password fields, 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/?animal=Mule
-
The application returns the response in your browser (see below).
Studio XML Editor
-
Create a simple flow using an
http:inbound-endpoint
and your newbarn:config
. Configure the parameters of each element according to the code sample below.<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:barn="http://www.mulesoft.org/schema/mule/barn" 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/barn http://www.mulesoft.org/schema/mule/barn/current/mule-barn.xsd"> <barn:config name="Barn" username="user" password="pass" myProperty="whatever you want" doc:name="Barn"> <barn:connection-pooling-profile initialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW" /> </barn:config> <flow name="barntestFlow1" doc:name="barntestFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP" /> <barn:put-in-barn config-ref="Barn" animal="#[message.inboundProperties['animal']]" doc:name="Barn" /> </flow> </mule>
-
Save, then run the project.
-
From a browser, navigate to
http://localhost:8081/?animal=Mule
-
The application returns the response in your browser (see below).
See Also
-
NEXT STEP: implement authentication on your connector.