Over a million developers have joined DZone.

How to Invoke a Mule Flow From Java

DZone 's Guide to

How to Invoke a Mule Flow From Java

In this Mule tutorial, you can learn the steps required to invoke a Mule flow from Java in a common Mule configuration file.

· Integration Zone ·
Free Resource

In this article, we will learn how to invoke Java components from a Mule flow, along with calling a Mule flow from Java component while following the standards of centralizing connector and bean definition in a common Mule configuration file.

In this example, we are going to get first and last name from an HTTP endpoint as request params, set them as flow variables, and pass them on to a Java component. In the Java layer, we are going to append the names and return the payload by invoking another Mule flow.

Step 1: Create a Mule configuration file and name it "global XML." Here is where we will centralize defining HTTP connector and spring bean definition, so it can be used in multiple Mule flows if required. This helps with the reusability principle.

<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" basePath="name" doc:name="HTTP Listener Configuration"/>

<spring:bean id="AddingTwoNames" name="AddingTwoNames"

Image title

Step 2: We will create our primary Mule flow to receive first and last name from HTTP endpoint, set the request params as flow variables and pass it on to a Java component.

Image title

Log the incoming request parameters:

Image title

Set the incoming request parameter as a flow variable:

Image title

Invoke the Java component. Select the Bean name from the drop-down. This is the bean that we defined in global XML file.

Image title

Step 3: Here we have created a Java class that implements the Callable interface.

Extract from MuleSoft site: With the Callable interface, your flow can accept multiple types of incoming data with a single method named onCall. If you implement a callable interface on your component, Mule always invokes the onCall method on the component no matter how many other methods a present.

In this class, we are getting Mule message from Mule event context. From the Mule message, we are getting the first and last name that we had set as flow variables in the parent Mule flow. Then we are appending first and last name, producing a combined name and setting it as the payload as well as a flow variable.

We then invoke another method, invokeMuleFlow, by passing (1) Mule message (2) Mule Context (3) Mule flow name to be invoked.

In the invokeMuleFlow method, we get the second mule flow reference from MuleContext. We then use MuleEvent implementation – DefaultMuleEvent, by passing a Mule message, Exchange Pattern and flow reference to transfer the control to Second Mule Flow.

package com.practice.sample;

import org.mule.DefaultMuleEvent;
import org.mule.DefaultMuleMessage;
import org.mule.MessageExchangePattern;
import org.mule.api.MuleContext;
import org.mule.api.MuleEvent;
import org.mule.api.MuleEventContext;
import org.mule.api.MuleMessage;
import org.mule.api.lifecycle.Callable;
import org.mule.api.transport.PropertyScope;
import org.mule.construct.Flow;

 * http://localhost:8081/name?firstName=Mule&lastName=soft

public class AddingTwoNames implements Callable {

 StringBuilder nameBuilder = new StringBuilder();
 String name = null;

 public Object onCall(MuleEventContext eventContext) throws Exception {

  MuleMessage message = eventContext.getMessage();
  String firstName = message.getProperty("firstName", PropertyScope.INVOCATION);
  String lastName = message.getProperty("lastName", PropertyScope.INVOCATION);

  name = nameBuilder.toString();

  MuleMessage muleMessage = new DefaultMuleMessage(name, eventContext.getMuleContext());

   * invocation scoped property is simply a flow variable as it can only be accessed within a flow. 
   * You won’t be able to access when the message traverses to another flow as a result of an outbound endpoint.

  muleMessage.setProperty("name", name, PropertyScope.INVOCATION);

  invokeMuleFlow(muleMessage, eventContext.getMuleContext(), "second-flow");

  return name;


 public static MuleEvent invokeMuleFlow(MuleMessage muleMessage, MuleContext muleContext,
  String flowName) throws Exception {

  Flow flow = (Flow) muleContext.getRegistry().lookupFlowConstruct(flowName);
  MuleEvent muleEvent = new DefaultMuleEvent(muleMessage,
   MessageExchangePattern.REQUEST_RESPONSE, flow);

  return flow.process(muleEvent);


Step 4: This is the second flow, which we invoked from the Java component.

Image title

Here we are logging both the Payload and flow variable that we had set in the Java component.

Image title

Step 5: Testing

Image title

Image title

Step 6: Console Output

INFO  [[mule-java].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: firstName: Mule; lastName: soft
INFO  [[mule-java].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Output from Java Component: Payload: Mulesoft; Flow Variable: Mulesoft
mule esb ,java ,j2ee ,spring ,mule ,integration

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}