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
Activemq mqtt 点对点聊天实现(转载)的更多相关文章
- Linux 网络编程详解三(p2p点对点聊天)
//p2p点对点聊天多进程版--服务器(信号的使用) #include <stdio.h> #include <stdlib.h> #include <string.h& ...
- 使用ActiveMQ实现简易聊天功能
一 什么是消息队列 我们可以把消息队列比作是一个存放消息的容器,当我们需要使用消息的时候可以取出消息供自己使用.消息队列是分布式系统中重要的组件,使用消息队列主要是为了通过异步处理提高系统性能和削峰. ...
- ASP.NET SingalR 点对点聊天实现思路总结
前一段时间写了一个简单的聊天室,是群聊的方式.博客地址:http://www.cnblogs.com/panzi/p/4980346.html.还有一种需求就是常见的尤其是培训机构的主页面,经常会有1 ...
- SpringBoot2.0之整合ActiveMQ(点对点模式)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 开源Html5+Websocket+Mqtt实时聊天室
本应用示例使用Coolpy7作为Mqtt服务器并启用Websocket代理完美支持高并发大流量即时通过能力,本示以即时通信聊天为为例.还可以应用到其他软件应用如:网页客服系统.网站信息通知.网页即时通 ...
- (二)ActiveMQ之点对点消息实现
一.点对点消息实现概念 在点对点或队列模型下,一个生产者向一个特定的队列发布消息,一个消费者从该队列中读取消息.这里,生产者知道消费者的队列,并直接将消息发送到消费者的队列.这种模式被概括为:只有一个 ...
- windows安装ActiveMQ以及点对点以及发布订阅
一.MQ产品的分类 1.RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,XMPP, SMTP, STOMP,也正是如此,使的它变的非常重量级,更适合于企业级 ...
- MQTT介绍与使用(转载)
物联网是新一代信息技术的重要组成部分,也是"信息化"时代的重要发展阶段.其英文名称是:"Internet of things(IoT)".顾名思义,物联网就是物 ...
- 基于ActiveMQ的点对点收发消息
ActiveMQ是apache的一个开源消息引擎.可以作为即通引擎或者消息中间件引擎. 准备 下载ActiveMQ http://activemq.apache.org/download.html 进 ...
随机推荐
- 在Oracle中恢复被DROP掉的表
在Oracle中可能不小心会DROP掉一个表,如果没有定期做备份的话,将会带来很大的麻烦.如果有的情况下,每天的数据都很重要,而定期备份的周期又稍长,情况恐怕也不容乐观!以前只知道Windows有个回 ...
- Merge K Sorted Arrays
This problem can be solved by using a heap. The time is O(nlog(n)). Given m arrays, the minimum elem ...
- Ternary Expression Parser
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...
- ScriptedSandbox64.exe 在写Winform程序Debug时不停提交数据
抓包时发现不停的在提交数据,导致抓包内容看不到. 取消方式:Tools -> Options -> Debugging -> General -> Enable Diagnos ...
- 如何做JS 单体模式的设计---->>js设计模式<<-------单体模式
1. 单体模式是js中最基本 单最有用的模式之一,非常常用. 单体模式的基本结构如下: var Person = { name: 'lilu', age:', sayHi: function(){ a ...
- maven学习(上)- 基本入门用法
一.下载及安装 1.1 下载maven 3.1.1 先到官网http://maven.apache.org/download.cgi 下载最新版本(目前是3.1.1 ),下载完成后,解压到某个目录(本 ...
- iOS10 远程推送服务器所需证书以及应用授权文件配置
推送证书制作步骤(目的:导出服务器需要的p12证书) 第一步: 打开Mac系统的"钥匙串访问"-"证书助理"-"从证书颁发机构请求证书" 取 ...
- LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- Delphi多线程的OnTerminate属性(附加一个关于临界区线程同步的例子)
首先看TThread源码中关于OnTerminate的代码: public .... property OnTerminate: TNotifyEvent read FOnTerminate writ ...
- direct path read
在11g中,全表扫描可能使用direct path read方式,绕过buffer cache,这样的全表扫描就是物理读了. 在10g中,都是通过gc buffer来读的,所以不存在direct pa ...