I have created a REST service with Apache Camel (REST DSL) running on Karaf container that accepts a list of items and returns the availabilities of those them. The problem is that if the service is called in parallel it doesn’t return the correct results. For example, let’s assume that we have the following two calls:
Call 1: item1, item2 Call 2: item3, item4
I would expect the following two answers
Response 1: availabilityOfItem1, availabilityOfItem2 Response 2: availabilityOfItem3, availabilityOfItem4
But instead of that I am getting the two answers with mixed availabilities
Response 1: availabilityOfItem1, availabilityOfItem2, availabilityOfItem3, availabilityOfItem4 Response 2: availabilityOfItem1, availabilityOfItem2, availabilityOfItem3, availabilityOfItem4
Probably this problem occurs due to the related bean that I have for the service, but let me give you an overview of what I did in my Camel context.
<camelContext>
<restConfiguration ... />
<rest path="/" consumes="application/json" produces="application/json">
<post uri="/availabilities" type="com.xxx.ArticleListReq">
<to uri="direct:availabilities"/>
</post
</rest>
<route id="route.Availabilities">
<from uri="direct:availabilities"/>
<bean ref="availabilitiesBean" method="init"/>
<split parallelProcessing="true">
<simple>${body.articles}</simple>
...
SQL calls for each item
<bean ref="availabilitiesBean" method="buildResponse"/>
</split>
<bean ref="availabilitiesBean" method="getResponse"/>
</route>
</camelContext>
As you can notice when the service is called it goes to the direct:availabilities route that does the job. I am using availabilitiesBean in order to initialize the response object (List), then for each request item I am getting availability from db and put the result to the response object that was previously initialized (buildResponse) and at the end I am returning that object with method getResponse.
The problem is that bean has a singleton scope and that means that for both calls the same bean is used. So, call 1 writes the answer for item1 and item2 but the same time call 2 adds also item3 and item 4 in the response.
Is there a possibility to create a request scope bean? Or follow another approach in order to overcome this issue?
Thanks a lot!