Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Create a Custom Connector Using XML SDK and Deploy it in Anypoint Exchange

DZone 's Guide to

Create a Custom Connector Using XML SDK and Deploy it in Anypoint Exchange

This tutorial explains how to create a custom connector using XML SDK and deploy it in Anypoint Exchange.

· Integration Zone ·
Free Resource

Introduction

XML SDK is an alternative approach to build custom modules in Mulesoft. This approach is comparatively easier to build compared to the already existing JAVA Mule SDK. Creating a custom module in Mule SDK is similar to creating a Mule application. This document is focused on demonstrating the steps to build a custom module using the XML SDK and deploying it in Anypoint Exchange. We will create a custom connector in Mule 4 that will perform basic arithmetic operations like addition and subtraction

Requirements

Application/Service

Version

Mule Runtime

4.0.0 or later

Anypoint Studio

7.0 or later

Java

1.8.0_x

Creating an XML SDK application

The first step in creating a custom module is to generate the skeleton for the application.

For creating the XML SDK skeleton, one should run the following command through the desired location:

mvn archetype:generate -DarchetypeGroupId=org.mule.extensions -DarchetypeArtifactId=mule-extensions-xml-archetype -DarchetypeVersion=1.3.0-SNAPSHOT -DgroupId=com.companyName.mule.modules -DartifactId=mule4-arithmetic-connector -Dpackage=mule-module -Dversion=1.0.0-SNAPSHOT -DextensionName="Arithmetic Connector"

Note: This command should be run in one single line.

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

Note: It is important that the archetypeVersion points to the latest version to incorporate all the features.

The output after you run the following command in the command prompt/ terminal:

Once it is built successfully, the skeleton project is created in the folder.

The next step is to import this project in Anypoint Studio.

Open Anypoint Studio 7 or higher and import the project.

Click on File -> Import Project from file system

In the Directory tab, provide the path for the root folder of the structure created from the previous step.

On successful import, the Package Explorer should look like this:

The entire functionality is in the file “module-Arithmetic-Connector.xml”.

The Test suites for the module are in the file “assertion-munit-test.xml”.

The files consist of a template code and munit-test cases.


The Mule 4 connectors are modularized, hence each connector is now a collection of various operations.

If we analyze the file “module-Arithmetic-Connector.xml”, the operation tag consists of three important tags which are Parameters, Body, and Output.

  • The <parameter> tag defines the input parameters for our operation

  • The <body> tag consists of all the logic for that operation.

  • The <output> tag defines the output of the particular operation. An operation can return only one value as an output.

Here’s the sample code for our Arithmetic Connector:

<?xml version="1.0" encoding="UTF-8"?>
<module name="Arithmetic Connector Smart Connector" prefix="module-arithmetic-connector"
doc:description="This module relies in runtime provided components"

xmlns="http://www.mulesoft.org/schema/mule/module" xmlns:mule="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:tns="http://www.mulesoft.org/schema/mule/module-arithmetic-connector"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/module http://www.mulesoft.org/schema/mule/module/current/mule-module.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/module-arithmetic-connector http://www.mulesoft.org/schema/mule/module-arithmetic-connector/current/mule-module-arithmetic-connector.xsd">

<operation name="Addition" doc:description="Add two objects">
<parameters>
<parameter name="number1" displayName="Number 1" use="REQUIRED"
type="number" example="1"></parameter>
<parameter name="number2" displayName="Number 2" use="REQUIRED"
type="string" example="2"></parameter>
</parameters>
<body>
<mule:set-payload value="#[vars.number1 + vars.number2]"></mule:set-payload>
</body>
<output type="string" />
</operation>

<operation name="Subtraction" doc:description="Subtract two objects">
<parameters>
<parameter name="number1" displayName="Number 1" use="REQUIRED"
type="number" example="1"></parameter>
<parameter name="number2" displayName="Number 2" use="REQUIRED"
type="string" example="2"></parameter>
</parameters>
<body>
<mule:set-payload value="#[vars.number1 - vars.number2]"></mule:set-payload>
</body>
<output type="string" />
</operation>
</module>

The above connector performs two basic Arithmetic operations: Addition and Subtraction

Writing Munit test cases is optional for this connector, so we are skipping the demonstration of Munits.


To install the connector locally, execute the following command in the terminal:

 mvn install 

If there are no errors in the project, then it should build successfully.

To utilize this connector in a project, add the following dependency:

<dependency>
<groupId>com.companyName.mule.modules</groupId>
<artifactId>mule4-arithmetic-connector</artifactId>
<version>1.0.0-SNAPSHOT</version>
<classifier>mule-plugin</classifier>
</dependency>

To deploy this connector in Exchange, there are few additional steps required

  • Update the groupId in the pom.xml to your Anypoint Organization Id
  • Add the following snippet in your pom.xml:

Note: Replace {{orgId}}) with your Organization Id

  • Add the credentials of your Anypoint Platform in the maven settings.xml under <servers> tag:
<server>
<id>Repository</id>
<username>{{Your Username}}</username>
<password>{{Your Password}}</password>
</server>
  • Add the Repository profile in your maven settings.xml under <profiles> tag
<profile>
<repositories>
<repository>
<id>Repository</id>
<name>Corporate Repository</name>
<url>https://maven.anypoint.mulesoft.com/api/v1/organizations/{{orgId}}/maven</url>
</repository>
</repositories>
<id>Repository</id>
</profile>

Activate this profile by adding it in the <activeProfiles> tag

<activeProfile>Repository</activeProfile>

Once these changes are done, execute the command “mvn deploy” from the terminal.

On successful build, you should view the connector in your Anypoint Exchange

To utilize this connector in a project, add this dependency in the pom.xml

<dependency>
<groupId>{{orgId}}</groupId>
<artifactId>mule4-arithmetic-connector</artifactId>
<version>1.0.0-SNAPSHOT</version>
<classifier>mule-plugin</classifier>
</dependency>

Well, we just created our first Connector using Mule XML SDK and deployed to Anypoint Exchange.

References

Topics:
mulesoft api ,anypoint connectors ,anypoint exchange ,mule 4 custom connector ,integration tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}