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

How to Trigger a Mule Flow From Java

DZone's Guide to

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.

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

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.

Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
mulesoft ,java ,integration ,tutorial ,mule flow

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}