Akhil K Nambiar
Akhil K Nambiar

Reputation: 3975

multi threaded persistent queue in java

Advice required on architecting module

I have a PORT Listener that listens to a PORT. All the requests received needs to be queued. I have 5 threads to process the queue. At the same time all the incoming requests has to be persisted and so is the responses on processing the same.

What is the best method to implement that?

Requests and responses can't be persisted together as there may be cases when I have to temporarily stop processing for few hours. Another situation may be failure of this application. We should be able to resume the queue from the persistent storage.

Another issue is we can't guarantee the size of the queue. So the time taken to fetch item continuously from db should be less. There may be times with no requests in DB. Should I go for NOSQL?

Upvotes: 0

Views: 912

Answers (1)

npe
npe

Reputation: 15709

The basic concept is to use a persistent queue.

Your request-handler (a Servlet, for example) should transform requests into a JMS messages and send them via a JMS Queue. You can use ActiveMQ, HornetQ, or others. Next, create a Message Driven Bean that consumes messages from that queue.

The JMS queues are persistent, so if your server breaks down, you will not lose your tasks. The MDB can be controlled (for example using JMX, or System properties) so you can stop processing any time and tasks will be stored inside the queue until you enable the MDB again.

Here is a complete JMS walk-through from Oracle that will show you step by step how to do it.

Upvotes: 1

Related Questions