|  |  | 
 |  |  | package com.doumee.core.mqtt.service; | 
 |  |  |  | 
 |  |  | import com.doumee.core.mqtt.config.MqttClientInit; | 
 |  |  | import com.doumee.core.mqtt.config.MqttConfig; | 
 |  |  | import com.doumee.service.business.DeviceService; | 
 |  |  | import com.doumee.service.business.DeviceSubcribeService; | 
 |  |  | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | 
 |  |  | import org.eclipse.paho.client.mqttv3.MqttCallback; | 
 |  |  | import org.eclipse.paho.client.mqttv3.MqttMessage; | 
 |  |  | import org.eclipse.paho.client.mqttv3.*; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  |  | 
 |  |  | 
 |  |  |         public void connectionLost(Throwable cause) { | 
 |  |  |  | 
 |  |  |             // 连接丢失后,一般在这里面进行重连 | 
 |  |  |             System.out.println("连接断开,可以做重连"); | 
 |  |  |             System.out.println("连接断开,重连中"); | 
 |  |  |             try { | 
 |  |  |                 MqttClientInit.refreshClient(); | 
 |  |  |             } catch (MqttException e) { | 
 |  |  |                 throw new RuntimeException(e); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         @Override | 
 |  |  | 
 |  |  |         @Override | 
 |  |  |         public void messageArrived(String topic, MqttMessage message) throws Exception { | 
 |  |  |             // subscribe后得到的消息会执行到这里面 | 
 |  |  |             System.out.println("接收消息主题 : " + topic); | 
 |  |  |             System.out.println("接收消息Qos : " + message.getQos()); | 
 |  |  |             System.out.println("接收消息内容 : " + new String(message.getPayload())); | 
 |  |  |             deviceSubcribeService.listener(new String(message.getPayload()),topic); | 
 |  |  |             try { | 
 |  |  |                 System.out.println("接收消息主题 : " + topic); | 
 |  |  |                 System.out.println("接收消息Qos : " + message.getQos()); | 
 |  |  |                 System.out.println("接收消息内容 : " + new String(message.getPayload())); | 
 |  |  |                 deviceSubcribeService.listener(message.getId()+"",new String(message.getPayload()),topic); | 
 |  |  |             }catch (Exception e){ | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  | } |