Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Synchronous connect() hangs

I have tried to implement an Mqtt client basically following the examples given in the source code and was able to get the asynchronous connect to work but not the simpler synchronous connect. I assume I am doing something wrong but I don’t know what.

 

In the asynchronous case my code is as follows:

 

    public APSMqttClient(String mqttServiceUrl, String clientId)

    {

        this.mqttServiceUrl = mqttServiceUrl;

        try

        {

            mqttConnectionOptions = new MqttConnectOptions();

 

            mqttConnectionOptions.setCleanSession(false);

            mqttConnectionOptions.setWill("status",             // Topic to be used when connection is lost

                                          "LOST".getBytes(),    // payload to be sent when connection is lost

                                          2,                    // QoS to be used

                                          true);                // retained flag (true if qos = 2)

            mqttConnectionOptions.setKeepAliveInterval(60);     // time of inactivity before client sends a PING

 

            mqttClient = new MqttAsyncClient(mqttServiceUrl, clientId, new MqttDefaultFilePersistence("./MqttPersistenceData"));

            mqttClient.setCallback(this);

        }

        catch (MqttException e)

        {

            System.out.println("MqttException. Message: " + e.getMessage());

            e.printStackTrace();

        }

    }

 

    public boolean connect()

    {

        System.out.println("Connecting to " + mqttServiceUrl + " with client ID " + mqttClient.getClientId());

        try

        {

            connectAsync();

            return true;

        }

        catch(Throwable e)

        {

            System.out.println("Trying to connect threw an Exception. Message: " + e.getMessage());

            e.printStackTrace();

        }

        return false;

    }

 

    private void connectAsync()

    {

        IMqttActionListener conListener = new IMqttActionListener()

        {

            public void onSuccess(IMqttToken asyncActionToken)

            {

                System.out.println("Connected");

            }

 

            public void onFailure(IMqttToken asyncActionToken, Throwable exception)

            {

                System.out.println ("connect failed" + exception);

            }

        };

   

        try

        {

            // Connect using a non-blocking connect

            mqttClient.connect(mqttConnectionOptions, "Connect sample context", conListener);

        }

        catch (MqttException e)

        {

            System.out.println("MqttException. Message: " + e.getMessage());

            e.printStackTrace();

        }

    }

 

The subscriptions (which are blocking) still appear to work.

 

My synchronous attempt is as follows:

    public APSMqttClient(String mqttServiceUrl, String clientId)

    {

        this.mqttServiceUrl = mqttServiceUrl;

        try

        {

            mqttConnectionOptions = new MqttConnectOptions();

 

            mqttConnectionOptions.setCleanSession(false);

            mqttConnectionOptions.setWill("status",             // Topic to be used when connection is lost

                                          "LOST".getBytes(),    // payload to be sent when connection is lost

                                          2,                    // QoS to be used

                                          true);                // retained flag (true if qos = 2)

            mqttConnectionOptions.setKeepAliveInterval(60);     // time of inactivity before client sends a PING

 

            mqttClient = new MqttClient(mqttServiceUrl, clientId, new MqttDefaultFilePersistence("./MqttPersistenceData"));

            mqttClient.setCallback(this);

        }

        catch (MqttException e)

        {

            System.out.println("MqttException. Message: " + e.getMessage());

            e.printStackTrace();

        }

    }

 

    public boolean connect()

    {

        System.out.println("Connecting to " + mqttServiceUrl + " with client ID " + mqttClient.getClientId());

        try

        {

            // Connect using a blocking connect

            mqttClient.connect(mqttConnectionOptions);

            return true;

        }

        catch(MqttException e)

        {

            System.out.println("Trying to connect threw an Exception. Message: " + e.getMessage());

            e.printStackTrace();

        }

        return false;

    }

 

When I get to the connect() method it hangs. I have stepped through the debugger and it ends up on a wait  for a response in Token.java on line 122 and there it stays.

 

Have I done something stupid that the ‘simpler’ synchronous call doesn’t work and the async call does?

 

Thanks

 

Brian Reinhold

    LNI


Back to the top