Upcoming Features: RSS in Mule

April 29th, 2010

Those of you who are keeping a close eye on expected changes for Mule 3 will certainly have noticed that a number of new and improved features will make their debut soon.
Read the rest of this entry »

If you enjoyed this post, make sure you subscribe to my RSS feed!

Improvements and Fixes

March 29th, 2010

One of the advantages of Mule is that it is open source. As we all know, this means that we can poke around and tweak the source code to our needs. While this is certainly useful, it is far better to make sure that the main code base is changed.
Read the rest of this entry »

If you enjoyed this post, make sure you subscribe to my RSS feed!

A Merry Little Stream

March 15th, 2010

Mule’s file connector allows us to read and write files to and from the underlying file system. Conveniently, we can also use stream large files rather than reading the whole lot in but what is the difference in behaviour in each case?
Read the rest of this entry »

If you enjoyed this post, make sure you subscribe to my RSS feed!

Component Clones

March 11th, 2010

When you run Mule, your services (and their components) are initialised and ready to process inbound messages. Mule will create a new object instance for each request but this may not be what you want to happen.
Read the rest of this entry »

If you enjoyed this post, make sure you subscribe to my RSS feed!

Inheriting Models

March 8th, 2010

I wrote about how you can have multiple models in a Mule application and how this is a convenient way to group services based on, say, exception handling. What if the models are in different configuration files?
Read the rest of this entry »

If you enjoyed this post, make sure you subscribe to my RSS feed!

Mule Model: What Is It Good For?

March 4th, 2010

In the good old days of Mule 1.x, the Mule had a specific purpose related to threading or, sometimes, streaming of data. People tended to stick to the default model more often than not. This changed in Mule 2.x
Read the rest of this entry »

If you enjoyed this post, make sure you subscribe to my RSS feed!

Cryptic Error #1

March 1st, 2010

I work with Mule on a regular basis and am used to the error messages that may sometimes seem cryptic to the untrained eye. The truth is that they are not cryptic at all, if you know what to look for.
Read the rest of this entry »

If you enjoyed this post, make sure you subscribe to my RSS feed!

Service Statistics

February 25th, 2010

Continuing on the theme of a ServiceAware service, what if you wanted to just be able to extract certain statistics from your Mule services at run time?
Read the rest of this entry »

If you enjoyed this post, make sure you subscribe to my RSS feed!

Reacting to Your Environment

February 15th, 2010

Back after a brief hiatus, I thought I’d build upon one of my previous articles related to Mule-aware services.
Read the rest of this entry »

If you enjoyed this post, make sure you subscribe to my RSS feed!

Short Circuiting The Response Flow

December 14th, 2009

Today’s guest post is from Stephen Fenech, Consultant at Ricston who talks about his most recent Mule engagement.

A couple of weeks ago I was at SwissCom, the ‘leading Swiss provider of innovative communications and IT solutions’. They are currently using Mule in one of their major projects and we came across an interesting scenario. For this post I have watered down the scenario slightly; to not distract you from the main details while still leaving in all the relevant items.

We had a synchronous request from an external client, which was forwarded in parallel to multiple services. The results from these services were aggregated and then sent back as the response to the client.

<service name="MyService">
	<inbound>
		<inbound-endpoint ref="InboundEndpoint" 
			synchronous="true" />
	</inbound>
	<component  class="my.Component" />
	<outbound>
		<multicasting-router>
			<outbound-endpoint ref="Out1">
			<outbound-endpoint ref="Out2">
			<reply-to ref="Reply" />
			<payload-type-filter 
				expectedType="my.Request"/>
		</multicasting-router>
	</outbound>
	<async-reply>
		<inbound-endpoint ref="Reply" />
		<custom-async-reply-router class="my.Aggregator"/>
	</async-reply>
<service>

This is quite a common pattern, however since the requests to obtain this information (the ones to Service1 and Service2) were quite heavy, the results were being cached. The Main service would check if there is a cached result and, if so, will return this result. The way this was solved was to have two routers with filters. If the MainService produces a request, the normal flow will be used but if a result is found in the cache then this will be sent directly to the Asynchronous Reply Aggregator.

<service name="MyService">
	<inbound>
		<inbound-endpoint ref="InboundEndpoint" 
			synchronous="true" />
	</inbound>
	<component  class="my.Component" />
	<outbound>
		<multicasting-router>
			<outbound-endpoint ref="Out1">
			<outbound-endpoint ref="Out2">
			<reply-to ref="Reply" />
			<payload-type-filter 
				expectedType="my.Request"/>
		</multicasting-router>
		<multicasting-router>
			<outbound-endpoint ref="Reply">
			<payload-type-filter 
				expectedType="my.Result"/>
		</multicasting-router>
	</outbound>
	<async-reply>
		<inbound-endpoint ref="Reply" />
		<custom-async-reply-router class="my.Aggregator"/>
	</async-reply>
<service>

This worked, however, it was slightly inefficient since we use an extra thread to dispatch the message over VM, only to be aggregated by the original thread. We wanted to push Mule to the limit so even minor improvements would make the server handle a larger load.

So, we asked ourselves, what if we could short circuit the flow so that a cached result is sent directly to the client without passing this result over VM and then blocking & waiting for the same result. How can we do this short-circuiting in the most painless way possible?

The secret is in the getResponse method of the aggregator. By default we simply use the method of the parent, which makes use of the response aggregator to block waiting for the responses after which the custom aggregation is called and this method will return the aggregated message. In our case, we want to return the response immediately in certain situation. The getResponse method gets a Mule Message as a parameter. Typically, this message is the one sent out by the outbound router and is used in order to get the info to correlate on. In the case where no outbound router accepts the message returned by the service component this message is passed on to the getResponse method. So all we have to do is filter the cached message on the outbound side so that nothing is sent out. Then in the getResponse method, when we return the cached message or call the normal parent method.

In order to make things a bit more generic, a filter was used to decide if the message is a response or not, making the router configurable. Another advantage of this is that by looking at the configuration, you can tell that there is something different about this aggregation router thus making the config more explicit.

public class CustomAggregator extends ResponseCorrelationAggregator {
 
	// This filter is used to check if the result should be sent 
	// back immediately rather than wait for the aggregation.
	private Filter shortCircuitingFilter;
 
    @Override
    public MuleMessage getResponse(MuleMessage message) throws
		RoutingException
    {
     if(shortCircuitingFilter!=null&&shortCircuitingFilter.
		accept(message))
        {
        	logger.debug("Short-Circuiting flow.");
        	return message;
        }else
        {
        	return super.getResponse(message);
        }
    }

The configuration is as follows:

<service name="MyService">
	<inbound>
		<inbound-endpoint ref="InboundEndpoint" 
			synchronous="true" />
	</inbound>
	<component  class="my.Component" />
	<outbound>
		<multicasting-router>
			<outbound-endpoint ref="Out1">
			<outbound-endpoint ref="Out2">
			<outbound-endpoint ref="Out3">
			<reply-to ref="Reply" />
			<payload-type-filter 
				expectedType="my.Request"/>
		</multicasting-router>
	</outbound>
	<async-reply>
		<inbound-endpoint ref="Reply" />
		<custom-async-reply-router class="my.Aggregator">
			<spring:property name="shortCircuitingFilter" 
				ref="ShortCircuitingFilter"/>
		</custom-async-reply-router>
	</async-reply>
<service>

With this simple 10 line tweak we managed to improve the flow, reducing the complexity of the scenario and making the configuration more elegant.

If you enjoyed this post, make sure you subscribe to my RSS feed!

© Copyright 2005-2008 Ricston, All Rights Reserved
 Sitemap   Privacy Policy    Legal