Mule Custom Connector Using XML SDK
In this blog, we will look into the implementation of Mule XML Connector which invokes a java class.
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:
Here we are going to create a connector that will use java code to Flatten the JSON.
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.
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.
- Parameters: The input parameters required for the operation.
- Body: Contains the logic to be performed.
- Output: Gives the single output after performing the 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:
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.
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:
Step 6) We should deploy the connector to exchange. We should make few changes to the pom.xml and settings.xml files.
We have to add a distributionManagement tag to deploy to exchange.
- 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:
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:
We should add the above dependency details in the project’s pom.xml, then the connector will get imported and we can use it.
Call the endpoint to see the flatten result.
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.
Opinions expressed by DZone contributors are their own.