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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Spring Transaction Propagation in a Nutshell
  • How to Merge HTML Documents in Java

Trending

  • Debugging With Confidence in the Age of Observability-First Systems
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Spring and PersistenceContextType.EXTENDED
  • Contextual AI Integration for Agile Product Teams
  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.6K 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
  • Step-by-Step Guide to Use Anypoint MQ: Part 1
  • Spring Transaction Propagation in a Nutshell
  • How to Merge HTML Documents in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!