1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| package com.qtone.study.mqtt;
import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component public class MqttPushClient { private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);
@Autowired private PushCallback pushCallback;
@Autowired private MqttConfig mqttConfig;
private static MqttClient client;
private static MqttClient getClient() { return client; }
private static void setClient(MqttClient client) { MqttPushClient.client = client; }
public void connect(String host, String clientID, String username, String password, int timeout, int keepalive) { MqttClient client; try { client = new MqttClient(host, clientID, new MemoryPersistence()); MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(false); options.setUserName(username); options.setPassword(password.toCharArray()); options.setConnectionTimeout(timeout); options.setKeepAliveInterval(keepalive); MqttPushClient.setClient(client); try { client.setCallback(pushCallback); client.connect(options); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
public void publish(String topic,String pushMessage){ publish(0, false, topic, pushMessage); }
public void publish(int qos, boolean retained, String topic, String pushMessage) { MqttMessage message = new MqttMessage(); message.setQos(qos); message.setRetained(retained); message.setPayload(pushMessage.getBytes()); MqttTopic mTopic = MqttPushClient.getClient().getTopic(topic); if (null == mTopic) { logger.error("topic not exist"); } MqttDeliveryToken token; try { token = mTopic.publish(message); token.waitForCompletion(); } catch (MqttPersistenceException e) { e.printStackTrace(); } catch (MqttException e) { e.printStackTrace(); } }
public void subscribe(String name,String topic){ logger.info(name +"开始订阅主题" + topic); subscribe(topic,2); }
public void subscribe(String topic, int qos) { try { MqttPushClient.getClient().subscribe(topic, qos); } catch (MqttException e) { e.printStackTrace(); } }
public void unSubscribe(String name,String topic) { logger.info(name +"取消订阅主题" + topic); try { MqttPushClient.getClient().unsubscribe(topic); } catch (MqttException e) { e.printStackTrace(); } }
public synchronized void startReconnect() { if (!client.isConnected()) { while (!client.isConnected()) { logger.info("mqtt开始尝试重连"); try { TimeUnit.SECONDS.sleep(2); mqttConfig.getMqttPushClient(); logger.info("mqtt重连成功"); break; } catch (Exception e) { logger.error("mqtt重连失败,继续重连中"); } } } else { logger.info("mqtt已经连接,无需重连"); } } }
|