MuleSoft Technical Guides

Create Custom Connector using Mule XML SDK

User MuleSoft Integration Team
Calendar April 02, 2021

MuleSoft creates a great opportunity of reusing the readily available connectors but in some cases, the connector might not be available for connecting to a particular system. We need to write custom logic like spring beans/Java code to connect to it. However, if we don’t want to include the same logic in every API integration which implements the requirement, we can create a custom connector. There are broadly two types of custom connectors:

  1. Java SDK
  2. XML SDK.

This article will look at the XML SDK , which provides an easier learning curve than Java SDK and is more like developing a Mule application.

Anatomy of XML SDK

  1. Operations – An operation in XML SDK is relatable to a Java method with a set number of arguments required (can have zero arguments as well), a body to process, and an output.
  2. Properties – To define a global configuration, we can use properties inside a connector. When specified, a user can create a global configuration with defined properties.

Creating a custom connector

The first step while creating a custom connector is to generate a skeleton project.

Run the below command in the command prompt.

mvn archetype:generate -DarchetypeGroupId=org.mule.extensions -

DarchetypeArtifactId=mule-extensions-xml-archetype -

DarchetypeVersion=1.2.0 -DgroupId=demo-connector -DartifactId=demo-

connector -DmuleConnectorName=demo-connector

 

Argument Description
-DarchetypeGroupId Group ID of Archetype in the Mule repository
-DarchetypeArtifactId Artifact ID of the Archetype in the Mule repository
-DarchetypeVersion Version of the aerchetype
-DgroupId Group ID of the user’s Mule application
-DartifactId Artifact Id for user’s application/module
-DmuleConnectorName Name of the connector

Once generated, import it in MuleSoft Anypoint Studio. The structure looks like below:

 xml-sdk-structure

module-Hello.xml contains the main logic implemented by the connector.

Here, we will write a simple connector with two operations. One to modify the incoming query parameter and second to return the sum of two numbers which will further pass in the parameters.

Operation to get sum of two numbers

<operation name="Get-Sum" doc:description="Return the sum of two incoming paramters">
		<parameters>
			<parameter name="num1" type="number" use="REQUIRED"/>
			<parameter name="num2" type="number" use="REQUIRED"/>
		</parameters>
		<body>
			<mule:set-payload
				value="#[vars.num1 + vars.num2]" />
		</body>
		<output type="number" />
	</operation>

Operation to modify the query param

<operation name="Modify-query-parameter"
		doc:description="Modifies the incoming query paramter">
		<parameters>
			<parameter name="qParam" type="string" use="REQUIRED"/>
		</parameters>
		<body>
			<mule:set-payload value="#['Hello ' ++ vars.qParam]" />
		</body>
		<output type="string" />
	</operation>

 

To install the connector locally, run mvn clean install

To use the connector in project, add the dependency

<dependency>
	<groupId>{group id of connector}</groupId>
	<artifactId>{artifact id of connector}</artifactId>
	<version>{version of connector}</version>
	<classifier>mule-plugin</classifier>
</dependency>

Once done, the connector will be available in the Mule palette.

xml-sdk-pallette-view.

Add the operations in the flow as required. Below is the property view for each operation.

xml-sdk-operation-1xml-sdk-operation-2

Conclusion

Mule XML-based SDK is a straightforward and efficient way to create a custom connector and fulfill any ad hoc requirements.

Find more MuleSoft Technical guides at Caelius Consulting MuleSoft Resource Centre.

 

Leave a comment

Your email address will not be published. Required fields are marked *