Nav

Tutorial - Barn Connector

This document describes the steps needed to build, install, and test a connector in Anypoint Studio. 

The following tutorial describes how to create a simple connector that displays "Mule has been placed in the barn" in a browser. You can use the user interface for the connector in Anypoint Studio to change which animal is put in the barn.

Before Creating a Connector

  1. Install:

  2. Start Anypoint Studio.

  3. Configure Maven:

    1. Click Anypoint Studio > Preferences > Anypoint Studio > Maven Settings. In DevKit 3.7, when you first create a connector, if Maven is not configured, you are prompted to install it.

  4. Install the DevKit Plugin: 

    1. Click Install New Software.

    2. Click Work with > Anypoint Addons Update Site > Anypoint Devkit Plugin.

    3. Click Next, Finish, and restart Studio.

Creating a Connector Project

  1. Create your initial Barn connector. Use File > New > Project > Anypoint Connector Project. Name the connector Barn. You can leave all other fields the same. Click Finish. You can ignore any warning messages in the Studio Console.

  2. Review the scaffolded connector: 

    
                
             
    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
    
    /**
     * (c) 2003-2015 MuleSoft, Inc. The software in this package is published under the terms of the CPAL v1.0 license,
     * a copy of which has been included with this distribution in the LICENSE.md file.
     */
    package org.mule.modules.barn;
    import org.mule.api.annotations.ConnectionStrategy;
    import org.mule.api.annotations.Connector;
    import org.mule.api.annotations.Configurable;
    import org.mule.api.annotations.Processor;
    import org.mule.api.annotations.param.Default;
    import org.mule.modules.barn.strategy.ConnectorConnectionStrategy;
    /**
     * Anypoint Connector
     *
     * @author MuleSoft, Inc.
     */
    @Connector(name="barn", schemaVersion="1.0", friendlyName="Barn")
    public class BarnConnector
    {
        /**
         * Configurable
         */
        @Configurable
        @Default("value")
        private String myProperty;
        @ConnectionStrategy
        ConnectorConnectionStrategy connectionStrategy;
        /**
         * Custom processor
         *
         * {@sample.xml ../../../doc/barn-connector.xml.sample barn:my-processor}
         *
         * @param content Content to be processed
         * @return Some string
         */
        @Processor
        public String myProcessor(String content) {
            /*
             * MESSAGE PROCESSOR CODE GOES HERE
             */
            return content;
        }
        /**
         * Set property
         *
         * @param myProperty My property
         */
        public void setMyProperty(String myProperty) {
            this.myProperty = myProperty;
        }
        /**
         * Get property
         */
        public String getMyProperty() {
            return this.myProperty;
        }
        public ConnectorConnectionStrategy getConnectionStrategy() {
            return connectionStrategy;
        }
        public void setConnectionStrategy(ConnectorConnectionStrategy connectionStrategy) {
            this.connectionStrategy = connectionStrategy;
        }
    }

Coding a Connector

To code the connector:

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

    BarnJava

  2. Paste the following code in the connector class, before the final closing curly brackets, and after the existing @Processor block:

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    /**
     * 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(friendlyName="Put an animal in the barn")
    public String putInBarn(@Placement(group="Parameters")
      @FriendlyName("The animal's name") String animal) {
            return animal + " has been placed in the barn";
        }

    The path name in the @Sample statement indicates the path to the Javadoc *.xml.sample file in Studio–it’s not a filesystem path. For each Javadoc sample file in your connector, specify corresponding statements in the xml.sample file.

    The line starting with @Processor identifies the processor function, which specifies the user interface for the connector.
    The line starting with @FriendlyName indicates what the UI elements that display in the browser when the connector runs in Anypoint Studio. 

    This code changes the operation name to "Put an animal in the barn", and changes the Animal prompt to "The animal’s name". After inserting, @Placement and @FriendlyName are underscored as an error condition. Mouse over each annotation and select the import command from the popup menu to add import commands to your connector:

    BarnImportAnno2

    BarnImportAnnotation

  3. Add loading statements for future Javadoc documentation in your connector by providing map-payload statements:

    1. Open doc/barn-connector.xml.sample:

      
                     
                  
      1
      2
      3
      
      <!-- BEGIN_INCLUDE(barn:my-processor) -->
        <barn:my-processor config-ref="" content="#[map-payload:content]" />
      <!-- END_INCLUDE(barn:my-processor) -->
    2. Include one BEGIN/END block for each @Processor statement block in your connector. For the second @Processor statement block, add these lines:

      
                     
                  
      1
      2
      3
      
      <!-- BEGIN_INCLUDE(barn:my-processor) -->
        <barn:put-in-barn config-ref="" animal="#[map-payload:content]" />
      <!-- END_INCLUDE(barn:my-processor) -->
    3. Save this file. 

  4. Disable spell checking by mousing over text with a red underline and clicking Disable spell checking:

    BarnDisableSpellChecking

  5. The completed connector code appears as follows:

    View the Completed Connector’s Code:

    
                
             
    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
    
    /**
     * (c) 2003-2014 MuleSoft, Inc. The software in this package is published under the terms of the CPAL v1.0 license,
     * a copy of which has been included with this distribution in the LICENSE.md file.
     */
    package org.mule.modules.barn;
    import org.mule.api.annotations.ConnectionStrategy;
    import org.mule.api.annotations.Connector;
    import org.mule.api.annotations.Configurable;
    import org.mule.api.annotations.Processor;
    import org.mule.api.annotations.display.FriendlyName;
    import org.mule.api.annotations.display.Placement;
    import org.mule.api.annotations.param.Default;
    import org.mule.modules.barn.strategy.ConnectorConnectionStrategy;
    /**
     * Anypoint Connector
     *
     * @author MuleSoft, Inc.
     */
    @Connector(name="barn", schemaVersion="1.0", friendlyName="Barn")
    public class BarnConnector
    {
        /**
         * Configurable
         */
        @Configurable
        @Default("value")
        private String myProperty;
        @ConnectionStrategy
        ConnectorConnectionStrategy connectionStrategy;
        /**
         * Custom processor
         *
         * {@sample.xml ../../../doc/barn-connector.xml.sample barn: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 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(friendlyName="Put an animal in the barn")
        public String putInBarn(@Placement(group="Parameters")
          @FriendlyName("The animal's name") String animal) {
                return animal + " has been placed in the barn";
        }
        /**
         * Set property
         *
         * @param myProperty My property
         */
        public void setMyProperty(String myProperty) {
            this.myProperty = myProperty;
        }
        /**
         * Get property
         */
        public String getMyProperty() {
            return this.myProperty;
        }
        public ConnectorConnectionStrategy getConnectionStrategy() {
            return connectionStrategy;
        }
        public void setConnectionStrategy(ConnectorConnectionStrategy connectionStrategy) {
            this.connectionStrategy = connectionStrategy;
        }
    }

Identifying Your Connector in Studio

When you create an Anypoint Connector Project, Studio creates the @Connector annotation containing a name, schema version, and friendly name. 

Connectors use the @Connector annotation to describe the contents of a connector. The friendlyName parameter defines the connector’s label in the Studio palette.  


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

After you install your connector in Studio, when users create a Mule Project, the friendlyName identifies your connector:

connector-display

Installing Your Connector

  1. Install your project: Right-click the connector project’s name in Package Explorer, and click Anypoint Connector > Install or Update

  2. Follow the prompts to accept the details, the terms of the license agreement, and installing software with unsigned content. Restart Studio for the changes to take effect. The connector then becomes available to include in a Mule Project.

Creating a Mule Project With Your Connector

  1. After Studio restarts, create a Mule project using File > New > Mule Project.

  2. Search for "http" and drag the HTTP connector to the canvas. The default settings are correct.

  3. Search for "barn" and drag the Barn connector to the canvas. 
    The canvas appears as:

    BarnFlow

    To configure the Barn connector:

    1. Click the green plus:

      BarnPanel1

    2. Ensure that the screen appears as follows and click OK:

      BarnPanel2

      The two values for My Property and My Strategy Property are supplied by the default connector. You can ignore these.

    3. In the first screen, click Operation and click Put in barn. In the Animal field specify a name such as the Mule animal:

      BarnPanel3

  4. Deploy your Mule project by clicking Run > Run As > Mule Application.

  5. Open a browser and set the address to http://0.0.0.0:8081 - The browser displays:

    BarnBrowser

  6. You can change the Animal value, save your project, and refresh your browser to put other animals in the barn. 

See Also