You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by hasikada <ad...@gmail.com> on 2015/12/01 19:21:22 UTC

Producer-consumer problem

Hello gentlemen, 

Please, how to implement *producer-consumer problem* (bounded-buffer) in
Camel. One route puts exchanges to shared queue (e.g on user request).
Second route processes exchanges from shared queue *one by one*. The first
route doesn't wait for exchange processing completion.

from("direct:handleUserRequest")
    .to("seda:requests?waitForTaskToComplete=Never")
    .setBody().constant("Your request is being processed."); // User gets a
response immediately

from("seda:requests?concurrentConsumers=1").to("direct:processRequest");

from("direct:processRequest")
     // Long running operation, notify user via email when completed
    .......................

The setting only 'waitForTaskToComplete=Never' allows to process multiple
user requests in time. The setting 'concurrentConsumers=1' blocks
'handleUserRequest' route.








--
View this message in context: http://camel.465427.n5.nabble.com/Producer-consumer-problem-tp5774608.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Producer-consumer problem in Camel

Posted by hasikada <ad...@gmail.com>.
Hello again, this is the solution what I found after playing with seda, load
balancer, wiretap:

        ExecutorService execExecutorService = new
ThreadPoolBuilder(getContext())
           
.poolSize(1).maxPoolSize(1).maxQueueSize(10).build("InvoiceExecutorService");

        from("direct:handleUserRequest")
        .routeId("invoice")
            .log("RECEIVED")
            .wireTap("direct:processRequestSync", executorService);
            .setBody().constant("Your request is being processed."); // User
gets a response immediately

        from("direct:processRequestSync").to(ExchangePattern.InOut,
"direct:processRequest");
        
        from("direct:processRequest")
            .log("PROCESSING")
            // Long running operation, notify user via email when completed
            .............................
            .log("DONE")

Now, I get the output what I expect. For example the 'handleUserRequest' is
invoked randomly REST 3 times via REST.
    RECEIVED
    RECEIVED
    PROCESSING
    RECEIVED
    DONE
    PROCESSING
    DONE
    PROCESSING
    DONE



--
View this message in context: http://camel.465427.n5.nabble.com/Producer-consumer-problem-in-Camel-tp5774608p5774834.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Producer-consumer problem in Camel

Posted by hasikada <ad...@gmail.com>.
And this one would be the final solution, sorry for distribution:

       
from("activemq:queue:requests").to("direct:processRequest?exchangePattern=InOut");
       
        from("direct:handleUserRequest")
            .to(ExchangePattern.InOnly, "activemq:queue:requests")
            .............................
        
        from("direct:processRequest")
            .............................




--
View this message in context: http://camel.465427.n5.nabble.com/Producer-consumer-problem-in-Camel-tp5774608p5774836.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Producer-consumer problem in Camel

Posted by hasikada <ad...@gmail.com>.
Hello,

When setting option 'waitForTaskToComplete=Never' the 'handleUserRequest'
route puts an exchange to seda queue and immediately responds to user 'Your
request .....'. I would expect the setting 'concurrentConsumers=1' makes
that the exchanges from seda queue are processed one by one in
'direct:processRequest' route. But it doesn't happen. Currently when I
invoke the 'handleUserRequest' two times (or more times). I see two
processing by 'direct:processRequest' route. 

Adam



--
View this message in context: http://camel.465427.n5.nabble.com/Producer-consumer-problem-in-Camel-tp5774608p5774617.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Producer-consumer problem in Camel

Posted by yogu13 <yo...@gmail.com>.
what do you mean by "It still allows to process multiple user requests in
time" ?

Regards,
-Yogesh



--
View this message in context: http://camel.465427.n5.nabble.com/Producer-consumer-problem-in-Camel-tp5774608p5774613.html
Sent from the Camel - Users mailing list archive at Nabble.com.