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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • MuleSoft Custom Connector With XML SDK
  • Log Forwarding Connector Mule XML-SDK
  • Mule Smart Logging Connector Using XML SDK
  • Make Your Integration Seamless By Using Ballerina Client Connectors

Trending

  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • How to Merge HTML Documents in Java
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  1. DZone
  2. Coding
  3. Languages
  4. Mule Custom Connector Using XML SDK

Mule Custom Connector Using XML SDK

In this blog, we will look into the implementation of Mule XML Connector which invokes a java class.

By 
Vyshnavi Jaksani user avatar
Vyshnavi Jaksani
·
Gaurav Dhimate user avatar
Gaurav Dhimate
DZone Core CORE ·
Updated Feb. 13, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
10.1K Views

Join the DZone community and get the full member experience.

Join For Free

Why do we need a custom connector when there is already a lot of connectors available from MuleSoft? Well, reasons can be various such as reusability, simplicity of use, compactness, etc.

Suppose you have a legacy system for which there is no connector available and it is a complex one to connect, so now you want to hide this complex logic somewhere and make sure all the API which are planning to connect to this legacy system does without knowing the complexity of it. 

Other benefits being:

  • You don’t need to replicate the code to access the system across multiple APIs.
  • You just need to maintain logic in one place. 

There are two ways of creating custom connectors in MuleSoft.

  • Using Java SDK
  • Using XML SDK

In this blog, we will see how to create a custom connector that invokes a Java class using XML SDK.

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. In fact, you can use existing Mule components in the module. The framework adds few additional elements as below:

  • Module
  • Operation
  • Properties

Here we are going to create a connector that will use java code to Flatten the JSON.

Example:

Input:

input

Output:

output

Flatten JSON will give output as a string with a key as a JSON key, along with its index if it is an array in a hierarchy. For eg: in the above output, “cars.0.models.0” signifies the first array element of the cars array and the first element of the models array is “Fiesta.”

Step1) First we will create an empty project using the maven command as below.

mvn archetype:generate -DarchetypeGroupId=org.mule.extensions -DarchetypeArtifactId=mule-extensions-xml-archetype -DarchetypeVersion=1.2.0 -DgroupId=<<groupId>> -DartifactId=flatten-json -DmuleConnectorName=flatten-json 

Once the above command is successful, an archetype project will be created.

Step 2) We have to import the created project into Anypoint studio and add the plugin repositories into the project pom.xml file.

XML
 




x
11


 
1
<pluginRepositories>
2
        <pluginRepository>
3
            <id>mulesoft-releases</id>
4
            <name>mulesoft release repository</name>
5
            <layout>default</layout>
6
            <url>https://repository.mulesoft.org/releases/</url>
7
            <snapshots>
8
                <enabled>false</enabled>
9
            </snapshots>
10
        </pluginRepository>
11
    </pluginRepositories>



Step 3) Using XML SDK for connector development, we cannot use the drag and drop functionality. We will get a module-<<extension>>.xml file under src/main/resources from the first step. We need to add an operation with the required parameters and logic to flatten JSON. The below screenshot shows the operation code. 

Operation Contains:

  • Parameters: The input parameters required for the operation.
  • Body: Contains the logic to be performed.
  • Output: Gives the single output after performing the operation.
XML
 




xxxxxxxxxx
1
21


 
1
 <operation name="flatten-json" doc:description="flattens json">
2
        <parameters>
3
            <parameter name="inputJson" type="string"/>
4
        </parameters>
5
        <body>
6
            <java:invoke-static doc:name="Invoke static" doc:id="d89e1c97-f268-412e-86ca-7b3b56586db8" class="com.flatten.json.FlattenJson" method="startParsing(String)">
7
            <java:args ><![CDATA[#[{
8
    "arg0": vars.inputJson
9
}]]]></java:args>
10
        </java:invoke-static>
11
        <ee:transform doc:name="Transform Message" doc:id="21eabde8-3ee2-46bc-9e15-e0c5ee379b2e" >
12
            <ee:message >
13
                <ee:set-payload ><![CDATA[%dw 2.0
14
output application/json
15
---
16
payload]]></ee:set-payload>
17
            </ee:message>
18
        </ee:transform>
19
        </body>
20
        <output type="string" doc:description="Payload's output"/>
21
    </operation>



I have added the java code to flatten the JSON under package named com/flatten/json in src/main/java and it looks as shown below:

flatten-json package

Step 4) We should add the required namespaces to the module.xml file as we used to transform the message, and invoke Java in the operation.

Adding required namespaces

XML
 




xxxxxxxxxx
1
18


 
1
<module name="flattenJson"
2
        prefix="module-flattenjson"
3
        doc:description="This module relies in runtime provided components"
4
        category="SELECT"
5
        xmlns="http://www.mulesoft.org/schema/mule/module"
6
        xmlns:java="http://www.mulesoft.org/schema/mule/java"
7
        xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
8
        xmlns:mule="http://www.mulesoft.org/schema/mule/core"
9
        xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
10
        xmlns:tns="http://www.mulesoft.org/schema/mule/module-flattenjson"
11
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
12
        xsi:schemaLocation="
13
           http://www.mulesoft.org/schema/mule/module http://www.mulesoft.org/schema/mule/module/current/mule-module.xsd
14
           http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
15
           http://www.mulesoft.org/schema/mule/java http://www.mulesoft.org/schema/mule/java/current/mule-java.xsd
16
           http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
17
           http://www.mulesoft.org/schema/mule/module-flattenjson http://www.mulesoft.org/schema/mule/module-flattenjson/current/mule-module-flattenjson.xsd">
18
 
          



Step 5) This step is required in case you want to change the default packaging of the application .jar. Here in this blog, we are trying to include the Java class into the .jar which in turn will be used by our MuleSoft application.

For this, we should create a “META-INF/mule-artifact/mule-artifact.json” file with an exported package containing the created java package(com.flatten.json). By doing this, the runtime will find the java class FlattenJson.java in the imported project.

The Folder structure:

Folder structure

mule-artifact.json:

JSON
 




xxxxxxxxxx
1
29


 
1
{
2
  "extensionModelLoaderDescriptor": {
3
    "id": "xml-based",
4
    "attributes": {
5
      "validate-xml": true,
6
      "resources-paths": [],
7
      "resource-xml": "flatten/json/module-flattenJson.xml"
8
    }
9
  },
10
  "name": "flattenJson Smart Connector",
11
  "requiredProduct": "MULE_EE",
12
  "classLoaderModelLoaderDescriptor": {
13
    "id": "mule",
14
    "attributes": {
15
    "exportedResources": [],
16
 
          
17
"exportedPackages":[
18
 
          
19
"com.flatten.json"
20
 
          
21
]
22
    }
23
  },
24
  "bundleDescriptorLoader": {
25
    "id": "mule",
26
    "attributes": {}
27
  },
28
  "minMuleVersion": "4.1.1"
29
}



Step 6) We should deploy the connector to exchange. We should make few changes to the pom.xml and settings.xml files.

Settings.xml file:

XML
 




x


 
1
<settings>
2
<profiles>
3
    <profile>
4
        <id>Mule</id>
5
        <activation>
6
            <activationByDefault>true</activationByDefault>
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>
18
<servers>
19
    <server>
20
        <id>Repository</id>
21
        <username>anypoint-username</username>
22
        <password>anypoint-pwd</password>
23
    </server>
24
</servers>
25
</settings>
26
 
          



Pom.xml:

We have to add a distributionManagement tag to deploy to exchange.

XML
 




xxxxxxxxxx
1
14


 
1
<distributionManagement>
2
        <snapshotRepository>
3
            <id>Repository</id>
4
            <name>Exchange Repository</name>
5
            <url>https://maven.anypoint.mulesoft.com/api/v1/organizations/b4f0eff7-65da-4416-a387-ce2ca54fbb20/maven</url>
6
            <layout>default</layout>
7
        </snapshotRepository>
8
        <repository>
9
            <id>Repository</id>
10
            <name>Exchange Repository</name>           
11
            <url>https://maven.anypoint.mulesoft.com/api/v1/organizations/b4f0eff7-65da-4416-a387-ce2ca54fbb20/maven</url>
12
            <layout>default</layout>
13
        </repository>
14
    </distributionManagement>



  • Replace <<orgId>> with the organizationId of the Anypoint account.
  • Executing the below command from the root of the project deploys the connector to Exchange: "mvn deploy"
  • Once deployed to Exchange, the connector will get listed in the Exchange:

Connector listed in the exchange

Step 7) We are ready to use the connector. In the Mule project, we can add the created connector dependency and we can find the connector and its methods as shown below. Dependency details will be found in Exchange:

Dependency snippets

We should add the above dependency details in the project’s pom.xml, then the connector will get imported and we can use it.

testflattenjsonFlow

Call the endpoint to see the flatten result.

Calling the endpoint to see the flatten result

Conclusion

So we have seen how we can XML SDK to create a custom connector and then how we can call a Java class from the custom connector.

Thank you for reading.

XML Software development kit Connector (mathematics)

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft Custom Connector With XML SDK
  • Log Forwarding Connector Mule XML-SDK
  • Mule Smart Logging Connector Using XML SDK
  • Make Your Integration Seamless By Using Ballerina Client Connectors

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!