When it comes to integrating your applications, message routing is where the rubber meets the road. You’ve already identified your applications, you’ve selected Mule as your integration platform, and you know which Java classes and web services you want to use in your Mule services to process the messages. So how do you wire everything together so that the messages flow through the services correctly, ensuring that you get the output you need?

Mule provides powerful and flexible options for routing messages among the services in your Mule application. This article describes common messaging styles and several of the specific routers you can use.

Following is a description of the key concepts behind message routers:

Endpoints define the channel through which messages are sent and received. For example, a purchasing component may receive an order request over HTTP. Once the order has been processed by the component, a JMS message may be sent over a topic to notify an auditing system, and a response can be sent back over HTTP. Endpoints allow you to listen for JMS messages, send email, invoke web services, and more.

Inbound routers control how a service handles incoming messages, such as selectively consuming only those messages that meet specific criteria or grouping messages together that share a group ID before forwarding them on to a service for processing.

Outbound routers control how a message is dispatched after the service has processed it, such as sending it to a list of recipients or splitting up the message and sending the parts to different endpoints.

Asynchronous reply routers are used in request/response scenarios where a request is made, which triggers one or more further requests and the results of the requests need to be consolidated before a response is given. The classic example of this is where a request is made and tasks are executed in parallel. Each task must finish executing and the results processed before a response can be sent back.

Catch-all strategies are invoked if no routing path can be found for the current message. An inbound or outbound endpoint can be associated with a catch-all strategy so that any orphaned messages can be caught and routed to a common location.

Filters provide the logic used to invoke a particular router. Filters can be combined using the logic filters AndFilter, OrFilter, and NotFilter. Not all routers need to use filters, but all routers support them.

Selecting a Messaging Style

When wiring your Mule services together, new users sometimes get confused about when to use an outbound router and when it's sufficient to simply get a reply. Following is a description of the messaging styles you can use in Mule. To see a table of which messaging styles are supported by each transport, see Transports Feature Matrix in the Mule User Guide (login is required, but registration is free and only takes a moment).

Asynchronous

If you just want to "fire and forget" a message to a service, (no response to the caller is required), you can use the asynchronous messaging style. If synchronous is set to false on the inbound endpoint, the no response is returned to the caller.

Request-Response

In simple scenarios that require a response, a service receives a request on a synchronous inbound endpoint, processes the request, and then sends it back to the caller as a reply. For example, if a user enters a value in an HTML form, and you want to transform that value and display the results in the same page, you can simply configure a synchronous inbound endpoint on the service that does the transformation. This scenario does not use an outbound router. This is the request-response messaging style.

Synchronous

If you need to pass the message to a second service for additional processing, you would configure an outbound router on the first service to pass the message to the second service. After the second service processes the message, the first service sends it back to the caller as a reply. Note that setting the synchronous inbound endpoint on the first service means that the message is treated synchronously throughout all subsequent services, so you do not need to set the synchronous flag on the second service. This is the synchronous messaging style.

For example:

<model name="Synchronous_Message_Pattern">
<service name="SynchronousService">
<inbound>
<!-- Set synchronous to "true" to specify that you want to return a response -->
<jms:inbound-endpoint queue="test.in" synchronous="true"/>
</inbound>
<component class="org.myorg.WidgetHandler"/>
<outbound>
<!-- When using the pass-through router, you must also set synchronous to "true" on the outbound endpoint if you want to return a response -->
<pass-through-router>
<!-- Set the outbound endpoint -->
<jms:outbound-endpoint queue="test.out" synchronous="true"/>
</pass-through-router>
</outbound>
</service>
<!-- Configure the second service, and set its inbound endpoint to the same path as the outbound endpoint of the previous service. Note that you do not have to set the synchronous flag, as the message has already been set to synchronous by the first service. -->
<service>
<inbound>
<jms:inbound-endpoint queue="test.out"/>
</inbound>
<component class="org.myorg.WidgetProcesser"/>
</service>
</model>

Note: In previous versions of Mule, you had to set the remoteSync flag to true to configure synchronous processing in remote services. As of Mule 2.2, this flag has been removed, and you only set the synchronous flag to "true" to create a synchronous flow.

Asynchronous Request-Response

In the most complex scenario, you can enable request-response messaging and allow the back-end process to be forked to invoke other services, returning a reply asynchronously based on the results of multiple service invocations. You can set the inbound endpoint's synchronous flag to false, since the response will be handled by the asynchronous reply router, unless you also want to send a response to the caller. This is the asynchronous request-response messaging style.

In the following example, a request comes in on an HTTP endpoint, is broadcast to two endpoints using the Multicast router, and the results are sent asynchronously to a JMS endpoint.

For complete information on the messaging styles you can use, see Mule Messaging Styles in the Mule User Guide.

Now that you understand which messaging styles to use for routing in different scenarios, let’s look at several routers you can use for achieving finer control over message routing. For a complete list of message routers you can use, see Using Message Routers in the Mule User Guide.

Passing Messages to Another Endpoint

The pass-through router is designed to simply pass messages from one endpoint to another. This is useful for dispatching messages to a queue, for example.

You can also use the pass-through router to perform protocol bridging to another outbound endpoint. For example:

When using the pass-through router, if you want to return a response, you must set synchronous to "true" on the outbound endpoint. Other routers such as the chaining router do not require that you set synchronous to true on the outbound endpoint and always return a response in synchronous scenarios. Therefore, if you are sending a message through several services, you might want to use the chaining router instead of the pass-through router so that you do not have to remember to set synchronous to true on each endpoint.

Filtering Messages

You can control which messages a service handles by using filters. The Selective Consumer Router works on inbound endpoints to control which messages that service will process. The Filtering Router works on outbound endpoints to control which messages the service sends along to the next endpoint. You can use a combination of these approaches to control the message flow.

For example, if you only want to process messages that don't have errors, you can use a selective consumer to ensure that only those with the result code "success" are processed. You can then use a Catch-all Strategy to forward all other messages to another endpoint for error handling:

If you want the service to process all messages but then want to specify criteria to determine where the message is sent next, you can use filtering outbound routers. In the following example, messages that contain an exception are sent to the system administrator's email address, messages whose payload contains a specific string are sent to the string queue, and all other messages are picked up by the forwarding catch-all router and sent to an error queue:

Similar routers are the forwarding router, which allows you to process some messages and selectively forward others, and the wiretap router, which allows you to process all messages and send them on as normal but also send a copy to another endpoint. For more information, see Inbound Routers in the Mule User's Guide.

Chaining Outbound Endpoints Together

Let's assume we have a validation service, and if the message fails validation, the message and its exception are forwarded to another service AND the message and its exception are returned to the caller. You could achieve this using the chaining router, which is a fast and lightweight configuration for sending a message to an endpoint and then sending the result of that endpoint to another endpoint. For example:

<chaining-router>
<!-- First, send the message to this endpoint for validation -->
<vm:outbound-endpoint path="ValidationService" synchronous="true"/>
<!-- Then send the message to this endpoint if it contains an exception -->
<vm:outbound-endpoint path="ValidationError" synchronous="true">
<exception-type-filter expectedType="java.lang.Exception"/>
</vm:outbound-endpoint>
</chaining-router>

Splitting Messages

A message splitter can be used to break down an outgoing message into parts and dispatch those parts over different endpoints configured on the router. For example, in an order-processing application, you might want to send different parts of the message to different services for processing. You could do this using one of the following routers:

List Message Splitter: accepts a list of objects that will be routed to different endpoints. For example:

You could also split a message into parts to improve performance. The Round Robin Message Splitter splits the message into parts and sends them to endpoints in a round-robin approach. The Message Chunking Router splits a single message into a number of fixed-length messages that will all be routed to the same endpoint.

After splitting messages, you use the Message Chunking Aggregator to aggregate the message parts back together again. The aggregator uses the correlation ID, which is set by the outbound router, to identify which parts belong to the same message.

Processing a Message Only Once

The Idempotent Receiver ensures that only unique messages are received by a service by checking the unique message ID of the incoming message. The ID can be generated from the message using an expression defined in the idExpression attribute. By default, the expression used is #[message:id], which means the underlying endpoint must support unique message IDs for this to work. In the following example, a unique ID is a combination of the message ID and the contents of the label field in the header, and the IDs are then written to a simple text file to keep track of which messages have already been processed:

Calling an External Service through Component Bindings

In addition to using messaging routers to control how messages flow between services, you can also have your component call out to an external service as it is processing the message.

In this approach, you bind Mule endpoints to Java interface methods. The advantage of this approach is that you can use an external service while the component is still processing the message, but you do not have to use Mule APIs or modify your component class. Instead, you configure a component binding in the configuration XML file to specify the endpoint of the external service. For example, the following binding causes the sayHello method in HelloInterface to call out to the external HelloWeb service when sayHello is called:

Summary

Mule provides you with many approaches for controlling how messages are exchanged in your application. This article has provided an overview of message routing in Mule, the messaging styles it supports, some of the more commonly used routers, and component bindings. For complete information on message routing, see the Mule User Guide.

"I have an external REST webservice, which takes three headers Content-Type, Content-Length and Authorization."

Now I would like to understand1. How to configure the external REST webservice in Mule.2. How to get the Header's information. so that I can use those headers for an another purpose. that means I should be able to access those headers in a Custom class.3. If we call Mule ESB's url, which class will be having all the request information.

Is your profile up-to-date? Please take a moment to review and update.

Email Address

Note: If updating/changing your email, a validation request will be sent

Company name:

Keep current company name

Update Company name to:

Company role:

Keep current company role

Update company role to:

Company size:

Keep current company Size

Update company size to:

Country/Zone:

Keep current country/zone

Update country/zone to:

State/Province/Region:

Keep current state/province/region

Update state/province/region to:

Subscribe to our newsletter?

Subscribe to our architect newsletter?

Subscribe to our industry email notices?

You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.