diff --git a/src/main/java/com/xydl/util/MqttUtil.java b/src/main/java/com/xydl/util/MqttUtil.java index 25c6c6f..e831336 100644 --- a/src/main/java/com/xydl/util/MqttUtil.java +++ b/src/main/java/com/xydl/util/MqttUtil.java @@ -38,73 +38,36 @@ public class MqttUtil { private MqttDeliveryToken token; - public void connect(){ - try { - client = new MqttClient(broker, publishClientid, new MemoryPersistence()); - } catch (MqttException e) { - throw new RuntimeException(e); - } + public void connect() throws Exception { + client = new MqttClient(broker, publishClientid, new MemoryPersistence()); MqttConnectOptions options = new MqttConnectOptions(); options.setUserName(username); options.setPassword(password.toCharArray()); options.setConnectionTimeout(60); options.setKeepAliveInterval(30); - try { - client.connect(options); - } catch (MqttException e) { - throw new RuntimeException(e); - } + client.connect(options); } - public void disconnect(){ + public void disconnect() { try { client.disconnect(); - } catch (MqttException e) { - throw new RuntimeException(e); - } - // close client - try { client.close(); } catch (MqttException e) { - throw new RuntimeException(e); } + } - public boolean publish(String content){ + public boolean publish(String content) throws Exception { MqttMessage message = new MqttMessage(content.getBytes()); message.setQos(qos); - // publish message - try { - token = client.getTopic(topic).publish(message); - token.waitForCompletion(); - } catch (MqttException e) { - throw new RuntimeException(e); - } + token = client.getTopic(topic).publish(message); + token.waitForCompletion(); return token.isComplete(); } - public boolean publish2MQTT(String content) { -// MqttClient client; -// MqttDeliveryToken token; -// try { -// client = new MqttClient(broker, publishClientid, new MemoryPersistence()); -// } catch (MqttException e) { -// throw new RuntimeException(e); -// } -// MqttConnectOptions options = new MqttConnectOptions(); -// options.setUserName(username); -// options.setPassword(password.toCharArray()); -// options.setConnectionTimeout(60); -// options.setKeepAliveInterval(30); -// // connect -// try { -// client.connect(options); -// } catch (MqttException e) { -// throw new RuntimeException(e); -// } - // create message and setup QoS + MqttMessage message = new MqttMessage(content.getBytes()); message.setQos(qos); // publish message @@ -114,30 +77,12 @@ public class MqttUtil { } catch (MqttException e) { throw new RuntimeException(e); } - // disconnect -// try { -// client.disconnect(); -// } catch (MqttException e) { -// throw new RuntimeException(e); -// } -// // close client -// try { -// client.close(); -// } catch (MqttException e) { -// throw new RuntimeException(e); -// } return token.isComplete(); } public void subScribeMQTT() { -// String broker = "tcp://139.196.211.222:10883"; -// String topic = "mqtt/test"; -// String username = "test"; -// String password = "AliOS%1688"; -// String clientid = "subscribe_client"; -// int qos = 0; try { MqttClient client = new MqttClient(broker, subscribeClientid, new MemoryPersistence()); // connect options diff --git a/src/main/java/com/xydl/util/Subscribe.java b/src/main/java/com/xydl/util/Subscribe.java deleted file mode 100644 index 58c899b..0000000 --- a/src/main/java/com/xydl/util/Subscribe.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.xydl.util; - -import com.xydl.service.impl.MqttServiceImpl; -import lombok.extern.slf4j.Slf4j; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.text.SimpleDateFormat; -import java.util.Date; - -@Component -@Slf4j -public class Subscribe { - - @Autowired - MqttUtil mqttUtil; - - private static Subscribe single = new Subscribe(); - - @PostConstruct - private void SubscribeMqtt() { -// log.info("开始订阅===subScribe执行一次==={}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); -// mqttUtil.subScribeMQTT(); - } - - public static Subscribe getInstance() { - return single; - } - - -}