wenhua.zhou 2 years ago
parent 84a7445f25
commit 9b12e12f96

@ -38,73 +38,36 @@ public class MqttUtil {
private MqttDeliveryToken token; private MqttDeliveryToken token;
public void connect(){ public void connect() throws Exception {
try {
client = new MqttClient(broker, publishClientid, new MemoryPersistence()); client = new MqttClient(broker, publishClientid, new MemoryPersistence());
} catch (MqttException e) {
throw new RuntimeException(e);
}
MqttConnectOptions options = new MqttConnectOptions(); MqttConnectOptions options = new MqttConnectOptions();
options.setUserName(username); options.setUserName(username);
options.setPassword(password.toCharArray()); options.setPassword(password.toCharArray());
options.setConnectionTimeout(60); options.setConnectionTimeout(60);
options.setKeepAliveInterval(30); options.setKeepAliveInterval(30);
try {
client.connect(options); client.connect(options);
} catch (MqttException e) {
throw new RuntimeException(e);
}
} }
public void disconnect() { public void disconnect() {
try { try {
client.disconnect(); client.disconnect();
} catch (MqttException e) {
throw new RuntimeException(e);
}
// close client
try {
client.close(); client.close();
} catch (MqttException e) { } 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()); MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos); message.setQos(qos);
// publish message
try {
token = client.getTopic(topic).publish(message); token = client.getTopic(topic).publish(message);
token.waitForCompletion(); token.waitForCompletion();
} catch (MqttException e) {
throw new RuntimeException(e);
}
return token.isComplete(); return token.isComplete();
} }
public boolean publish2MQTT(String content) { 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()); MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos); message.setQos(qos);
// publish message // publish message
@ -114,30 +77,12 @@ public class MqttUtil {
} catch (MqttException e) { } catch (MqttException e) {
throw new RuntimeException(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(); return token.isComplete();
} }
public void subScribeMQTT() { 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 { try {
MqttClient client = new MqttClient(broker, subscribeClientid, new MemoryPersistence()); MqttClient client = new MqttClient(broker, subscribeClientid, new MemoryPersistence());
// connect options // connect options

@ -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;
}
}
Loading…
Cancel
Save