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 Invoke a Mule Flow From Java

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.

Vishnu Ramakrishnan user avatar by
Vishnu Ramakrishnan
·
Dec. 18, 17 · Tutorial
Like (4)
Save
Tweet
Share
25.92K Views

Join the DZone community and get the full member experience.

Join For Free

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:beans>
<spring:bean id="AddingTwoNames" name="AddingTwoNames"
class="com.practice.sample.AddingTwoNames">
</spring:bean>
</spring:beans>

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;

 @Override
 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);

  nameBuilder.append(firstName);
  nameBuilder.append(lastName);
  name = nameBuilder.toString();

  MuleMessage muleMessage = new DefaultMuleMessage(name, eventContext.getMuleContext());
  muleMessage.setEncoding("UTF-8");

  /*
   * 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);
  muleMessage.setPayload(name);

  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
Flow (web browser) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why You Should Automate Code Reviews
  • 3 Ways That You Can Operate Record Beyond DTO [Video]
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • How To Get Page Source in Selenium Using Python

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: