Mule Smart Logging Connector Using XML SDK
This document describes demonstrating steps to build a mule soft logging connector using XML SDK instead of java SDK connector.
Join the DZone community and get the full member experience.
Join For FreePreface
This document describes demonstrating steps to build a mule soft logging connector using XML SDK instead of java SDK connector, which extends mule runtime capabilities.
This approach is easier to build compared to the existing JAVA Mule SDK
SDK in Mule 4
Mule SDK connector enables reusability, offload complexity, and loosely coupled from mule project.
When we develop a Mule connector module using either the Java or XML SDKs, we are developing on top of Mule Extensions API. This API is a well-defined contract for how modules can interact with the Mule Runtime. These SDKs provide a simple layer of abstraction over the Extensions API so developers do not need to have details of how to use the API. The arc-type maven command is responsible to generate this skeleton.
XML SDK Use Case Mule-smart-logging-connector
A most useful logging connector can be created using XML SDK to fit into any my mule soft project.
Prerequisites:
Java 1.8
Mule Runtime 4.x.x
Any point Studio 7.XX
Step 1: Generation of XML SDK Skeleton
To create the XML SDK template, we should run the following command at any desired location.
xxxxxxxxxx
mvn archetype:generate -DarchetypeGroupId=org.mule.extensions -DarchetypeArtifactId=mule-extensions-xml-archetype -DgroupId=org.mule.extension -DartifactId=mule-smart-logging-connector -DmuleConnectorName=mule-smart-logging-connector
If the studio is installed with https://repository.mulesoft.org/nexus-ee/content/repositories/releases/archetype-catalog.xml, we can generate this project in the studio as well.
This will generate a skeleton with extension file module-Hello.xml, which is having below structure seen in any point studio 7.4
Step 2: Replace module-hello.xml with module-smart-logging-Connector.xml, with below simple self-explanatory xml code.
xxxxxxxxxx
<module name="mule-smart-logging-connector"
prefix="smart-logger"
doc:description="This module relies in runtime provided components"
xmlns="http://www.mulesoft.org/schema/mule/module"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mule="http://www.mulesoft.org/schema/mule/core"
xmlns:httpn="http://www.mulesoft.org/schema/mule/http"
xmlns:wsc="http://www.mulesoft.org/schema/mule/wsc"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/module http://www.mulesoft.org/schema/mule/module/current/mule-module.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<property name="AppName" type="string"></property>
<property name="ApiVersion" type="string"></property>
<property name="MuleEnvironment" type="string"></property>
<operation name="smartlog">
<parameters>
<parameter name="messageid" type="string"/>
<parameter name="transactionid" type="string"/>
<parameter name="event" type="string"/>
</parameters>
<body>
<mule:logger category="com.techstar.log" level="INFO" doc:name="Logger" message="#['{'++'"transactionid"'++ ':' ++ '"' ++ vars.transactionid ++'"'++ ','
++ '"messageid"'++':' ++ '"'++ vars.messageid ++ '"'++ ','
++ '"AppName"'++':' ++ '"'++ vars.AppName ++ '"'++ ','
++ '"ApiVersion"'++':' ++ '"'++ vars.ApiVersion ++ '"'++ ','
++ '"MuleEnv"'++':' ++ '"'++ vars.MuleEnvironment ++ '"'++ ','
++ '"logevent"'++':' ++ '"'++ vars.event ++ '"'++ '}']"></mule:logger>
</body>
</operation>
</module>
Here I am declaring three input parameters message-id, transaction-id and event, and 3 configuration property parameter AppName, API version, and MuleEnv as part of my smart logger required parameter.
Finally, log all the above parameters under a package or category com.techstar.log by concatenating all the above parameters.
Step 3: To resolve a few dependencies like a mule:log etc, it may require to update or add maven plugin latest version in the pom file.
xxxxxxxxxx
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven.resources.plugin.version}</version>
Step 4: Install the connector using simple maven command
xxxxxxxxxx
mvn clean install -DskipTests
This step will install the smart connector in local maven repository
Step 5:- Add this dependency in any project pom file
xxxxxxxxxx
<dependency>
<groupId>org.mule.extension</groupId>
<artifactId>mule-smart-logging-connector</artifactId>
<version>1.0.1</version>
<classifier>mule-plugin</classifier>
</dependency>
Step 6:- complete the configuration for the smart connector in the project, which looks likes below
Step 7: Once it runs in the studio, the console logs looks like below
xxxxxxxxxx
INFO 2020-08-20 21:32:19,862 [[MuleRuntime].uber.03: [mule-test-app].mule-test-appFlow.CPU_LITE ] [processor: smartlog/processors/0; event: 86842840-e2fe-11ea-aa04-4c1d96260e7b] com.techstar.log: {"transactionid":"814cf14a-1c54-4257-bc26-a2de7f195bf3","messageid":"0bcb76cb-a99d-4107-8423-ca1369ed75d7","AppName":"mule-test-app","ApiVersion":"v1.0","MuleEnv":"Dev","logevent":"Entering Main FLow"}
Step 8: Once it deployed in the cloud, the runtime logs look like below
Limitations:
The community edition having limited functionality available for development.
Final Conclusion
The Mule SDK allows MuleSoft developers to extend the Mule runtime with good features. This is made up of Java SDK, and the XML SDK. XML SDK is easier to develop without using any java code in the mule.
Before we start developing a Mule module, we should decide which SDK and which version we are going to use, etc.
The decisions we make will depend on your particular project requirement.
References
Hope this will be useful.
Opinions expressed by DZone contributors are their own.
Comments