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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Harish Kumar user avatar by
Harish Kumar
·
Mar. 09, 17 · Tutorial
Like (7)
Save
Tweet
Share
9.93K 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.

Popular on DZone

  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • How To Get Page Source in Selenium Using Python
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: