1 packagecom.mao.mqtt;2
3 importjava.text.SimpleDateFormat;4 importjava.util.Date;5
6 importorg.eclipse.paho.client.mqttv3.IMqttDeliveryToken;7 importorg.eclipse.paho.client.mqttv3.MqttCallback;8 importorg.eclipse.paho.client.mqttv3.MqttClient;9 importorg.eclipse.paho.client.mqttv3.MqttConnectOptions;10 importorg.eclipse.paho.client.mqttv3.MqttException;11 importorg.eclipse.paho.client.mqttv3.MqttMessage;12 importorg.eclipse.paho.client.mqttv3.MqttTopic;13
14 /**
15 *16 * 功能描述:MQTT测试17 * 创建人: mao2080@sina.com18 * 创建时间:2017年7月4日 下午5:08:5919 * 修改人: mao2080@sina.com20 * 修改时间:2017年7月4日 下午5:08:5921 */
22 public classMQTTTest_tcp {23
24 /**MQTT服务端ip及端口*/
25 private static String host = "ws://ip:8882";26
27 /**账号*/
28 private static String username = "li2080";29
30 /**密码*/
31 private static String password = "123";32
33 /**订阅的主题*/
34 private static String subTopic = "a/b/c";35
36 /**clientID*/
37 private static String clientId = "li2080";38
39 /**发布的主题*/
40 private static String pubTopic = "a/b/c";41
42 /**MQTT-Client*/
43 private staticMqttClient client;44
45 /**MQTT-Client*/
46 private static int uuid = 0;47
48 /**
49 *@throwsInterruptedException50 *@throwsMqttException*/
51 public static void main(String[] args) throwsInterruptedException, MqttException {52
53 //订阅消息的方法
54 subscribe();55 //
56 publish();57 }58
59 /**
60 *61 * 描述:订阅信息62 *@authormao2080@sina.com63 * @created 2017年7月4日 下午4:53:4764 *@since
65 *@return
66 */
67 public static voidsubscribe() {68 try{69 //创建MqttClient
70 MQTTTest_tcp.getClient().setCallback(newMqttCallback() {71
72 public voidconnectionLost(Throwable arg0) {73
74 }75
76 public void messageArrived(String topic, MqttMessage message) throwsException {77 System.out.println("MQTT Rece:" +message.toString());78 }79
80 public voiddeliveryComplete(IMqttDeliveryToken token) {81
82 }83
84 });85 MQTTTest_tcp.getClient().subscribe(subTopic, 0);86 System.out.println("连接状态:" +client.isConnected());87 } catch(Exception e) {88 e.printStackTrace();89 }90 }91
92 /**
93 *94 * 描述:获取MqttClient95 *@authormao2080@sina.com96 * @created 2017年7月6日 上午9:56:3797 *@since
98 *@return
99 *@throwsMqttException100 */
101 public static MqttClient getClient() throwsMqttException{102 try{103 if(client == null){104 client = newMqttClient(host, clientId);105 MqttConnectOptions conOptions = newMqttConnectOptions();106 conOptions.setUserName(username);107 conOptions.setPassword(password.toCharArray());108 conOptions.setCleanSession(true);109 client.connect(conOptions);110 }111 if(!client.isConnected()){112 client.reconnect();113 }114 } catch(Exception e) {115 e.printStackTrace();116 }117 returnclient;118 }119
120 /**
121 *122 * 描述:发布信息123 *@authormao2080@sina.com124 *@throwsMqttException125 * @created 2017年7月4日 下午4:53:32126 *@since
127 */
128 public static void publish() throwsMqttException {129 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");130 String sendMsg = "{time:"+sdf.format(new Date())+", content:"+com.lds.iot.common.util.UUIDUtil.getLowerLetterNumber(15)+", from: java console}";131 try{132 MqttTopic topic =MQTTTest_tcp.getClient().getTopic(pubTopic);133 MqttMessage message = newMqttMessage(sendMsg.getBytes());134 message.setQos(0);135 topic.publish(message);136 System.out.println("MQTT Send:" +sendMsg);137 } catch(Exception e) {138 e.printStackTrace();139 }140 }141
142
143
144 }