eclipse paho java_Eclipse paho学习笔记

eclipse paho java_Eclipse paho学习笔记EclipsePahojava支持java和android两个客户端异步和同步,MqttAsyncClient,MqttClient其中后者是前者的包装同步器,特效除了5.0都支持有两个版本可以相互替换org.eclipse.pahoorg.eclipse.paho.client.mqttv3%VERSION%下面是简单同步的例子:importorg.eclipse.paho.clien…

Eclipse Paho

java 支持java和android

两个客户端异步和同步, MqttAsyncClient,MqttClient

其中 后者是前者的包装同步器,特效除了5.0都支持

有两个版本可以相互替换

org.eclipse.paho

org.eclipse.paho.client.mqttv3

%VERSION%

下面是简单同步的例子:

import org.eclipse.paho.client.mqttv3.MqttClient;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;

import org.eclipse.paho.client.mqttv3.MqttException;

import org.eclipse.paho.client.mqttv3.MqttMessage;

import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;public classMqttPublishSample {public static voidmain(String[] args) {

String topic= “MQTT Examples”;

String content= “Message from MqttPublishSample”;int qos = 2;

String broker= “tcp://iot.eclipse.org:1883”;

String clientId= “JavaSample”;

MemoryPersistence persistence= newMemoryPersistence();try{

MqttClient sampleClient= newMqttClient(broker, clientId, persistence);

MqttConnectOptions connOpts= newMqttConnectOptions();

connOpts.setCleanSession(true);

System.out.println(“Connecting to broker:”+broker);

sampleClient.connect(connOpts);

System.out.println(“Connected”);

System.out.println(“Publishing message:”+content);

MqttMessage message= newMqttMessage(content.getBytes());

message.setQos(qos);

sampleClient.publish(topic, message);

System.out.println(“Message published”);

sampleClient.disconnect();

System.out.println(“Disconnected”);

System.exit(0);

}catch(MqttException me) {

System.out.println(“reason”+me.getReasonCode());

System.out.println(“msg”+me.getMessage());

System.out.println(“loc”+me.getLocalizedMessage());

System.out.println(“cause”+me.getCause());

System.out.println(“excep”+me);

me.printStackTrace();

}

}

}

下面是异步处理三个例子

https://github.com/eclipse/paho.mqtt.java/tree/master/org.eclipse.paho.sample.mqttv3app/src/main/java/org/eclipse/paho/sample/mqttv3app

最新的java的同步例子有四个类

https://github.com/eclipse/paho.mqtt.java/tree/master/org.eclipse.paho.sample.mqttclient/src/main/java/org/eclipse/paho/sample/mqttclient/mqttv3

发布订阅 连接执行

执行了4种发布的数据,有文件和消息

今天的文章eclipse paho java_Eclipse paho学习笔记分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7035.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注