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

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

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

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

  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Hexagonal Architecture in the Frontend: A Real Case
  • Kafka JDBC Source Connector for Large Data
  • Make Your Integration Seamless By Using Ballerina Client Connectors

Trending

  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • How to Practice TDD With Kotlin

Mule 4 Custom Connector and Icon

In this step-by-step tutorial, learn how to create a simple Mule 4 custom connector using Mule XML SDK and how to change the icon for this custom connector.

By 
Karan Gupta user avatar
Karan Gupta
·
Jan. 24, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
9.2K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will see how to create a custom connector using XML SDK and change the icon for Mule 4 custom connector.

Firstly, you need to create a custom connector in Mule 4. Let's create a simple logging connector that prints a message in the console, and we will also change the default icon for the connector.

We will be creating a connector using XML SDK. There are certain things we need to understand (listed below) before proceeding further with the development.

  • Operations: It is just like a function with a set of input parameters, body, and output. In our case, we will be creating a logger that takes messages and displays the output on the console.
  • Input parameters: Type of parameter when calling the operation.
  • Body: Here you can call a Mule flow or display the output directly.
  • Output: Define the type of the payload after it is processed.
  • Errors: Error types that can be raised.

To start with, we need to first run the below Maven command to create a Maven project using a Maven archetype. Run this command in a command prompt in the folder where you want this project to be generated.

Java
 
mvn archetype:generate 
-DarchetypeGroupId=org.mule.extensions 
-DarchetypeArtifactId=mule-extensions-xml-archetype 
-DarchetypeVersion=1.2.0 
-DgroupId=org.mule.extension 
-DartifactId=mule-custom-logger
-DmuleConnectorName=logger

 

Once the project is created you can now import the same in Anypoint Studio. Below is the structure I see.screenshot of menu for mule-custom-logger

Let us now modify the module-custom-logger.xml file. Since we are creating a simple logger, we will add only the message field as a parameter in the LogMessage operation and print it on the console.

XML
 
<?xml version="1.0" encoding="UTF-8"?>
<module name="Custom Logger"
        prefix="module-custom-logger"
        doc:description="This module relies in runtime provided components"

        xmlns="http://www.mulesoft.org/schema/mule/module"
        xmlns:mule="http://www.mulesoft.org/schema/mule/core"
        xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
        xmlns:tns="http://www.mulesoft.org/schema/mule/module-custom-logger"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
           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/module-hello http://www.mulesoft.org/schema/mule/module-custom-logger/current/mule-module-custom-logger.xsd">

<operation name="LogMessage"
		doc:description="This operation builds a standard message and logs into the log file'">
		<parameters>
			<parameter name="logLevel" displayName="Log Level" type="any" defaultValue='INFO'
				doc:description="Select the appropriate log level" />
			<parameter name="message" displayName="Message"
				use="OPTIONAL" type="any" defaultValue='Message to log'
				doc:description="Message to be logged" />
		</parameters>
		<body>
			<mule:logger level="INFO" doc:name="Log DEBUG" doc:id="e2a25903-83a5-4949-bf93-878ac1a6015e" message='#[output application/json
---
{
	logLevel: vars.logLevel default "INFO",
	message: vars.message}]'/>
		</body>
		<output type="string" />
	</operation>

 </module>

 

Once this is done, we will now change the default icon for the connector in the root repository, create an icon folder, and add an icon.svg image of your choice inside of it (as shown below).Changing default icon

After this, modify your POM file, i.e. add your organization id in groupId element tag and also add respository. Then deploy your code. Also make sure you have set your Anypoint username and password in the settings.xml file. Below is my project POM file.

XML
 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>353f142f-2292-45c3-a93b-778a4430b8a9</groupId>
	<artifactId>mule-custom-logger</artifactId>
	<packaging>mule-extension</packaging>
	<version>1.0.2-SNAPSHOT</version>

	<name>mule-custom-logger</name>
	<description>A Mule extension done with pure Mule DSL that depends in the runtime operations (set-payload, set-variable, etc.)</description>

	<properties>
		<mule.version>4.1.1</mule.version>
		<mule.extensions.maven.plugin.version>1.1.6</mule.extensions.maven.plugin.version>

		<munit.version>2.1.0</munit.version>
		<munit.extensions.maven.plugin.version>1.0.0-BETA</munit.extensions.maven.plugin.version>
		<munit.input.directory>src/test/munit</munit.input.directory>
		<munit.output.directory>${basedir}/target/test-mule/munit</munit.output.directory>

		<maven.resources.version>3.0.2</maven.resources.version>
	</properties>

	<dependencies>
		<!--Needed to discover the 'xml-based' XmlExtensionLoader for smart connectors-->
		<dependency>
			<groupId>org.mule.runtime</groupId>
			<artifactId>mule-module-extensions-xml-support</artifactId>
			<version>${mule.version}</version>
			<scope>provided</scope>
		</dependency>
		<!--MUnit Dependencies-->
		
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>com.mulesoft.munit</groupId>
					<artifactId>munit-extensions-maven-plugin</artifactId>
					<version>${munit.extensions.maven.plugin.version}</version>
					<executions>
						<execution>
							<id>munit-extension-test</id>
							<phase>integration-test</phase>
							<goals>
								<goal>test</goal>
							</goals>
						</execution>
					</executions>
					<configuration>
						<dynamicPorts>
							<dynamicPort>a.dynamic.port</dynamicPort>
						</dynamicPorts>
						<environmentVariables>
							<MY_ENV>envVar</MY_ENV>
						</environmentVariables>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-resources-plugin</artifactId>
					<version>${maven.resources.version}</version>
					<executions>
						<execution>
							<id>copy-munit-resources</id>
							<phase>process-test-resources</phase>
							<goals>
								<goal>copy-resources</goal>
							</goals>
							<configuration>
								<outputDirectory>${munit.output.directory}</outputDirectory>
								<resources>
									<resource>
										<directory>${munit.input.directory}</directory>
										<filtering>true</filtering>
									</resource>
								</resources>
							</configuration>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>

		<plugins>
			<plugin>
				<groupId>org.mule.runtime.plugins</groupId>
				<artifactId>mule-extensions-maven-plugin</artifactId>
				<version>${mule.extensions.maven.plugin.version}</version>
				<extensions>true</extensions>
			</plugin>
			<plugin>
				<groupId>com.mulesoft.munit</groupId>
				<artifactId>munit-extensions-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
<repositories>
		<repository>
			<id>MuleRepository</id>
			<name>MuleSoft Master Repository</name>
			<url>https://repository.mulesoft.org/nexus/content/repositories/releases-ee</url>
			<layout>default</layout>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>mulesoft-releases</id>
			<name>mulesoft release repository</name>
			<layout>default</layout>
			<url>https://repository.mulesoft.org/releases/</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>
	<distributionManagement>
		<repository>
			<id>exchange-server</id>
			<name>Anypoint Exchange</name>
			<url>https://maven.anypoint.mulesoft.com/api/v2/organizations/353f142f-2292-45c3-a93b-778a4430b8a9/maven</url>
			<layout>default</layout>
		</repository>
	</distributionManagement>
</project>

 

Settings.xml file config:

XML
 
<server>
		<id>exchange-server</id>
		<username>username</username>
		<password>password</password>
	</server>


To publish your code to Exchange, use this Maven command: mvn deploy.

You can now see the connector in Exchange, pictured below.Screenshot of the connector in Exchange

Now create a Mule application and import the connector from Exchange in your project as below. You can see the custom icon is also applied to your connector. Now run the code and you will see logs in the console.
Screenshot of logs in the console

Connector (mathematics) Icon

Opinions expressed by DZone contributors are their own.

Related

  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Hexagonal Architecture in the Frontend: A Real Case
  • Kafka JDBC Source Connector for Large Data
  • 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!