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

How to Trigger a Mule Flow From a Java App

DZone's Guide to

How to Trigger a Mule Flow From a Java App

This method allows you to meet your requirement to initialize a Mule flow for an enterprise or web application in a very simple and easy way.

· Integration Zone ·
Free Resource

The Future of Enterprise Integration: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

Initiating an integration flow as part of an enterprise or web application is an important and basic requirement. Luckily, triggering a Mule flow is easy.

Here, I'm going to show you how to start the Mule flow from your Java application using Mule JARs where you don't need a Mule server.

Regardless of your application server, you can trigger a Mule application in a way similar to executing your Java file in an application.

Below is the code snippet for triggering a Mule flow from a Java file.

muleContext = muleContextFactory.createMuleContext(configBuilder);
muleContext.start();

Below is the sample Mule flow which I would like to run from the Java class.

Mule flow:

< 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: spring = "http://www.springframework.org/schema/beans"
xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation = "http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsdhttp://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd" >
 < http: listener - config name = "HTTP_Listener_Configuration"
host = "localhost"
port = "6061"
doc: name = "HTTP Listener Configuration" / >
 < flow name = "endpointFlow" >
 < http: listener config - ref = "HTTP_Listener_Configuration"
path = "/*"
doc: name = "HTTP" / >
 < set - payload value = "test"
doc: name = "Set Payload" / >
 < logger level = "INFO"
doc: name = "Logger" / >
 < /flow> < /mule>

Here, the Mule flow listens to the HTTP request with port 6061 and outputs the message "test" in the browser.

  • Input: http://localhost:6061 .

  • Output: Test.

Next, write the Java class to trigger the Mule flow. These are the Mule internal classes required for it:

org.mule.api.MuleContext
org.mule.config.spring.SpringXmlConfigurationBuilder
org.mule.context.DefaultMuleContextFactory

The result is:

 SpringXmlConfigurationBuilder configBuilder;
             MuleContext muleContext = null;
             try {
                     configBuilder = new SpringXmlConfigurationBuilder("httptest.xml");
                     muleContext = muleContextFactory.createMuleContext(configBuilder);
                     muleContext.start();

                 } catch (MuleException e) {
                        // TODO Auto-generated catch block
                         e.printStackTrace();
                 }

MuleContextFactory creates the Mule context where you need to pass the Mule XML file name as an argument.

MuleContext.start() method will trigger the flow.

Now you're done!

If you access the URL http://localhost:6061, you will see the output "test."

This method allows you to meet your requirement to initialize a Mule flow for an enterprise or web application in a very simple and easy way.

I have used this approach to trigger the Mule flow in the windows service.

Note: Do not forget to add Mule-related JARs in the lib folders of your application.

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 ,java ,integration ,tutorial

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}