Embed Mule 4 Runtime Into a Spring Boot Application
A developer gives a tutorial on creating a Spring Boot application that takes advantage of Mule to establish a consistent and sustainable application.
Join the DZone community and get the full member experience.
Join For FreeIntro to Mule
One of Mule’s main capabilities is that it allows you to forget about point-to-point integrations, which can become a real headache as they grow in complexity.
Mule is a ESB (Enterprise Service Bus) messaging framework, scalable and distributable, that can orchestrate interactions with services and applications using different transport and messaging technologies.
Relying on Mule’s reusability and connectivity allows you to establish a much more consistent and sustainable application network, so that future integrations can reuse previously created components and integrate them into the network in an increasingly dynamic way.
With this in mind, MuleSoft proposes the use of an API-led Connectivity architecture when designing the model of integrations to be managed with Mule ESB.
The Goal
The main goal of this article is to join Mule's productivity with the Spring Boot ecosystem. To achieve this we are going to use Spring Boot Starter for Mule 4, an open-source library licensed under the Apache License 2.0.
This will allow you to build highly productive and easily scalable systems with very interesting features:
- Mule Runtime monitoring with Spring Boot Admin:
- Easy loglevel management.
- Follow and download logfile.
- JVM and memory metrics.
- Interact with JMX-beans.
- Show health status.
- ...
- Manage Mule artifacts (applications/domains), exposing Mule deployment services through Spring Boot REST controllers, as a stand-alone instance.
- Easy deployment of Mule applications as microservices on Kubernetes or another container management platform.
- Deploy Mule artifacts stored in an external repository.
- Improve CI/CD, building Docker images within seconds containing the latest Mule Runtime version and deploying your Mule applications on a container management platform, for example Kubernetes, to test integrations before deploying them to production.
Your Home Cooked "Spring Boot Mule 4 Runtime"
The Ingredients
- JDK 8
- Maven
3.5+
- Spring Boot
2.1.0+
- Mule Runtime
4.2.1
-4.3.0
- A pom.xml file
- A
main
Class
The pom.xml
x
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.hawkore.springframework.boot</groupId>
<artifactId>spring-boot-mule4-runtime-ce</artifactId>
<version>4.2.2</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven.deploy.skip>true</maven.deploy.skip>
<maven.install.skip>true</maven.install.skip>
<!-- Spring boot starter for Mule -->
<mule4-spring-boot-starter.version>2.0.0</mule4-spring-boot-starter.version>
<!-- spring-boot-admin-client to register at spring boot admin server-->
<spring-boot-admin-client.version>2.1.6</spring-boot-admin-client.version>
<!-- Should match with Mule's one to avoid incompatibilities -->
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
<projectReactorVersion>3.2.12.RELEASE</projectReactorVersion>
<projectReactorExtraVersion>3.1.6.RELEASE</projectReactorExtraVersion>
<muleProductExt></muleProductExt>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Optional Spring Boot Web for Mule deployment services and remote management -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- Optional for remote management actuator + client -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>${spring-boot-admin-client.version}</version>
</dependency>
<!-- Spring boot starter for Mule 4 Runtime CE -->
<dependency>
<groupId>org.hawkore.springframework.boot</groupId>
<artifactId>mule4-spring-boot-starter-ce</artifactId>
<version>${mule4-spring-boot-starter.version}</version>
</dependency>
<!-- Required by Mule's APIKit -->
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>${projectReactorVersion}</version>
</dependency>
<dependency>
<groupId>io.projectreactor.addons</groupId>
<artifactId>reactor-extra</artifactId>
<version>${projectReactorExtraVersion}</version>
<exclusions>
<exclusion>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Add here more dependencies: Mule patches, etc... -->
</dependencies>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<id>mulesoft-releases</id>
<name>MuleSoft Releases Repository</name>
<url>https://repository.mulesoft.org/releases/</url>
<layout>default</layout>
</repository>
</repositories>
</project>
The main Class
package org.hawkore.springframework.mule;
import org.hawkore.springframework.boot.mule.config.EnableSpringMuleRuntimeDeploymentServices;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class SpringBootEmbeddedMuleRuntime {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(SpringBootEmbeddedMuleRuntime.class);
app.setBannerMode(Mode.OFF);
app.run(args);
}
}
The Mule 4 Runtime Version
Build it with the Mule 4 Runtime BOM version you want, for example 4.2.2-hf5:
xxxxxxxxxx
mvn clean package -Dmule.bom.version=4.2.2-hf5
Check available Mule Runtime Kernel BOM versions at MuleSoft's public Maven repository.
Easy Peasy...
Just run your Mule applications using your home cooked "Spring Boot Mule 4 Runtime":
x
java -Dmule.base=/opt/mule -Dmule.apps=file:/opt/muleapps/sample-app-1.0.0-mule-application.jar -jar target/spring-boot-mule4-runtime-ce-4.2.2.jar
What if Your Mule Applications Are Located in an External Repository?
Don't worry, you can still load them. Mule applications and Mule domains are loaded using Spring's ResourceLoader, so you can provide a well-known URL format:
Prefix | Example | Explanation |
---|---|---|
classpath: | classpath:com/myapp.jar | Loaded from the classpath. |
file: | file:/opt/shared/myapp.jar | Loaded as a URL, from the filesystem. |
http(s): | http://myserver/myapp.jar | Loaded as a URL. |
xxxxxxxxxx
java -Dmule.base=/opt/mule -Dmule.apps=https://my.nexus.server.com/repositories/internal/org/mule/apps/sample-app-1.0.0-mule-application.jar?access_token=1234 -jar target/spring-boot-mule4-runtime-ce-4.2.2.jar
See more examples at GitHub.
Opinions expressed by DZone contributors are their own.
Comments