简体   繁体   English

基于队列的集群聊天实现

[英]Clustered chat implementation based on Queue

I'm working on chat implementation. 我正在从事聊天实施。 The idea is very simple there is map with where keys are userNames and values are queue of messages which should be delivered to user 这个想法很简单,有一个映射,其中的键是用户名,值是应发送给用户的消息队列。

This map is represented by ConcurrentHashMap<String, <ConcurrentLinkedQueue<Message>>> 此映射由ConcurrentHashMap<String, <ConcurrentLinkedQueue<Message>>>

When new messaged for user arrived it is written to this map, and every user fetch their messages from queue using simple http pooling. 当有新消息发送给用户时,它将被写入此映射,并且每个用户都使用简单的http池从队列中获取消息。 This approach works fine. 这种方法效果很好。

Now i need implement similar functionality in cluster. 现在,我需要在集群中实现类似的功能。 For cache replication i use jboss cache. 对于缓存复制,我使用jboss缓存。 Right now cache replication works in synchronous fashion. 现在,缓存复制以同步方式工作。 This mean that write on local node to cache would not occur before write on others nodes. 这意味着在其他节点上进行写操作之前,不会在本地节点上写入要缓存的内容。

Basically cache interface looks similar to map with put and get methods. 基本上,缓存接口看起来类似于使用put和get方法进行映射。

Right now i would replicate the whole queue. 现在,我将复制整个队列。 When new messaged arrived i get queue from map, add new message in queue and “put” (rewrite) new queue on all nodes. 当收到新消息时,我从地图中获取队列,在队列中添加新消息,然后在所有节点上“放入”(重写)新队列。

When request comes from the user to fetch new messages to clone current queue, make queue associated with with this user empty on all nodes and deliver messages to user from cloned queue. 当用户发出请求以获取新消息以克隆当前队列的请求时,请将与此用户关联的队列在所有节点上都清空,然后将消息从克隆的队列传递给用户。

I feel there are a lot of cases, when something might go wrong. 我觉得在很多情况下可能会出问题。 For example i put message on queue on node A, replication of queue started and right now on another node B user fetch messages from queue making queue empty. 例如,我将消息放在节点A上的队列中,开始复制队列,现在就在另一个节点B上,用户从队列中获取消息,从而使队列为空。 And later replication of queue from node A finished and node B got already delivered messages again. 然后,从节点A的队列复制完成,节点B再次获得了已传递的消息。 User receive same messages twice 用户两次收到相同的消息

What is the common approach to implement this mesage routine in cluster ? 在集群中实现此消息例程的常用方法是什么?

In short, messaging. 简而言之,消息传递。 You can roll your own based on JMS, or use an Enterprise Service Bus . 您可以基于JMS滚动,也可以使用Enterprise Service Bus

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

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