RC
Technology

Trigger Mule flow from JAVA

While working on the 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 use the response payload for your further… Continue Reading →

Harish Kumar

While working on the 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 use the response payload for your further processing. Doing this is pretty straight forward and I will be showing you today how to do it.

But, before I move forward into all the code and xml’s 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 mule message process for XML to Object or Object to XML conversion, you create two sub-flows to do this and 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 web-service
  • You have created a super awesome data-weave conversion for your two different kind of data-set’s and you don’t want to write the same logic in your some java class, This could be another example of calling mule flow in java class.

How to do it

To achieve calling mule flow in your java code, first this we need to do is make our calling java class aware of Mule Context and to do this we will using springs implementation of aware pattern, where if we implement a interface on our class i.e. implements 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, next step is to use this object and get reference to the flow or sub-flow using the name of flow that you have created in you mule-config.xml files.

@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 lookup of flow from the 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 call by getting the payload out of MuleEvent generated as a result of flow triggering.

As you saw it is very simple to trigger any flow or sub-flow from java class.

 

Related reading