Search code examples
javaapache-camelrestletspring-camel

Camel Restlet maxThreads not working as expected


I am working on an application where a lot of camel routes are exposed as restlet routes. Lets call them endpoints. These endpoints are consumed by an angular application. These endpoints calls to a 3rd party system to gather the data and then after processing them, it passes the response to the angular application.

There are times when the 3rd party system is very slow, and in such cases our server's (Websphere 8.5.5.9) thread pool reaches at its maximum size (because most of them are waiting to get a response from 3rd party). Due to this there are no threads available for other parts (which does not interact with server via these endpoints) of application and hence they also suffers due to this.

So basically we want to limit the number of requests to be served by these 'endpoints' if the server is considerably overloaded so that other parts of application won't get affected. So we wanted to play around the number of threads which can process the incoming request on any of the endpoint. To do that as a poc (proof of concept) I used this example https://github.com/apache/camel/tree/master/examples/camel-example-restlet-jdbc

In this example I changed the following configuration

<bean id="RestletComponentService" class="org.apache.camel.component.restlet.RestletComponent">
    <constructor-arg ref="RestletComponent" />
    <property name="maxQueued" value="0" />
    <property name="maxThreads" value="1" />
</bean>

And in the

org.apache.camel.example.restlet.jdbc.MyRouteConfig

I added a sleep of 20 secs on one of the get direct route as following:

      from("direct:getPersons")
        .process(exchange -> { Thread.sleep(20000);})
        .setBody(simple("select * from person"))
        .to("jdbc:dataSource");

Now my assumption (which I understood from the camel documentation at http://camel.apache.org/restlet.html) is that only 1 request can be served at a given time and no other requests will be accepted (since maxQueued is set to 0) when the original request is still in process. But that is not happening in real. With this code I can call this endpoint many times concurrently and all of them give response after 20 secs and few millis.

I am searching for similar kind of setup from last few days and I haven't got anything yet. I wanted to understand if I am doing something wrong or if I have understood the documentation incorrectly.

Camel version used here is 2.23.0-SNAPSHOT


Solution

  • Instead of trying to configuring the thread pool of a Camel component, you could try to use Camel Hystrix to control the downstream calls of your application with the Circuit Breaker pattern.

    As soon as the downstream service returns errors or responds too slow, you can return an alternative response to the caller.