|
|
|
@ -34,10 +34,11 @@ public class MqttUtil {
|
|
|
|
|
@Value("${mqtt.qos}")
|
|
|
|
|
private int qos;
|
|
|
|
|
|
|
|
|
|
private MqttClient client;
|
|
|
|
|
|
|
|
|
|
public boolean publish2MQTT(String content) {
|
|
|
|
|
MqttClient client;
|
|
|
|
|
MqttDeliveryToken token;
|
|
|
|
|
private MqttDeliveryToken token;
|
|
|
|
|
|
|
|
|
|
public void connect(){
|
|
|
|
|
try {
|
|
|
|
|
client = new MqttClient(broker, publishClientid, new MemoryPersistence());
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
@ -48,37 +49,83 @@ public class MqttUtil {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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){
|
|
|
|
|
MqttMessage message = new MqttMessage(content.getBytes());
|
|
|
|
|
message.setQos(qos);
|
|
|
|
|
// publish message
|
|
|
|
|
try {
|
|
|
|
|
token = client.getTopic(topic).publish(message);
|
|
|
|
|
token.waitForCompletion();
|
|
|
|
|
//打印发送状态
|
|
|
|
|
// log.info("messagei is published completely! {}", token.isComplete());
|
|
|
|
|
// client.publish(topic, message);
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
// disconnect
|
|
|
|
|
try {
|
|
|
|
|
client.disconnect();
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
// close client
|
|
|
|
|
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
|
|
|
|
|
try {
|
|
|
|
|
client.close();
|
|
|
|
|
token = client.getTopic(topic).publish(message);
|
|
|
|
|
token.waitForCompletion();
|
|
|
|
|
} 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();
|
|
|
|
|
}
|
|
|
|
|