Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Running Mule CE Embedded in Spring Boot

DZone's Guide to

Running Mule CE Embedded in Spring Boot

In this post, a Mule developer gives a quick tutorial on how to integrate Spring Boot with into your Mule project using the open source Mule CE tool.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

Mule CE is an open source integration tool. Mule CE applications are normally run inside a Mule runtime. With mule-spring-boot-starter, you can run Mule CE embedded in a Spring Boot application. This allows Mule developers to quickly prototype and/or deploy Mule applications without having to download Mule runtime, create a Maven artifact, and push the artifact to the Mule runtime. This project will allow developers to build and run the Mule application in much the same manner as other Spring Boot applications.

Add Maven Dependency:

<dependency>
    <groupId>net.taptech</groupId>
    <artifactId>mule-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

Add Repositories:

<repositories>
    <repository>
        <id>Central</id>
        <name>Central</name>
        <url>http://repo1.maven.org/maven2/</url>
        <layout>default</layout>
    </repository>
    <repository>
        <id>mulesoft-releases</id>
        <name>MuleSoft Repository</name>
        <url>http://repository.mulesoft.org/releases/</url>
        <layout>default</layout>
    </repository>
    <repository>
        <id>mulesoft-snapshots</id>
        <name>MuleSoft Snapshot Repository</name>
        <url>http://repository.mulesoft.org/snapshots/</url>
        <layout>default</layout>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones Repository</name>
        <url>http://repo.spring.io/milestone</url>
        <layout>default</layout>
    </repository>
    <repository>
        <id>spock-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

Add Mule Modules and Dependencies as Needed:

In the demo application, the HTTP connector is used so that a jar file is needed for the XML schema and corresponding Java code to support the HTTP connector.

<dependency>
    <groupId>org.mule.transports</groupId>
    <artifactId>mule-transport-http</artifactId>
    <version>${mule.version}</version>
</dependency>

Create a Mule Config File:

Make sure this file is in the artifact classpath. Create an application property called
 mule.config.files. Add a comma separated list of mule config files.

mule.config.files=mule-config.xml

Here's an example mule-config.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />
    <flow name="mule-demo-filesFlow">
        <logger level="INFO" doc:name="Logger" category="net.taptech" message="It works!!!!" />
        <set-payload value="testing" doc:name="Set Payload" />
    </flow>
    <flow name="mule-demo-filesFlow1">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP" />
        <flow-ref name="mule-demo-filesFlow" doc:name="mule-demo-filesFlow" />
    </flow>
</mule>

Add Annotation to Your Spring Boot Application Entry Point:

@EnableMuleConfiguration

@EnableMuleConfiguration
@SpringBootApplication
public class DemoMuleSpringBootApplication {

 private static final Logger logger = LoggerFactory.getLogger(DemoMuleSpringBootApplication.class);

 @Autowired
 private ApplicationContext context;

 public static void main(String...args) {
  logger.info("Starting SpringApplication...");
  SpringApplication app = new SpringApplication(DemoMuleSpringBootApplication.class);
  app.setBannerMode(Banner.Mode.CONSOLE);
  app.setWebEnvironment(false);
  app.run();
  logger.info("SpringApplication has started...");
 }
}

You can now run your application:

./mvnw clean package -Dmaven.test.skip=false && java -jar target/demo-mule-spring-boot-0.0.1-SNAPSHOT.jar

The demo project can be viewed on my GitHub

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

Topics:
mule ,integration ,spring boot

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}