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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency

Trending

  • S3 Vectors: How to Build a RAG Without a Vector Database
  • LLM Integration in Enterprise Applications: A Practical Guide
  • Clean Code: Concurrency Patterns, Context Management, and Goroutine Safety, Part 5
  • [closed] DZone's 2025 Developer Community Survey
  1. DZone
  2. Coding
  3. Java
  4. How to Trigger a Mule Flow From Java

How to Trigger a Mule Flow From Java

Triggering a Mule flow or a sub-flow by sending it a message payload and then using the response payload for your further processing is pretty straightforward.

By 
Harish Kumar user avatar
Harish Kumar
·
Mar. 09, 17 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
10.7K Views

Join the DZone community and get the full member experience.

Join For Free

While working on an integration project in Mule ESB, there are various situations where you would like to trigger a Mule flow or a sub-flow by sending it a message payload and then using the response payload for your further processing.

Doing this is pretty straightforward. Today, I will show you how to do it.

However, before I move forward into all the code and XML involved in doing the flow calls from Java code, let’s first see some scenarios where we would require to do this.

  • Suppose you are creating a SOAP web-service using Mule CXF and now you want to use the Mule message process for XML-to-object or object-to-XML conversion. You create two sub-flows to do this. They work perfectly when you test them using a test flow with HTTP inbound. Now, the problem is how to call them in your service implementation class of the web-service.
  • You have created a super awesome DataWeave conversion for your two different kind of data sets and you don’t want to write the same logic in your some Java class.

How to Do It

To achieve calling a Mule flow in your Java code, the first thing we need to do is make our calling Java class aware of the Mule context. To do this, we will be using Spring's implementation of the aware pattern where if we implement a interface on our class (i.e., MuleContextAware), then Spring will automatically provide the MuleContext object to our class.

  • Don’t forget to create a variable for your MuleContext muleContext. 
  • Don’t forget to generate setter method for your muleContext variable.
public class HelloWorldWSImpl implements MuleContextAware {

 MuleContext muleContext;

 @Override
 public void setMuleContext(MuleContext context) {
  muleContext = context;
 }

}

Once you are done with making your class MuleContext aware, the next step is to use this object and get reference to the flow or sub-flow using the name of flow that you created in mule-config.xml.

@SuppressWarnings("deprecation")
public Object triggerMuleFlow(String flowName, boolean isSubFlow, Object inputData, Object returnClass) {
 try {
  if (!isSubFlow) {
   Flow flow = (Flow) muleContext.getRegistry().lookupFlowConstruct(flowName);
   // Can be implemented in same way as below
  } else {
   MessageProcessor subFlow = muleContext.getRegistry().lookupObject(flowName);
   MuleMessage muleMessage = new DefaultMuleMessage(inputData, muleContext);
   MuleEvent inputEvent = new DefaultMuleEvent(muleMessage, MessageExchangePattern.REQUEST_RESPONSE,
    new DefaultMuleSession());
   MuleEvent result = subFlow.process(inputEvent);
   returnClass = result.getMessage().getPayload();
  }

 } catch (Exception e) {
  e.printStackTrace();
 }
 return returnClass;
}

The code above is self-explanatory. What we are doing here is doing a looking up a flow from the Mule registry and then creating a MuleMessage to be sent to this flow with the payload data that our flow or sub-flow expects.

Once we are done with the call, we get the result of the call by getting the payload out of MuleEvent, generated as a result of flow triggering.

As you can see, it is very simple to trigger a flow or sub-flow from a Java class.

Flow (web browser) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook