2
votes

I am building a load balancer for http requests using apache camel 2.12.2. For this I am using camel-servlet, camel-http components and my route definition is like:

<from uri="servlet:///my/path?matchOnUriPrefix=true" />
<loadBalance inheritErrorHandler="false">
    <failover roundRobin="true" maximumFailoverAttempts="2">
        <exception>java.io.IOException</exception>
    </failover>
    <to uri="http://server1:9090?bridgeEndpoint=true&amp;throwExceptionOnFailure=false" />
    <to uri="http://server2:9090?bridgeEndpoint=true&amp;throwExceptionOnFailure=false" />
    <to uri="http://server3:9090?bridgeEndpoint=true&amp;throwExceptionOnFailure=false" />
</loadBalance>

Now I am trying to implement caching for http requests using Memcached. For this, I am trying to use RoutePolicy. Is it possible to break the exchange and return back from onExchangeBegin when there is a cache hit, without hitting http end points? Or is there any better way to implement caching for http requests using Memcached?

I tried camel-cache component, but we cannot use EHCache as we already have Memcached in our project.

1
Are your clients accessing the servlet directly or through a reverse proxy? - Ralf
It is possible both the ways. - Joewyn
If you could enforce access through the reverse proxy, then that is probably the better place to implement caching of HTTP responses. I would think that you get better performance as you will not even go to the Java layer if you have a cache hit. - Ralf

1 Answers

5
votes

You can mark the exchange to stop, by setting this property

exchange.setProperty(Exchange.ROUTE_STOP, true);

(this is how < stop /> works if you use that in a route)

... then the routing engine will not continue routing the exchange, and the consumer will be able to use the exchange as the response asap.