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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Jakarta Faces Flow Scope: Managing Multi-Step UX Without Session State
  • Techniques You Should Know as a Kafka Streams Developer
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop

Trending

  • Closing the Drift Loop: An AI Agent That Drafts Terraform Fixes
  • Applying Microservices Lessons to Multi-Agent AI Systems
  • Genkit Flows as senro Steps: Extremely Powerful AI Pipelines in Go
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Mar. 09, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Jakarta Faces Flow Scope: Managing Multi-Step UX Without Session State
  • Techniques You Should Know as a Kafka Streams Developer
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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