1

When i run my code it's working fine but after some hours it stop working and show this error Not able to publish: MQTT client is not connected

    public Mqtt5AsyncClient connect(String host, int port) {
        Mqtt5AsyncClient client = MqttClient.builder().useMqttVersion5()
                .identifier(UUID.randomUUID().toString())
          .serverHost(host)
                .serverPort(port).buildAsync();

        Mqtt5ConnAck connectionAck = null;
        try {
            connectionAck = client.toBlocking().connect();
            Mqtt5ConnAckReasonCode connAckCode = connectionAck.getReasonCode();
            logger.debug("Client connected to broker with url: " + host + ":" + port + " ::Connection ack code: " + " keep alive ");
           
        } catch (Exception ex) {
            logger.debug("Not able to connect to broker with url: " + host + ":" + port);
            ex.printStackTrace();
        }
 return client;
    }
NewbieCoder
  • 117
  • 1
  • 1
  • 6
  • 2
    Add an automatic reconnect handler - https://gitlab.com/bmorris591/air-quality-monitor/-/blob/master/hive-mqtt-client/src/main/kotlin/uk/co/borismorris/dyson/airqualitymonitor/mqtt/hive/NettyDysonMqttClient.kt#L84 – Boris the Spider Nov 17 '21 at 05:24

0 Answers0