package com.zy.sc.common.mqtt; 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; /** * Created by vincent on 2021/9/15 */ public class PublishSample { public static void main(String[] args) { String topic = "CROSSWALK_DOWN_TEST_866156056763090"; // String content = "{\"event\":\"lbs\",\"serial\":\"903597431105519616\"}"; String content = "hello world"; int qos = 0; String broker = "tcp://iot.emperyland.com:61613"; String userName = "admin"; String password = "xltys1995"; String clientId = "dz-admin1"; // 内存存储 MemoryPersistence persistence = new MemoryPersistence(); try { // 创建客户端 MqttClient sampleClient = new MqttClient(broker, clientId, persistence); // 创建链接参数 MqttConnectOptions connOpts = new MqttConnectOptions(); // 在重新启动和重新连接时记住状态 connOpts.setCleanSession(false); // 设置连接的用户名 connOpts.setUserName(userName); connOpts.setPassword(password.toCharArray()); // 建立连接 sampleClient.connect(connOpts); // 创建消息 MqttMessage message = new MqttMessage(content.getBytes()); // 设置消息的服务质量 message.setQos(qos); // 发布消息 sampleClient.publish(topic, message); // 断开连接 sampleClient.disconnect(); // 关闭客户端 sampleClient.close(); } 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(); } } }