简体   繁体   English

Java:跨多个服务器同步线程

[英]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. 我有一个问题,我需要为Windows上的Java服务跨多个不同服务器同步多个线程的处理。

In this application, I have multiple consumer threads pullings messages off the same JMS queue. 在这个应用程序中,我在同一个JMS队列中有多个消费者线程拉取消息。 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. 消息以3或4组的形式出现,我需要确保每个组中的消息都是完全串行处理的。 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. 我需要某种同步机制来确保thread1是否关闭了一个消息,然后thread2从该组中提取下一条消息,thread2在开始处理它的消息之前等待thread1完成处理。

Any suggestions on distributed synching mechanisms for threads? 关于线程的分布式同步机制的任何建议? Any type of solution would be good (JMS solutions, distributed caching, etc.) 任何类型的解决方案都会很好(JMS解决方案,分布式缓存等)

Note: the JMS provider we're using is ActiveMQ. 注意:我们使用的JMS提供程序是ActiveMQ。

ActiveMQ支持消息组 ,从字面上看,它应该正是您所需要的。

you might want to consider using Hazelcast distributed locks. 您可能要考虑使用Hazelcast分布式锁。 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 -talip

Hazelcast - Open Source Distributed Queue, Map, Set, List, Lock Hazelcast - 开源分布式队列,地图,设置,列表,锁定

Is there anything like a group ID in the message headers? 消息头中是否有类似组ID的内容? If so, a consumer could create a Selector to process the group in sequence. 如果是这样,消费者可以创建一个Selector来按顺序处理该组。

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). 可以通过散列组标识符来完成将组分配给特定消费者,或者可以使用诸如Paxos或虚拟同步之类的一些共识协议(其中消息通过单独的队列发送)主动地彼此协调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM