Java: thread synchronization on multiple servers

I have a problem where I need to synchronize processing for multiple threads on several different servers for a Java service on Windows.

In this application, I have several consumer thread streams that go out of the same JMS queue. Messages are in groups of 3 or 4, and I need to make sure that the messages in each group are processed completely in sequential order. I need some kind of synchronization mechanism to make sure thread1 pulled the message and then thread2 pulled the next message from this group, thread2 waits for thread1 to finish processing before starting to process this message.

Any suggestions on distributed thread synchronization mechanisms? Any type of solution would be good (JMS solutions, distributed caching, etc.)

Note. The JMS provider we use is ActiveMQ.

+5
source share
3 answers

ActiveMQ supports message groups that literally should be exactly what you need.

+7
source

You might consider using Hazelcast distributed locks. Super lightweight, simple and open source.

java.util.concurrent.locks.Lock lock = Hazelcast.getLock ("mymonitor");
lock.lock ();
try {
// do your stuff
}finally {
   lock.unlock();
}

Hello,

-talip

Hazelcast - , , , ,

+7

Is there something like group id in message headers? If so, the consumer can create Selectorgroups for sequential processing.

Assigning a group to a specific consumer can be accomplished by hashing a group identifier, or they can actively coordinate with each other using some consensus protocol, such as Paxos or virtual synchronization (when sending messages in a separate queue).

+1
source

All Articles