Reputation: 111
I am planning to write code that's similar to producer and consumer using ExecutorService
with a fixed threadpool and IBM MQ messaging.
Suppose as consumer I created 10 fixed threads. How it will handle it if I place 10 messages in consumer queue? How 10 consumer worker threads will cover below scenarios?
each worker thread take single message synchronously and process the message?
each consumer worker threads take all these 10 message like 1 worker thread per one message?
After reading this message as second scenario above ,How each thread call executor service.Is it done concurrenly or synchronously.
if there are 20 messages in queue,how consumer worker thread takes these message,each thread takes 2 messages? If it takes one message per one thread what will happen to other 10 messages?
While processing the above scenarios there is webservice call and internal api method calls but those are synchronous methods. So is there any use if i implement this class to process the code concurrently?
Upvotes: 1
Views: 1153
Reputation: 1236
If you're running in an application server, like WebSphere for example, then you can simply deploy a Message Driven Bean (MDB) onto the JMS queue, and it will do pretty much exactly what you're describing.
If you're just building a Java application, then using the ExecutorService would work. Start by placing a MessageListener onto the Session, and have the onMessage() of that listener submit() a processor (e.g. a Runnable) of the message to the ExecutorService. Once that processor has done its work, it should acknowledge() the message.
Upvotes: 3
Reputation: 53525
I don't think it's a good idea to implement producer/consumer scheme when you have good tools like JMS already implemented, debugged, support many features and supported by frameworks (like Spring for instance).
Don't invent the wheel!
Upvotes: 0