Java连接MQTT服务-ws方式

特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/mao2080/

1、Java代码

  1 package com.mao.mqtt;
  2 
  3 import java.text.SimpleDateFormat;
  4 import java.util.Date;
  5 
  6 import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  7 import org.eclipse.paho.client.mqttv3.MqttCallback;
  8 import org.eclipse.paho.client.mqttv3.MqttClient;
  9 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
 10 import org.eclipse.paho.client.mqttv3.MqttException;
 11 import org.eclipse.paho.client.mqttv3.MqttMessage;
 12 import org.eclipse.paho.client.mqttv3.MqttTopic;
 13 
 14 /**
 15  * 
 16  * 功能描述:MQTT测试
 17  * 创建人: mao2080@sina.com
 18  * 创建时间:2017年7月4日 下午5:08:59
 19  * 修改人: mao2080@sina.com
 20  * 修改时间:2017年7月4日 下午5:08:59
 21  */
 22 public class MQTTTest_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 static MqttClient client;
 44     
 45     /**MQTT-Client*/
 46     private static int uuid = 0;
 47 
 48     /**
 49      * @throws InterruptedException 
 50      * @throws MqttException */
 51     public static void main(String[] args) throws InterruptedException, MqttException {
 52         
 53         // 订阅消息的方法
 54         subscribe();
 55 //        
 56         publish();
 57     }
 58 
 59     /**
 60      * 
 61      * 描述:订阅信息
 62      * @author mao2080@sina.com
 63      * @created 2017年7月4日 下午4:53:47
 64      * @since 
 65      * @return
 66      */
 67     public static void subscribe() {
 68         try {
 69             // 创建MqttClient
 70             MQTTTest_tcp.getClient().setCallback(new MqttCallback() {
 71 
 72                 public void connectionLost(Throwable arg0) {
 73                     
 74                 }
 75 
 76                 public void messageArrived(String topic, MqttMessage message) throws Exception {
 77                     System.out.println("MQTT Rece:" + message.toString());
 78                 }
 79 
 80                 public void deliveryComplete(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      * 描述:获取MqttClient
 95      * @author mao2080@sina.com
 96      * @created 2017年7月6日 上午9:56:37
 97      * @since 
 98      * @return
 99      * @throws MqttException
100      */
101     public static MqttClient getClient() throws MqttException{
102         try {
103             if(client == null){
104                 client = new MqttClient(host, clientId);
105                 MqttConnectOptions conOptions = new MqttConnectOptions();
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         return client;
118     }
119 
120     /**
121      * 
122      * 描述:发布信息
123      * @author mao2080@sina.com
124      * @throws MqttException 
125      * @created 2017年7月4日 下午4:53:32
126      * @since
127      */
128     public static void publish() throws MqttException {
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 = new MqttMessage(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 }

 

2、Maven配置

1 <dependency>
2       <groupId>org.eclipse.paho</groupId>
3       <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
4       <version>1.2.0</version>
5 </dependency>

3、运行效果

posted @ 2017-11-06 16:22  mao2080  阅读(4068)  评论(0编辑  收藏  举报