1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > mqtt+ActiveMQ用发布订阅实现点对点推送

mqtt+ActiveMQ用发布订阅实现点对点推送

时间:2019-07-17 05:42:47

相关推荐

mqtt+ActiveMQ用发布订阅实现点对点推送

使用语言:java消息中间件:activemq用到工具:git、maven、任意文本编辑器

目录

目录发布到订阅流程图实现点对点点对点测试程序

发布到订阅流程图

实现点对点

ok,了解消息在activemq中的传输流程后我们来看看在哪里截取消息并发送到指定的消费者吧。

1、使用git下载activemq源码

git clone https://git-wip-/repos/asf/activemq.git

2、git自动下载最新版本的源码,需要下载和你使用的mq程序对应的版本,上一步的网址中有tag一栏,选择对应的版本进去,查看对应的commit所产生的hashcode

git reset –hard <对应版本hashcode>

3、自定义消息到topic后推送给订阅者的消息分发策略

策略存放路径在项目根路径下的\activemq-broker\src\main\java\org\apache\activemq\broker\region\policy\

在此目录下随便选一个原有的*Policy.java文件,修改此文件内容

4、重新编译broker

在\activemq-broker\下执行mvn package进行打包(打包后的内容应该在target文件夹中)

把生成的activemq-broker-version.jar拷贝到使用的mq程序的lib目录下

5、修改activemq的配置文件

接下来就是实现点对点的重点,自定义分发策略

1、点对点的实现是通过设置clientId来确定两个点,因为MQTT框架无法设置message的properties属性,所以把clientId放到消息体中,用某个特殊符号隔开,我这里用的是@符号

2、producer.send(message);发送消息后到达topic中,如果此topic有订阅的消费者,那么才会进入到分发策略,而进入到哪个分发策略是你在第五步配置的\conf\activemq.xml中指定的那个,配置文件更改后就会进入到下面的策略中。

3、分发策略中定义,必须是在名为.ptp结尾的topic中才会进行点对点推送,不然就和正常的一样一对多的推送

String msgBody = "message body";String clientId = "mqtt-1012";producer.send(clientId+"@"+msgBody);

package org.apache.activemq.broker.region.policy;import java.util.List;import org.apache.activemq.broker.region.MessageReference;import org.apache.activemq.broker.region.Subscription;import org.mand.ActiveMQDestination;import org.apache.activemq.filter.MessageEvaluationContext;public class PriorityDispatchPolicy extends SimpleDispatchPolicy {public boolean dispatch(MessageReference node, MessageEvaluationContext msgContext, List<Subscription> consumers) throws Exception {//指定特定后缀名的topic进入自定义分发策略if(!node.getMessage().getDestination().getQualifiedName().endsWith(".ptp"))return super.dispatch(node, msgContext, consumers);String _clientId = "";//获取消息内容ByteSequence sequence = node.getMessage().getContent();byte[] message = sequence.getData();String strMessage = new String(message, "utf-8");//format:clientID@messageBodyif (!"".equals(strMessage) && strMessage.indexOf("@")>-1) _clientId = strMessage.substring(0, strMessage.indexOf("@"));if ("".equals(_clientId))return super.dispatch(node, msgContext, consumers);strMessage = strMessage.substring(strMessage.indexOf("@") + 1, strMessage.length());node.getMessage().setContent(new ByteSequence(strMessage.getBytes("utf-8")));ActiveMQDestination _destination = node.getMessage().getDestination();int count = 0;for (Subscription sub : consumers) {// Don't deliver to browsersif (sub.getConsumerInfo().isBrowser()) {continue;}// Only dispatch to interested subscriptionsif (!sub.matches(node, msgContext)) {sub.unmatched(node);continue;}if (_clientId != null && _destination.isTopic() && _clientId.equals(sub.getContext().getClientId())&& _destination.getQualifiedName().endsWith(".ptp")) {//把消息推送给满足要求的subscriptionsub.add(node);count++;} else {sub.unmatched(node);}}return count > 0;}}

下面展示的是第五步修改的内容,用文本编辑器打开mq程序根目录\conf\activemq.xml文件,寻找到broker标签,修改其中的内容

<destinationPolicy><policyMap><policyEntries><policyEntry topic=">" ><dispatchPolicy><!-- 这里的priorityDispatchPolicy是你选对的策略分发类,首字母小写 --><priorityDispatchPolicy /></dispatchPolicy><pendingMessageLimitStrategy><constantPendingMessageLimitStrategy limit="1000"/></pendingMessageLimitStrategy></policyEntry></policyEntries></policyMap></destinationPolicy>

点对点测试程序

直接贴代码,不做解释啦

public class P2PMqttTest {public static Connection connection;public static Statement stat;public static void main(String[] args) throws JMSException {DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61616");//consumersubMsg(factory, "mqtt-1011");subMsg(factory, "mqtt-1012");subMsg(factory, "aaaaaa");//productsendMsg(factory);}private static void subMsg(ActiveMQConnectionFactory factory, String clientId) throws JMSException {Connection connection = factory.createConnection();connection.setClientID(clientId);connection.start();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);Topic topic = session.createTopic("channel.ptp");MessageConsumer consumer = session.createConsumer(topic);consumer.setMessageListener(new ConsumeListenerMessage(clientId));}private static void sendMsg(ActiveMQConnectionFactory factory) throws JMSException {Connection connection = factory.createConnection();connection.setClientID("mqtt-1022");connection.start();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);Topic topic = session.createTopic("channel.ptp");MessageProducer producer = session.createProducer(topic);//clientId@messageBodyMessage message = session.createTextMessage("mqtt-1011"+"@"+"test");producer.send(message);}static class ConsumeListenerMessage implements MessageListener {private String thisClientId;public void onMessage(Message message) {TextMessage msg = (TextMessage) message;try {String clientId = msg.getStringProperty("PTP_CLIENTID");String msgBody = msg.getText();System.out.println("============================================");System.out.println(thisClientId+" recive message");System.out.println(msgBody);System.out.println("============================================");} catch (JMSException e) {e.printStackTrace();}}public ConsumeListenerMessage(String thisClientId) {this.thisClientId = thisClientId;}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。