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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Techniques You Should Know as a Kafka Streams Developer
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency

Trending

  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  • Architecting an Embedded Efficiency Layer: A Platform Deep Dive into Day-Two Operational Tuning
  • Building Enterprise-Grade Real-Time IoT Dashboards with Vue 3, MQTT, and Kafka
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  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.8K 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
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook