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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Jakarta Faces Flow Scope: Managing Multi-Step UX Without Session State
  • Techniques You Should Know as a Kafka Streams Developer
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Trending

  • RAG Architecture Patterns: Proxy, Caching, and Routing
  • Microsoft Fabric Throttling: Diagnose Before You Scale
  • Designing Frontend Applications for Failure, Not Just Success
  • AI Agents Need an Identity Plane: Rethinking Enterprise Authorization for Autonomous Software

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Dec. 18, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
28.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Jakarta Faces Flow Scope: Managing Multi-Step UX Without Session State
  • Techniques You Should Know as a Kafka Streams Developer
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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