Andy White
Andy White

Reputation: 88475

Java: synchronizing threads across multiple servers

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

In this application, I have multiple consumer threads pullings messages off the same JMS queue. Messages come in in groups of 3 or 4, and I need to make sure the messages in each group are processed completely in serial. I need some sort of synch mechanism to make sure if thread1 pulls a message off, then thread2 pulls the next message from that group, thread2 waits for thread1 to finish processing before starting to process it's message.

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

Note: the JMS provider we're using is ActiveMQ.

Upvotes: 5

Views: 6251

Answers (3)

Talip Ozturk
Talip Ozturk

Reputation: 1709

you might want to consider using Hazelcast distributed locks. Super lite, easy and open source.

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

Regards,

-talip

Hazelcast - Open Source Distributed Queue, Map, Set, List, Lock

Upvotes: 8

erickson
erickson

Reputation: 269897

Is there anything like a group ID in the message headers? If so, a consumer could create a Selector to process the group in sequence.

The assignment of a group to a particular consumer could be done by hashing the group identifier, or they could actively coordinate with each other using some consensus protocol like Paxos or virtual synchrony (with the messages being sent over a separate queue).

Upvotes: 1

andri
andri

Reputation: 11292

ActiveMQ has support for message groups which, quite literally, should be exactly what you need.

Upvotes: 8

Related Questions