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

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

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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 Convert XLS to XLSX in Java

Trending

  • Testing SingleStore's MCP Server
  • Scalability 101: How to Build, Measure, and Improve It
  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  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.

By 
Vishnu Ramakrishnan user avatar
Vishnu Ramakrishnan
·
Dec. 18, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
27.5K 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.

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 Convert XLS to XLSX 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!