网络丢失时重新订阅MQTT主题



我开发了一个Quarkus应用程序,我想用它来接收和处理MQTT消息。到目前为止,这也是有效的。

我的问题是,当MQTT代理上的互联网断开,应用程序随后重新连接时,应用程序会重新连接到代理,但没有收到任何消息。我认为;订阅";方法不再被调用。

我该如何解决这个问题?

这是我的配置:

mp.messaging.incoming.smarthome/electricity.connector=smallrye-mqtt
mp.messaging.incoming.smarthome/electricity.host=192.168.1.88
mp.messaging.incoming.smarthome/electricity.port=1883
mp.messaging.incoming.smarthome/electricity.reconnect-attempts=3000
mp.messaging.incoming.smarthome/electricity.reconnect-interval-seconds=10
mp.messaging.incoming.smarthome/electricity.qos=1
mp.messaging.incoming.smarthome/electricity.failure-strategy=ignore

这是我的控制器:

@Incoming("smarthome/electricity")
public void consume(byte[] raw) {
String price = new String(raw,StandardCharsets.UTF_8);
String[] parts = price.split(",");
String watt = parts[0].trim();
String timeStamp = parts[1].trim();
byte wattH = Byte.parseByte(watt.replace("WH", ""));
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Vienna"))
.withHour(Integer.parseInt(timeStamp.split(":")[0]))
.withMinute(Integer.parseInt(timeStamp.split(":")[1]));
Message message = new Message(wattH,now);
System.out.println(message);
service.addToPackage(message);
scheudler.check();
}

如果我切断连接,堆栈输出:

2022-09-20 07:50:09,683 ERROR [io.sma.rea.mes.mqtt] (vert.x-eventloop-thread-0) SRMSG17105: Unable to establish a connection with the MQTT broker: java.net.SocketException: Connection reset

如果连接恢复:

2022-09-20 07:50:26,751 INFO  [io.ver.mqt.imp.MqttClientImpl] (vert.x-eventloop-thread Connection with 192.168.1.88:1883 established successfully

因此,连接似乎恢复了,但没有更多的传入消息。

我自己解决了这个问题。我设置:

quarkus.arc.remove-unused-beans=none

现在它运行良好。我尝试了很多方法来解决这个问题,但这似乎就是问题所在。我认为当连接丢失太长时间时,运行时会删除一些bean。

如果有人能解释为什么会发生这种情况,请告诉我

最新更新