zoukankan      html  css  js  c++  java
  • Activemq mqtt 点对点聊天实现(转载)

    我这想到一个点对点聊天的方法,不用没割人都建立一个topic了,思路还是自定义一个分发策略,具体如下:

    1、  建立一个topic,所有人都用匹配订阅的方式订阅以该topic为头的topic,例如:所有人都订阅PTP/#。

    2、  例如A向B发送聊天信息,B的clientId是bbb,A只需要向PTP/bbb 推送聊天信息,我写的自定义策略会针对所有PTP开头的topic做自定义分发<policyEntry topic="PTP.>">,将topic里的clientId解析出来,并将该消息只发给B。

    自定义策略代码:

    复制代码
    public class ClientIdFilterDispatchPolicy extends SimpleDispatchPolicy {
        public boolean dispatch(MessageReference node,
                MessageEvaluationContext msgContext, List<Subscription> consumers)
                throws Exception {
            System.out.println("--------------------------------来了------------------------------------------");
            System.out.println(node.getMessage().getDestination().getQualifiedName());
            System.out.println(node.getMessage().getDestination().getPhysicalName());
            String topic = node.getMessage().getDestination().getPhysicalName();
            String clientId = topic.substring(topic.indexOf(".")+1, topic.length());
            System.out.println("clientId:" + clientId);
            System.out.println("--------------------------------end------------------------------------------");
            
            if (clientId == null)
                super.dispatch(node, msgContext, consumers);
    
            ActiveMQDestination destination = node.getMessage().getDestination();
    
            int count = 0;
            for (Subscription sub : consumers) {
                if (sub.getConsumerInfo().isBrowser()) {
                    continue;
                }
                if (!sub.matches(node, msgContext)) {
                    sub.unmatched(node);
                    continue;
                }
                System.out.println("isTopic:" + destination.isTopic());
                System.out.println("getClientId:" + sub.getContext().getClientId());
                if ((clientId != null)
                        && (destination.isTopic())
                        && (clientId.equals(sub.getContext().getClientId()))
                        ) {
                    sub.add(node);
                    count++;
                } else {
                    sub.unmatched(node);
                }
            }
    
            return count > 0;
        }
    
        
    }
    复制代码
    
    

    activemq.xml

    
    
                  <policyEntry topic="PTP.>">
            <dispatchPolicy>
                        <clientIdFilterDispatchPolicy/>
                      </dispatchPolicy>
            </policyEntry>    

    转载:http://www.cnblogs.com/momofeng/p/5482775.html#undefined
  • 相关阅读:
    c++ 存储连续性,作用域和链接性注意点
    函数模板的知识点总结
    c++ 左值引用的注意点
    VS2015如何在同一个解决方案下建立多个项目及多个项目之间的引用
    编译opencv4.1.0+tesseract5.0 的Realease x64版本遇见的问题解决
    逻辑化简-卡诺图
    从Word Embedding到Bert模型—自然语言处理中的预训练技术发展史 (转载)
    matlab绘图
    多个EXCEL文件合并成一个
    数学建模及机器学习算法(一):聚类-kmeans(Python及MATLAB实现,包括k值选取与聚类效果评估)
  • 原文地址:https://www.cnblogs.com/candyzhmm/p/6092542.html
Copyright © 2011-2022 走看看