Author: Putul Manda

The XML SDK is an alternative to the more advanced Java-based Mule SDK. XML SDK is for creating custom modules, similar to the way you create a Mule app.This approach is comparatively easier to build compared to the already existing JAVA Mule SDK. 

Here, we’ve developed a basic and simple mule 4 connector which performs basic operations like multiplication and addition.

  • What are the prerequisites and how to set-up the system?
  • How to create an SDK for Connector development in mule 4.
  • How to set-up the basic configurations
  • How to run the maven command in cmd to create the basic architecture for the connector.
  • How to add the dependency to the pom file.

Where to find the connector and how to use it.

  • Prerequisites:
  1. Java Development Kit 8 (JDK 8)
  2. Apache Maven, version 3.3.9 or higher 
  • Set the system variables:
  1. Path 
  2. JAVA_HOME
  3. MAVEN_HOME

STEPS:

  • COMMAND PROMPT:
  1. Create a folder and open command prompt from there as shown below:

  1. Open the cmd and run a maven command as shown below:–

 mvn archetype:generate -DarchetypeGroupId=org.mule.extensions -DarchetypeArtifactId=mule-extensions-xml-archetype -DarchetypeVersion=1.2.0 -DgroupId=org.mule.extension -DartifactId=calculator-connector-extension -Dpackage=mule-module -Dversion=1.0.0-SNAPSHOT -DextensionName=”Calculator   Connector”

  • archetypeGroupId -> the groupId in the Mule repository where the skeleton for the XML SDK is defined
  • archetypeArtifactId -> the artifact id of the archetype under this repository
  • archetypeVersion -> the version of the mule-extensions-xml-archetype
  • groupId -> the group ID for the module that you are creating
  • artifactId -> the artifact ID for your custom module
  • Package -> the package under which you want to build your application
  • Version -> the version of your custom module
  • extensionName -> the name of your custom module

Here, the values for the groupId, artifactId, package, version, and extensonName are customizable.

3. It will start to build.

4. Press Enter.

 5. Running this command provides the basic structure to develop the connector.

  • ANYPOINT STUDIO:
  1. Now open your Anypoint Studio and import the project from the directory as it is shown below:

After importing the project we will get the project structure like this as shown below:

  1. After importing the project add the pom file given blow by replacing the old pom file in the project:

<?xml version=”1.0″ encoding=”UTF-8″?>

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.mule.extension</groupId>

    <artifactId>calculator-connector-extension</artifactId>

    <packaging>mule-extension</packaging>

    <version>1.1.4-SNAPSHOT</version>

    <name>Smart Connector: using mule components</name>

    <description>A Mule extension done with pure Mule DSL that depends in the runtime operations (set-payload, set-variable, etc.)</description>

    <properties>

        <mule.version>4.1.1</mule.version>

        <mule.extensions.maven.plugin.version>1.1.6</mule.extensions.maven.plugin.version>

        <munit.version>2.1.3</munit.version>

        <munit.extensions.maven.plugin.version>1.0.0</munit.extensions.maven.plugin.version>

        <munit.input.directory>src/test/munit</munit.input.directory>

        <munit.output.directory>${basedir}/target/test-mule/munit</munit.output.directory>

        <maven.source.plugin.version>3.0.1</maven.source.plugin.version>

        <maven.resources.plugin.version>3.1.0</maven.resources.plugin.version>

        <maven.clean.plugin.version>3.1.0</maven.clean.plugin.version>

        <maven.compiler.plugin.version>3.8.0</maven.compiler.plugin.version>

        <maven.install.plugin.version>2.5.2</maven.install.plugin.version>

        <maven.deploy.plugin.version>2.8.2</maven.deploy.plugin.version>

        <maven.site.plugin.version>3.8.2</maven.site.plugin.version>

        <maven.javadoc.plugin.version>3.1.1</maven.javadoc.plugin.version>

        <maven.surefire.plugin.version>2.22.2</maven.surefire.plugin.version>

    </properties>

    <dependencies>

        <!–Needed to discover the ‘xml-based’ XmlExtensionLoader for smart connectors–>

        <dependency>

            <groupId>org.mule.runtime</groupId>

            <artifactId>mule-module-extensions-xml-support</artifactId>

            <version>${mule.version}</version>

            <scope>provided</scope>

        </dependency>

         <dependency>

            <groupId>org.mule.connectors</groupId>

            <artifactId>mule-http-connector</artifactId>

            <version>1.5.16</version>

            <classifier>mule-plugin</classifier>

        </dependency>

        <dependency>

            <groupId>org.mule.connectors</groupId>

            <artifactId>mule-file-connector</artifactId>

            <version>1.3.2</version>

            <classifier>mule-plugin</classifier>

        </dependency>

        <!–MUnit Dependencies–>

        <dependency>

            <groupId>com.mulesoft.munit</groupId>

            <artifactId>munit-runner</artifactId>

            <version>${munit.version}</version>

            <classifier>mule-plugin</classifier>

            <scope>test</scope>

        </dependency>

    </dependencies>

    <build>

        <pluginManagement>

            <plugins>

                <plugin>

                    <groupId>com.mulesoft.munit</groupId>

                    <artifactId>munit-extensions-maven-plugin</artifactId>

                    <version>${munit.extensions.maven.plugin.version}</version>

                    <executions>

                        <execution>

                            <id>munit-extension-test</id>

                            <phase>integration-test</phase>

                            <goals>

                                <goal>test</goal>

                            </goals>

                        </execution>

                    </executions>

                    <configuration>

                        <dynamicPorts>

                            <dynamicPort>a.dynamic.port</dynamicPort>

                        </dynamicPorts>

                        <environmentVariables>

                            <MY_ENV>envVar</MY_ENV>

                        </environmentVariables>

                    </configuration>

                </plugin>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-resources-plugin</artifactId>

                    <version>${maven.resources.plugin.version}</version>

                    <executions>

                        <execution>

                            <id>copy-munit-resources</id>

                            <phase>process-test-resources</phase>

                            <goals>

                                <goal>copy-resources</goal>

                            </goals>

                            <configuration>

                                <outputDirectory>${munit.output.directory}</outputDirectory>

                                <resources>

                                    <resource>

                                        <directory>${munit.input.directory}</directory>

                                        <filtering>true</filtering>

                                    </resource>

                                </resources>

                            </configuration>

                        </execution>

                    </executions>

                </plugin>

                <!– Maven Plugins –>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-clean-plugin</artifactId>

                    <version>${maven.clean.plugin.version}</version>

                </plugin>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-source-plugin</artifactId>

                    <version>${maven.source.plugin.version}</version>

                </plugin>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-compiler-plugin</artifactId>

                    <version>${maven.compiler.plugin.version}</version>

                </plugin>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-install-plugin</artifactId>

                    <version>${maven.install.plugin.version}</version>

                </plugin>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-deploy-plugin</artifactId>

                    <version>${maven.deploy.plugin.version}</version>

                </plugin>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-site-plugin</artifactId>

                    <version>${maven.site.plugin.version}</version>

                </plugin>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-javadoc-plugin</artifactId>

                    <version>${maven.javadoc.plugin.version}</version>

                    <executions>

                        <execution>

                            <id>attach-javadocs</id>

                            <goals>

                                <goal>jar</goal>

                            </goals>

                        </execution>

                    </executions>

                    <configuration>

                        <doclint>none</doclint>

                    </configuration>

                </plugin>

            </plugins>

        </pluginManagement>

        <plugins>

            <plugin>

                <groupId>org.mule.runtime.plugins</groupId>

                <artifactId>mule-extensions-maven-plugin</artifactId>

                <version>${mule.extensions.maven.plugin.version}</version>

                <extensions>true</extensions>

            </plugin>

            <plugin>

                <groupId>com.mulesoft.munit</groupId>

                <artifactId>munit-extensions-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

    <repositories>

        <repository>

            <id>mulesoft-releases</id>

            <name>MuleSoft Releases Repository</name>

            <url>http://repository.mulesoft.org/releases/</url>

            <layout>default</layout>

        </repository>

    </repositories>

    <pluginRepositories>

        <pluginRepository>

            <id>mulesoft-releases</id>

            <name>mulesoft release repository</name>

            <layout>default</layout>

            <url>https://repository.mulesoft.org/releases/</url>

            <snapshots>

                <enabled>false</enabled>

            </snapshots>

        </pluginRepository>

    </pluginRepositories>

</project>

  • After adding the pom now we have to add the operations as here we will add the operation for multiplication and addition by removing the by-default operation in the module-Calculator Connetor.xml file.The operation are defined below:

<operation name=”Multiply” doc:description=”Takes two numbers and returns the product of them”>

     <parameters>

       <parameter name=”numberA” type=”number”/>

       <parameter name=”numberB” type=”number”/>

     </parameters>

     <body>

       <mule:set-payload value=”#[vars.numberA * vars.numberB]”/>

     </body>

    <output type=”number”/>

   </operation>

     <operation name=”sum” doc:description=”Takes two numbers and returns the sum of them”>

     <parameters>

       <parameter name=”numberA” type=”number”/>

       <parameter name=”numberB” type=”number”/>

     </parameters>

     <body>

       <mule:set-payload value=”#[vars.numberA + vars.numberB]”/>

     </body>

    <output type=”number”/>

   </operation>

  • After adding the dependencies in the pom.xml and operations in the module-Calculator Connector.xml  save the project and now go to the project directory, click on the path, and open the command prompt from there as shown below:

  • Then after opening the command prompt write the command – 

mvn clean install -DskipTests

And after that after the execution of the command there will be build success as shown below:

Go to Anypoint Studio build one new project and add the dependency of the above imported project to this project pom as shown below:

  1. Take the dependency from the previous project i.e, the groupId, artifactId and the version as shown below: 

After that come to the new project created and in its pom.xml inside the <dependencies> tag add a <dependency> tag as shown below:

After that inside the <dependency> tag add the dependencies taken from the other project i.e, the groupId, artifactId, and version as shown below:

Atlast add the <classifier> tag and write there mule-plugin as shown below and save the project.

We can see in the mule-palette Calculator Connector module is there and inside that two operations added are there as shown below:

Now drag and drop the sum or multiply connector and do the configuration i.e, just give the numbers to add or multiply and also add a transform message to get the output in json.

 Save the project and deploy it.

Once it get deployed hit the request from postman and we can see the output as shown below:

Also as the logger is added in the flow we can see the output in the console also.

Leave a Comment