Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Installing and Testing Your Connector

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:

  1. Set required @Connector annotations.

  2. Add a "put in barn" operation to connector.

  3. Document the "put in barn" operation.

  4. Build the connector project and Install it using the Anypoint DevKit plugin.

  5. 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. 


         
      
1
@Connector(name = "barn", schemaVersion = "1.0", friendlyName = "Barn", minMuleVersion = "3.5")

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.

  1. In Anypoint Studio, open the main Java file, src/main/java/org.mule.modules.barn/BarnConnector.java

  2. Paste the following code at the very end of the connector class, before the final closing curly brackets.

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    /**  
     * 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.   
  3. Confirm that the complete class appears as per the following.

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    
    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. 

  1. In your Anypoint Studio, open the file  doc/barn-connector.xml.sample then paste the following snippet at the end.

    
                 
              
    1
    2
    3
    
    <!-- BEGIN_INCLUDE(barn:putInBarn) -->
        <barn:put-in-barn animal="#[map-payload:animal]" />
    <!-- END_INCLUDE(barn:putInBarn) -->
  2. Confirm that the complete contents of the file appear as per the following.

    
                 
              
    1
    2
    3
    4
    5
    6
    7
    
    <!-- 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:


         
      
1
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.

barn-studio-icon

Testing the Connector

To see your new connector function, create, then run a Mule application which uses the Barn Connector.

  1. 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).

    flow-1

  2. 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.

    config1

    Parameter Value

    Display Name

    Barn

    Config Reference

    see next step

    Operation

    Put In Barn

    Animal

    #[message.inboundProperties['animal']] 

  3. Click the plus sign next to the Config Reference field to create a new global element for your connector to use.

    config

  4. In the My Property, Username and Password fields, enter any string as the value, then click OK.

    global-elements

  5. Save, then run the project as a Mule Application (right-click project name, then select Run As > Mule Application).

  6. From a browser, navigate to http://localhost:8081/?animal=Mule

  7. The application returns the response in your browser (see below).
    browser

  1. Create a simple flow using an http:inbound-endpoint and your new barn:config. Configure the parameters of each element according to the code sample below.

    
           
                   
                
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;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"&gt;
         
        &lt;barn:config name="Barn" username="user" password="pass"
            myProperty="whatever you want" doc:name="Barn"&gt;
            &lt;barn:connection-pooling-profile
                initialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW" /&gt;
        &lt;/barn:config&gt;
     
        &lt;flow name="barntestFlow1" doc:name="barntestFlow1"&gt;
            &lt;http:inbound-endpoint exchange-pattern="request-response"
                host="localhost" port="8081" doc:name="HTTP" /&gt;
             
            &lt;barn:put-in-barn config-ref="Barn"
                animal="#[message.inboundProperties['animal']]" doc:name="Barn" /&gt;
     
        &lt;/flow&gt;
    &lt;/mule&gt;
  2. Save, then run the project.

  3. From a browser, navigate to http://localhost:8081/?animal=Mule

  4. The application returns the response in your browser (see below).
    browser

See Also