|
@@ -32,7 +32,7 @@ public class MqttSubscribeClient implements CommandLineRunner {
|
|
|
if(StringUtils.equals(mqttSwitch,"true")){
|
|
|
log.info("[MqttMsgSubscribe]公共生产者启动开始.");
|
|
|
try{
|
|
|
- client = new MqttClient(commonProperties.getMqttUrl(), "1234567890");
|
|
|
+ client = new MqttClient(commonProperties.getMqttUrl(), "hnzy1234567890");
|
|
|
connOpts = new MqttConnectOptions();
|
|
|
connOpts.setCleanSession(true);
|
|
|
connOpts.setUserName(commonProperties.getMqttName());
|
|
@@ -87,6 +87,41 @@ public class MqttSubscribeClient implements CommandLineRunner {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try{
|
|
|
+ MqttClient client = new MqttClient("tcp://218.2.6.74:13883", "123456789");
|
|
|
+ MqttConnectOptions connOpts = new MqttConnectOptions();
|
|
|
+ connOpts.setCleanSession(true);
|
|
|
+ connOpts.setUserName("bydz-smt");
|
|
|
+ connOpts.setAutomaticReconnect(true);
|
|
|
+ connOpts.setPassword("bydzsmt123!@#".toCharArray());
|
|
|
+ log.info("Connecting to broker: " + "tcp://192.168.25.47:1883");
|
|
|
+ client.connect(connOpts);
|
|
|
+ log.info("Connected");
|
|
|
+ // client.subscribe(ConstantContextHolder.getMqttTopic() + ConstantContextHolder.getSysItemCode(), 2);
|
|
|
+ client.setCallback(new MqttCallback() {
|
|
|
+ @Override
|
|
|
+ public void connectionLost(Throwable cause) {
|
|
|
+ System.out.println("连接丢失: " + cause.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void messageArrived(String topic, MqttMessage message) throws Exception {
|
|
|
+ System.out.println("接收到消息: " + new String(message.getPayload()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deliveryComplete(IMqttDeliveryToken token) {
|
|
|
+ System.out.println("消息发送完成: " + token.isComplete());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }catch (MqttSecurityException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (MqttException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
}
|