1 packagecom.mao.mqtt;2
3 importjava.io.FileInputStream;4 importjava.security.KeyStore;5 importjava.security.cert.CertificateFactory;6 importjava.security.cert.X509Certificate;7 importjava.text.SimpleDateFormat;8 importjava.util.Date;9
10 importjavax.net.ssl.SSLContext;11 importjavax.net.ssl.SSLSocketFactory;12 importjavax.net.ssl.TrustManagerFactory;13
14 importorg.eclipse.paho.client.mqttv3.IMqttDeliveryToken;15 importorg.eclipse.paho.client.mqttv3.MqttCallback;16 importorg.eclipse.paho.client.mqttv3.MqttClient;17 importorg.eclipse.paho.client.mqttv3.MqttConnectOptions;18 importorg.eclipse.paho.client.mqttv3.MqttException;19 importorg.eclipse.paho.client.mqttv3.MqttMessage;20 importorg.eclipse.paho.client.mqttv3.MqttTopic;21
22 /**
23 *24 * 功能描述:MQTT测试25 * 创建人: mao2080@sina.com26 * 创建时间:2017年7月4日 下午5:08:5927 * 修改人: mao2080@sina.com28 * 修改时间:2017年7月4日 下午5:08:5929 */
30 public classMQTTTest_wss {31
32 /**MQTT服务端ip及端口*/
33 private static String host = "wss://ip:443";34
35 /**账号*/
36 private static String username = "li2080";37
38 /**密码*/
39 private static String password = "123";40
41 /**订阅的主题*/
42 private static String subTopic = "a/b/c";43
44 /**clientID*/
45 private static String clientId = "li2080";46
47 /**发布的主题*/
48 private static String pubTopic = "a/b/c";49
50 /**MQTT-Client*/
51 private staticMqttClient client;52
53 /**证书路径*/
54 private static String caPath = "E:\\mqtt-demo\\certfile\\CA.crt";55
56 /**
57 *@throwsInterruptedException58 *@throwsMqttException*/
59 public static void main(String[] args) throwsInterruptedException, MqttException {60
61 //订阅消息的方法
62 subscribe();63 //
64 publish();65 }66
67 /**
68 *69 * 描述:订阅信息70 *@authormao2080@sina.com71 * @created 2017年7月4日 下午4:53:4772 *@since
73 *@return
74 */
75 public static voidsubscribe() {76 try{77 //创建MqttClient
78 MQTTTest_wss.getClient().setCallback(newMqttCallback() {79
80 public voidconnectionLost(Throwable arg0) {81
82 }83
84 public void messageArrived(String topic, MqttMessage message) throwsException {85 System.out.println("MQTT Rece:" +message.toString());86 }87
88 public voiddeliveryComplete(IMqttDeliveryToken token) {89
90 }91
92 });93 MQTTTest_wss.getClient().subscribe(subTopic, 0);94 System.out.println("连接状态:" +client.isConnected());95 } catch(Exception e) {96 e.printStackTrace();97 }98 }99
100 /**
101 *102 * 描述:获取MqttClient103 *@authormao2080@sina.com104 * @created 2017年7月6日 上午9:56:37105 *@since
106 *@return
107 *@throwsMqttException108 */
109 public static MqttClient getClient() throwsMqttException{110 try{111 if(client == null){112 client = newMqttClient(host, clientId);113 MqttConnectOptions conOptions = newMqttConnectOptions();114 conOptions.setUserName(username);115 conOptions.setPassword(password.toCharArray());116 conOptions.setCleanSession(true);117 conOptions.setSocketFactory(getSSLSocktet(caPath));118 client.connect(conOptions);119 }120 if(!client.isConnected()){121 client.reconnect();122 }123 } catch(Exception e) {124 e.printStackTrace();125 }126 returnclient;127 }128
129 /**
130 *131 * 描述:发布信息132 *@authormao2080@sina.com133 *@throwsMqttException134 * @created 2017年7月4日 下午4:53:32135 *@since
136 */
137 public static void publish() throwsMqttException {138 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");139 String sendMsg = "{time:"+sdf.format(new Date())+", content:"+com.lds.iot.common.util.UUIDUtil.getLowerLetterNumber(15)+", from: java console}";140 try{141 MqttTopic topic =MQTTTest_wss.getClient().getTopic(pubTopic);142 MqttMessage message = newMqttMessage(sendMsg.getBytes());143 message.setQos(0);144 topic.publish(message);145 System.out.println("MQTT Send:" +sendMsg);146 } catch(Exception e) {147 e.printStackTrace();148 }149 }150
151 /**
152 * 获取SSLSocketFactory153 *@paramcaPath154 *@return
155 *@throwsException156 */
157 public static SSLSocketFactory getSSLSocktet(String caPath) throws Exception {158 CertificateFactory cAf = CertificateFactory.getInstance("X.509");159 FileInputStream caIn = newFileInputStream(caPath);160 X509Certificate ca =(X509Certificate) cAf.generateCertificate(caIn);161 KeyStore caks =KeyStore.getInstance(KeyStore.getDefaultType());162 caks.load(null, null);163 caks.setCertificateEntry("ca-certificate", ca);164 TrustManagerFactory tmf =TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());165 tmf.init(caks);166 caIn.close();167 SSLContext context = SSLContext.getInstance("TLSv1");168 context.init(null, tmf.getTrustManagers(), null);169 returncontext.getSocketFactory();170 }171
172 }