Search code examples
mqttquarkussmallrye-reactive-messaging

Resubscribing to MQTT Topic when Network ist Lost


I have developed a Quarkus app with which I want to receive and process MQTT messages. This also works so far.

My problem is that when the internet goes down at the MQTT broker and the app reconnects afterwards, the app reconnects to the broker but no messages are received. I think that the "subscribe" method is not called anymore.

How can I solve this problem?

Here is my Config:

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

Here is my Controller:

@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();
    }

Stack Output if i cut the Connection:

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

If the Connection is back:

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

So the connection seems to be back, but there are no more incoming messages.


Solution

  • I solved the Problem by myself. I set :

    quarkus.arc.remove-unused-beans=none
    

    And now it works fine. I tried many ways to fix the problem, but this seems to be the issue. I think there is some bean removed in the runtime when the connection is lost for a too long time.

    If anyone can explain why this happens please tell me