MQTT的学习研究(4)moquette-mqtt 的使用之mqtt Blocking API客户端订阅并接收主题信息
来源:互联网 发布:查看linux网络地址 编辑:程序博客网 时间:2024/05/18 17:57
MQTT的学习研究(四)moquette-mqtt 的使用之mqt Blocking API客户端订阅并接收主题信息
在上面两篇关于mqtt的broker的启动和mqtt的服务端发布主题信息之后,我们客户端需要订阅相关的信息并接收相关的主题信息。
package com.etrip.mqtt;import java.net.URISyntaxException;import org.fusesource.mqtt.client.BlockingConnection;import org.fusesource.mqtt.client.MQTT;import org.fusesource.mqtt.client.Message;import org.fusesource.mqtt.client.QoS;import org.fusesource.mqtt.client.Topic;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * * MQTT moquette 的Client 段用于订阅主题,并接收主题信息 * * 采用阻塞式 订阅主题 * * @author longgangbai */public class MQTTClient { private static final Logger LOG = LoggerFactory.getLogger(MQTTClient.class);private final static String CONNECTION_STRING = "tcp://192.168.208.46:1883";private final static boolean CLEAN_START = true;private final static short KEEP_ALIVE = 30;// 低耗网络,但是又需要及时获取数据,心跳30sprivate final static String CLIENT_ID = "publishService";public static Topic[] topics = { new Topic("china/beijing", QoS.EXACTLY_ONCE), new Topic("china/tianjin", QoS.AT_LEAST_ONCE), new Topic("china/henan", QoS.AT_MOST_ONCE)};public final static long RECONNECTION_ATTEMPT_MAX=6;public final static long RECONNECTION_DELAY=2000;public final static int SEND_BUFFER_SIZE=2*1024*1024;//发送最大缓冲为2M public static void main(String[] args) {//创建MQTT对象 MQTT mqtt = new MQTT(); BlockingConnection connection=null; try { //设置mqtt broker的ip和端口mqtt.setHost(CONNECTION_STRING);//连接前清空会话信息mqtt.setCleanSession(CLEAN_START);//设置重新连接的次数mqtt.setReconnectAttemptsMax(RECONNECTION_ATTEMPT_MAX);//设置重连的间隔时间mqtt.setReconnectDelay(RECONNECTION_DELAY);//设置心跳时间mqtt.setKeepAlive(KEEP_ALIVE);//设置缓冲的大小mqtt.setSendBufferSize(SEND_BUFFER_SIZE);//获取mqtt的连接对象BlockingConnection connection = mqtt.blockingConnection(); //MQTT连接的创建 connection.connect(); //创建相关的MQTT 的主题列表 Topic[] topics = {new Topic("china/beijing", QoS.AT_LEAST_ONCE)}; //订阅相关的主题信息 byte[] qoses = connection.subscribe(topics); // while(true){ //接收订阅的消息内容 Message message = connection.receive(); //获取订阅的消息内容 byte[] payload = message.getPayload(); // process the message then: LOG.info("MQTTClient Message Topic="+message.getTopic()+" Content :"+new String(payload)); //签收消息的回执 message.ack(); Thread.sleep(2000); }} catch (URISyntaxException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {connection.disconnect();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
0 0
- MQTT的学习研究(4)moquette-mqtt 的使用之mqtt Blocking API客户端订阅并接收主题信息
- MQTT的学习研究(四)moquette-mqtt 的使用之mqtt Blocking API客户端订阅并接收主题信息
- MQTT的学习研究(六) MQTT moquette 的 Blocking API 订阅消息客户端使用
- MQTT的学习研究(六) MQTT moquette 的 Blocking API 订阅消息客户端使用
- MQTT的学习研究(三)moquette-mqtt 的使用之mqtt服务发布主题信息
- MQTT的学习研究(3)moquette-mqtt 的使用之mqtt服务发布主题信息
- MQTT的学习研究(五) MQTT moquette 的 Blocking API 发布消息服务端使用
- MQTT的学习研究(5) MQTT moquette 的 Blocking API 发布消息服务端使用
- MQTT的学习研究(十二) MQTT moquette 的 Future API 消息发布订阅的实现
- MQTT的学习研究(十四) MQTT moquette 的 Callback API 消息发布订阅的实现
- MQTT的学习研究(十二) MQTT moquette 的 Future API 消息发布订阅的实现
- MQTT的学习研究(十四) MQTT moquette 的 Callback API 消息发布订阅的实现
- MQTT的学习研究(二)moquette-mqtt 的使用之mqtt broker的启动
- MQTT的学习研究(2)moquette-mqtt 的使用之mqtt broker的启动
- MQTT moquette 的 Blocking API 发布消息服务端使用
- MQTT的学习研究(6)MQTT 订阅消息客户端使用
- MQTT的学习研究(八)基于HTTP DELETE MQTT 订阅消息服务端使用
- MQTT的学习研究(八)基于HTTP DELETE MQTT 订阅消息服务端使用
- linux的eclipse下配置javah
- oc学习之旅:KVO
- 第十七周上机实践项目——委派任务
- ajax打开新窗口实现
- 深入理解java:注解(annotation)的基本概念
- MQTT的学习研究(4)moquette-mqtt 的使用之mqtt Blocking API客户端订阅并接收主题信息
- Solr Cookbook学习记录 - Highlighting学习
- 微信公众平台开发(十) 消息回复总结
- Java的安全学习(包括加密,数字签名,证书和认证)
- 关于货币
- Solr Cookbook学习记录 - MoreLikeThis学习
- MQTT的学习研究(5) MQTT moquette 的 Blocking API 发布消息服务端使用
- 如何在WinCE 5.0中安装 .Net Compact Framework?
- listview 实现微信删除功能向左移动item出现隐藏的删除按钮功能终于实现了,分享总结一下。(跟微信删除一样额)