How to Extend Mule 4 Runtime using SDK

Mule 4 is packed with enhancements and new features. It comes with a new Mule SDK to extend the Mule Runtime for creating custom Connectors and Modules. Even though the goal of SDK is similar to DevKit used in Mule 3, it differs in how custom Connectors and Modules are created using new Mule SDK. In this post, we will see how to create a custom Module using Mule SDK and use it in our project.

1. What is Mule 4 SDK?

Mule 4 comes with a whole new Mule SDK, which is a tool to extend and enhance the functionalities of Mule Runtime. It allows creating custom Modules and Connectors. These Modules and Connectors then can be used in Mule Applications.

2. Prepare for Customization

2.1 Prerequisites

You must have these components installed -

  • Java Development Kit 8 (JDK 8)

  • Apache Maven 3.3.9 or higher

Mule Target Versions:

  • Mule ESB 4

  • Anypoint Studio 7

2.2 Configuring Maven

The Maven Plugin and Maven Archetype to create Modules is available in MuleSoft Public repository. In Maven settings.xml, add this to pluginRepositories section -

Listing 2.2.A Maven Settings.xml Update
      <name>MuleSoft Repository</name>

3. Creating our First Module

New Module can be created by using Maven Plugin or Maven Archetype.

3.1 Using Maven Plugin

Maven Plugin mule-extensions-archetype-maven-plugin has all the steps with help descriptions. Run below on command line -

mvn org.mule.extensions:mule-extensions-archetype-maven-plugin:generate

It asks few question before it generates the module project. Here is a sample of that -

* Enter the name of the extension (empty for default):
* Enter the extension's groupId (empty for default):
* Enter the extension's artifactId (empty for default):
* Enter the extension's version (empty for default):
* Enter the extension's main package (empty for default):

3.2 Using Maven Archetype

Run below command on console to use Maven Archetype to generate the module.

mvn archetype:generate
  -DarchetypeCatalog=    (1)
  -DextensionName=SayHi (2) (3)
1 This archetype is available in Mule Maven Repository.
2 Extension name is not camel cased for generated class names. See MULE-14600.
3 extensionNameNoSpaces is computed based on extensionName. It is not required to specify but at the time of writing this post, this command was resulting in an unexpected warning message related to extensionNameNoSpaces. You can ignore that for now. I have filed a Jira issue MULE-14599 for this.

4. Understanding Structure

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── javastreets
    │   │           └── mule
    │   │               └── extension
    │   │                   └── sayhi
    │   │                       └── internal
    │   │                           ├── (1)
    │   │                           ├── (2)
    │   │                           ├── (3)
    │   │                           ├── (4)
    │   │                           └── (5)
    │   └── resources
    └── test
        ├── java
        │   └── com
        │       └── javastreets
        │           └── mule
        │               └── extension
        │                   └── sayhi
        │                       └── (6)
        └── resources
            └── test-mule-config.xml (7)

4.1 Extension Class

SayHiExtension is the Main entry point class and is marked with @Extension annotation. It defines the Connector attributes such as its name, XML prefix and namespace, category and other Connector attributes.

@Xml(prefix = "sayhi")
@Extension(name = "SayHi")
public class SayHiExtension {


It is possible to have more than one configuration for an extension. They can be specified as an array value - @Configurations({SayHiConfiguration.class, SayHelloConfiguration.class}).

4.2 Configuration Class

SayHiConfiguration is a common configuration provider for our new module. Any common attributes, parameters can be included in this class. This class is added to Extension class using @Configurations annotation.

It is possible to have more than one Configuration for an Extension. In that case, there can be ONLY one Configuration with default name 'config'. All other configurations must a unique name.
Listing 4.2.A Configuration with default Name
@Operations(SayHiOperations.class) (1)
@ConnectionProviders(SayHiConnectionProvider.class) (2)
public class SayHiConfiguration {

  private String configId;

  public String getConfigId(){
    return configId;
Listing 4.2.B Configuration with explicit name
@Operations(SayHelloOperations.class) (1)
@ConnectionProviders(SayHiConnectionProvider.class) (2)
public class SayHelloConfiguration {

  private String configId;

  public String getConfigId(){
    return configId;
1 Configuration class defines the operations available with it.
2 If there is any connection provider for this configuration, then it should be added here.

4.3 Connection Provider

This is implemented in two classes SayHiConnection and SayHiConnectionProvider.

SayHiConnection is a Connection Object Instance managed by Connection Provider. This class should have any methods such as invalidate(), and attributes that makes sense for an instance of a Connection, once established.

Listing 4.3.A Connection Instance Object
public final class SayHiConnection {

  private final String id;

  public SayHiConnection(String id) { = id;

  public String getId() {
    return id;

  public void invalidate() {
    // do something to invalidate this connection!

SayHiConnectionProvider implements PoolingConnectionProvider interface to provide methods such as connect(), disconnect(), and validate() to manage the Connection instance SayHiConnection.

Module can support multiple Connection Providers. They are added on Configuration class using @ConnectionProviders({SayHiConnectionProvider.class,SayHelloConnectionProvider.class}).

4.4 Operations

SayHiOperations is a regular java class, when added on Configurations class using @Operations, becomes the operations provider class. Every public method in this class becomes the operation supported by this module.

Modules may have some operations that do not depend on any Configurations. These are Global Operations.

Below is an example of a public method on SayHiOpererations class -

@MediaType(value = ANY, strict = false)
public String sayHi(String person) {
  return buildHelloMessage(person);

Operations can have three types of outputs -

  • Void: As the name indicates, this operation will not return anything. This operation will not modify the message and next component will receive the same message.

  • Any Object: When operation returns something, then it becomes the payload. For example, public String sayHi(String person) operation returns String which will be the payload to next component.

Message after this operation will not have any attributes.
  • Result Object: Result is a special object that can be returned by an operation. This helps to set any attributes, properties, and payload of the message returned by an operation.

5. Test, Build, and Install

When the module is generated, it also includes a test mule-config.xml file. It implements test flows that use Operations from this module.

Here is how the test-mule-config.xml looks like -

<mule xmlns=""

    <sayhi:config name="config" configId="configId"> (1)
        <sayhi:connection requiredParameter="aValue" /> (3)

    <flow name="sayHiFlow">
        <sayhi:say-hi person="Manik Magar"/> (6)

	<flow name="retrieveInfoFlow">
        <sayhi:retrieve-info config-ref="config"/> (6)


To Run test cases, build and install this new module into local maven repository, just run below maven command -

mvn clean install

Once successfully installed, maven repository will have a json and a jar for our new module.

├── mule-modules-sayhi-0.0.1-SNAPSHOT-extension-model-4.0.0.json
├── mule-modules-sayhi-0.0.1-SNAPSHOT-mule-plugin.jar
└── mule-modules-sayhi-0.0.1-SNAPSHOT.pom

6. Use and Share Module in Project

Now, the new module is built and installed in the local repository. To use this module in Mule 4 project, add below dependency to project pom -

Observe the presence of tag <classifier>mule-plugin</classifier>. This must be specified for Anypoint Studio 7 to recognize new module.

Once the dependency is added to project pom, Module will appear in the Mule Palette.

Adding Mule Custom Module to Anypoint Studio

Then we can add module operations to mule config -

<sayhi:config name="SayHi_Config" doc:name="SayHi Config" configId="MyConfig">
	<sayhi:connection requiredParameter="This is required" />

<flow name="flow-test-zip">
	<sayhi:say-hi doc:name="Say hi" person="Manik Magar"/>
	<sayhi:retrieve-info doc:name="Retrieve info" config-ref="SayHi_Config"/>

To share this module with others, we just need to push this to central maven repository. Then it can be used by just adding a dependency in other projects, without requiring any plugin installations.

7. Differences with DevKit

Mule 3 used Developer Kit (DevKit) to create custom Connectors. DevKit also generated lots of code when creating custom connectors, including the plugin for Anypoint Studio. To use Mule 3 Custom Connector in Studio, plugin had to be installed in the studio.

Mule SDK is the only option to extend Mule 4 Runtime and DevKit is not available in Mule 4.

An Essential difference between Mule SDK and DevKit is, Mule SDK is not a code generator. It is rather a plain extension mechanism.

Mule SDK is more powerful than DevKit. Compared to DevKit, Mule SDK also adds support for features like Transactions, Request-Response message sources, Dynamic configurations, Routers, Non Blocking operations, Classloading isolation, and much more.

8. Conclusion

Mule 4 Runtime is not only powerful but also easy to extend. In this post, we saw how easily we can extend the Runtime and create custom modules. These modules are not only clean but also easy to use and share.

Mule 4 has definitely evolved, simplified and became more powerful.

9. References and further reading

on twitter to get updates on new posts.

Stay updated!

I usually post about Java, Java EE, Integrations, Mule ESB and other things in java ecosystem.

Get updates for new Posts in your email by subscribing to JavaStreets feed here -

Lives on Java Planet, Walks on Java Streets, Read/Writes in Java, JCP member, Java EE enthusiast, MuleSoft ESB Consultant, Open Source Contributor and Supporter, also writes at Unit Testers, A Family man!