DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • MuleSoft Integration With RabbitMQ
  • Mule Custom Connector Using XML SDK
  • Log Forwarding Connector Mule XML-SDK
  • Mule Smart Logging Connector Using XML SDK

Trending

  • The 4 R’s of Pipeline Reliability: Designing Data Systems That Last
  • The Modern Data Stack Is Overrated — Here’s What Works
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  • Rethinking Recruitment: A Journey Through Hiring Practices
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. MuleSoft Custom Connector With XML SDK

MuleSoft Custom Connector With XML SDK

We go over a tutorial on how to work with the new MuleSoft XML SDK to perform basic operations and connect these operations to MuleSoft's Anypoint Exchange.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Dec. 28, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
10.7K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

MuleSoft created an XML SDK that lets developers write custom connectors and it's actually easier than using the Java-based Mule SDK. The new XML SDK is one of the easiest ways to write code for a custom connector with MuleSoft.

Here are the basic components of the new SDK:

  • Operations: these are basically a set of parameters, the body, and the output. Operations are a type of function which takes multiple parameters, performs a given set of actions, and gives a single output. For example, if you want to add two numbers,  'Add' is the operation. It will take two parameters, and the action will be performed in the body, which will add the two numbers. The output will then be the sum of these two numbers.
  • Input parameters: a set of parameters that declares the type to be entered when calling the operation. 
  • Body: where the action is performed. It executes the sequence of components, like flows.
  • Output: declares an output type for your XML SDK module.
  • Errors: declares an error type that the XML SDK can raise in the body.

Setting Up an XML SDK Connector Project

To get started, add the below profile to the settings.xml file located in your .m2 repository.

XML
 




x
17


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<profiles>
3
   <profile>
4
      <id>Mule</id>
5
      <activation>
6
         <activeByDefault>true</activeByDefault>
7
      </activation>
8
      <repositories>
9
         <repository>
10
            <id>mulesoft-releases</id>
11
            <name>MuleSoft Repository</name>
12
            <url>http://repository.mulesoft.org/releases/</url>
13
            <layout>default</layout>
14
         </repository>
15
      </repositories>
16
   </profile>
17
</profiles>


Next, add Anypoint credentials to your settings.xml file located in the .m2 repository.

XML
 




xxxxxxxxxx
1


 
1
<servers>
2
   <server>
3
      <id>Repository</id>
4
      <username>Anypoint_Username</username>
5
      <password>Anypoint_Password</password>
6
   </server>
7
</servers>


Complete the settings.xml File

Your settings.xml file should now look like this:

XML
 




xxxxxxxxxx
1
24


 
1
<settings><profiles>
2
<profile>
3
         <id>Mule</id>
4
         <activation>
5
             <activeByDefault>true</activeByDefault>
6
         </activation>
7
         <repositories>
8
             <repository>
9
                 <id>mulesoft-releases</id>
10
                 <name>MuleSoft Repository</name>
11
                 <url>http://repository.mulesoft.org/releases/</url>
12
                 <layout>default</layout>
13
             </repository>
14
         </repositories>
15
     </profile>
16
 </profiles>
17
 <servers>
18
    <server>
19
       <id>Repository</id>
20
       <username>Anypoint_Username</username>
21
       <password>Anypoint_Password</password>
22
     </server>
23
  </servers>
24
</settings>


Use the below command to generate a Maven project using an archetype. This command can be executed in command prompt or any command line interface.

Java
 




x


 
1
mvn archetype:generate                                       
2
  -DarchetypeGroupId=org.mule.extensions                     
3
  -DarchetypeArtifactId=mule-extensions-xml-archetype        
4
  -DarchetypeVersion=1.2.0                                   
5
  -DgroupId=math-connector-extension                               
6
  -DartifactId=math-connector-extension                          
7
  -DmuleConnectorName=math-connector


Here is a brief description of all the attributes contained in the above command:

Attributes Description
archetypeGroupId
The GroupId in the Mule repository where the skeleton of the XML SDK is defined.
archetypeArtifactId
The ArtifactId of  the archetype in the Mule repository.
archetypeVersion
A version of the archetype.
groupId
The GroupId for your application or module. Generally, it should be an AnypointOrganizationId.
artifactId
The ArtifactId for your project or module.
muleConnectorName
The name of your Mule custom connector.

Once the project is generated, you can import it into Anypoint Studio. This will generate the skeleton of your project and it will look like this: 

We will write all our code in the module-<app>.xml file contained in the following folder: src/main/resources/org/mule/extensions/smart/connector.

Next, we will write the following four operations and deploy the connector to Anypoint Exchange.

  • Add two numbers.
  • Subtract two numbers.
  • Multiply two numbers.
  • Divide two numbers.

Here is the code with these four operations:

XML
 




xxxxxxxxxx
1
58


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<module name="Hello Smart Connector"
3
        prefix="module-hello"
4
        doc:description="This module relies in runtime provided components"
5
        xmlns="http://www.mulesoft.org/schema/mule/module"
6
        xmlns:mule="http://www.mulesoft.org/schema/mule/core"
7
        xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
8
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9
        xsi:schemaLocation="
10
           http://www.mulesoft.org/schema/mule/module http://www.mulesoft.org/schema/mule/module/current/mule-module.xsd
11
           http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
12
           http://www.mulesoft.org/schema/mule/module-hello http://www.mulesoft.org/schema/mule/module-hello/current/mule-module-hello.xsd">
13

          
14
    <operation name="add-two-numbers" doc:description="Add Two Numbers">
15
    <parameters>
16
    <parameter name="value1" type="number" />
17
     <parameter name="value2" type="number" />
18
     </parameters>
19
        <body>
20
            <mule:set-payload value="#[vars.value1 + vars.value2]"/>
21
        </body>
22
        <output type="number" doc:description="Payload's output"/>
23
    </operation>
24
    
25
     <operation name="subtract-two-numbers" doc:description="Subtract Two Numbers">
26
   <parameters>
27
    <parameter name="value1" type="number" />
28
     <parameter name="value2" type="number" />
29
     </parameters>
30
        <body>
31
            <mule:set-payload value="#[vars.value1 - vars.value2]"/>
32
        </body>
33
        <output type="number" doc:description="Payload's output"/>
34
    </operation>
35

          
36
   <operation name="multiply-two-numbers" doc:description="Multiply Two Numbers">
37
    <parameters>
38
    <parameter name="value1" type="number" />
39
     <parameter name="value2" type="number" />
40
     </parameters>
41
        <body>
42
            <mule:set-payload value="#[vars.value1 * vars.value2]"/>
43
        </body>
44
        <output type="number" doc:description="Payload's output"/>
45
    </operation>
46
    
47
    <operation name="divide-two-numbers" doc:description="Divide Two Numbers">
48
     <parameters>
49
    <parameter name="value1" type="number" />
50
     <parameter name="value2" type="number" />
51
      </parameters>
52
        <body>
53
            <mule:set-payload value="#[vars.value1 / vars.value2]"/>
54
        </body>
55
        <output type="number" doc:description="Payload's output"/>
56
    </operation>
57
 </module>


Deploying a Connector to Exchange

Add the below Anypoint Exchange repository to the pom.xml file under the distributionManagement tag. Make sure {orgId} is replaced with your AnypointOrganizationId.

XML
 




xxxxxxxxxx
1
13


 
1
<distributionManagement>
2
        <snapshotRepository>
3
            <id>Repository</id>
4
            <name>Exchange Repository</name>
5
    <url>https://maven.anypoint.mulesoft.com/api/v1/organizations/{orgId}/maven</url>
6
            <layout>default</layout>
7
        </snapshotRepository>
8
        <repository>
9
            <id>Repository</id>
10
            <name>Exchange Repository</name>            <url>https://maven.anypoint.mulesoft.com/api/v1/organizations/{orgId}/maven</url>
11
            <layout>default</layout>
12
        </repository>
13
    </distributionManagement>


Execute the below command on the root of your project and it will deploy the project to Anypoint Exchange.

Plain Text
 




xxxxxxxxxx
1


 
1
mvn deploy


Adding a Connector to a Mule Application

We need to add a connector dependency to the application's pom.xml file. We can fetch this from the Anypoint Exchange connector details page.

You need to click on the Dependency Snippets and it will provide a dependency that you need to add in to your application.

Once you add the dependency, the connector and its operation will be visible in your Mule Palette.

GitHub Code: https://github.com/Jitendra85/MuleSoft-XML-SDK-Connector.git

Now you know how to use the MuleSoft XML SDK to create a custom connector.

XML Software development kit Connector (mathematics) MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft Integration With RabbitMQ
  • Mule Custom Connector Using XML SDK
  • Log Forwarding Connector Mule XML-SDK
  • Mule Smart Logging Connector Using XML SDK

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!