MqttAsyncClient Client is not connected (32104)

Viewed 4756

I'm using a MqttAsyncClient in Java. The following code is only a part of how i'm using it, the problem becomes when i'm trying to subscribe to any topic i get the error:

Client is not connected (32104)

    private int connections;
    private String topic;

    private MqttAsyncClient client;
    private MqttConnectOptions connOpts;
    private MemoryPersistence persistence;

private void configureMqtt() 
    {
        try 
        {
            logger.info("Starting the Mqtt Configuration...");

            client = new MqttAsyncClient(conf.getServerURI(), conf.getClientID(), persistence);
            connOpts = new MqttConnectOptions();

            connOpts.setCleanSession(true);
            connOpts.setUserName(conf.getUserName());
            connOpts.setPassword(conf.getPassword().toCharArray());
            connOpts.setAutomaticReconnect(true);

            logger.info("The Mqtt protocol has been configured successfully!!!");
        } 
        catch (Exception e) 
        {
            logger.error("An error has happened during Mqtt configuration");
            logger.error(e);
        }
    }

public void startSub()
{
    try
    {
        logger.info("Conecting to: " + conf.getServerURI() + " Mqtt Server");
        client.connect(connOpts);
        logger.info("Connected to the: "+ conf.getServerURI() + " Mqtt Server");
        client.setCallback(getCallback());
        logger.info("Subscribe to the Topic: " + this.topic);
        //Thread.sleep(1000);
        client.subscribe(this.topic,1);
        logger.info("Successful subscription to topic: " + this.topic);
    }
    catch(Exception me)
    {
        logger.error("An error has happened: " + me.toString());
        logger.error("\nmsg " + me.getMessage() + 
                    "\nloc " + me.getLocalizedMessage() + 
                    "\ncause " + me.getCause() + 
                    "\nexcep " + me);
    }
}

This is the error that i obtained:

[INFO ] 2019-10-12 21:56:37.234 [main] SpManager - Starting the Mqtt Configuration...
[INFO ] 2019-10-12 21:56:37.388 [main] SpManager - The Mqtt protocol has been configured successfully!!!
[INFO ] 2019-10-12 21:56:37.388 [main] SpManager - Conecting to: tcp://0.tcp.ngrok.io:xxxxx Mqtt Server
[INFO ] 2019-10-12 21:56:37.406 [main] SpManager - Connected to the: tcp://0.tcp.ngrok.io:xxxxx Mqtt Server
[INFO ] 2019-10-12 21:56:37.406 [main] SpManager - Subscribe to the Topic: home/Monitoreo/#
[ERROR] 2019-10-12 21:56:37.427 [main] SpManager - An error has happened: Client is not connected (32104)
[ERROR] 2019-10-12 21:56:37.428 [main] SpManager - 
msg Client is not connected
loc Client is not connected

Finally I would like to mention that this code works perfectly when i use MqttClient instead of MqttAsyncClient

1 Answers

The problem is that you are not waiting for the connection to complete before subscribing. This is because the MqttClient blocks where as the MqttAsyncClient returns a token that can be used to check when operations complete.

The quick fix will be to stick a waitForCompletion() in after the connect()

try
{
    logger.info("Connecting to: " + conf.getServerURI() + " Mqtt Server");
    IMqttToken token = client.connect(connOpts);
    token.waitForCompletion();
    logger.info("Connected to the: "+ conf.getServerURI() + " Mqtt Server");
    client.setCallback(getCallback());
    logger.info("Subscribe to the Topic: " + this.topic);
    //Thread.sleep(1000);
    client.subscribe(this.topic, 1);
    logger.info("Successful subscription to topic: " + this.topic);
}
catch(MqttException me)
{

}
Related