0

I am using Eclipse Paho Java Client to connect. Here is my extended callback:

protected IMqttAsyncClient mClient;

private final MqttCallbackExtended mCallback = new MqttCallbackExtended() {
    @Override
    public void connectComplete(boolean reconnect, String brokerAddress) {
        Log.d(LOG_TAG, "connectComplete " + brokerAddress);
    }

    @Override
    public void connectionLost(Throwable ex) {
        Log.d(LOG_TAG, "connectionLost", ex);
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken deliveryToken) {
        Log.d(LOG_TAG, "deliveryComplete " + deliveryToken);
    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        Log.d(LOG_TAG, "messageArrived " + topic);
    }
};

And here the connecting code:

protected void connect() throws MqttException {
    Log.d(LOG_TAG, "connect");

    MqttConnectOptions connectOptions = new MqttConnectOptions();
    connectOptions.setCleanSession(true);
    connectOptions.setAutomaticReconnect(false);
    connectOptions.setUserName(MQTT_USERNAME);
    connectOptions.setPassword(MQTT_PASSWORD.toCharArray());

    mClient = new MqttAsyncClient(mBrokerUri, mClientName, new MemoryPersistence());
    mClient.setCallback(mCallback);
    mClient.connect(connectOptions);

    Debug d = ((MqttAsyncClient) mClient).getDebug();
    d.dumpClientDebug();
}

I do not use the automatic reconnect feature, but would like to handle reconnecting in my own custom code, since I need custom delays.

For testing purposes I do not start MQTT broker yet and try to connect.

I was hoping to detect the initial connection failure in the connectionLost callback method, but it does not get called.

The MqttException is not thrown either.

When I inspect the paho0.log.0 log file I see the failed connection there -

FINE     17-03-09  07:55:33.0726    al.TCPNetworkModule  start                      61  ef978a39c826cd6d4ad22f20d5abe6c236eddb060b5d765a1fe2e1d79837fcc8: Failed to create TCP socket
Throwable occurred: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
    at java.lang.Thread.run(Thread.java:745)
FINE     17-03-09  07:55:33.0727    nternal.ClientComms  connectBG:run              61  ef978a39c826cd6d4ad22f20d5abe6c236eddb060b5d765a1fe2e1d79837fcc8: connect failed: unexpected exception
Throwable occurred: Unable to connect to server (32103) - java.net.ConnectException: Connection refused: connect
    at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79)
    at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
    ... 2 more
FINE     17-03-09  07:55:33.0732    nternal.ClientComms  shutdownConnection         61  ef978a39c826cd6d4ad22f20d5abe6c236eddb060b5d765a1fe2e1d79837fcc8: state=DISCONNECTING
FINE     17-03-09  07:55:33.0732    ernal.CommsCallback  stop                       61  ef978a39c826cd6d4ad22f20d5abe6c236eddb060b5d765a1fe2e1d79837fcc8: stopped
FINE     17-03-09  07:55:33.0733    nal.CommsTokenStore  quiesce                    61  ef978a39c826cd6d4ad22f20d5abe6c236eddb060b5d765a1fe2e1d79837fcc8: resp=Client is currently disconnecting (32102)

But how to detect that connection failure in my code? (So that I could initiate the later reconnection).

UPDATE:

Reported this issue as Bug #336

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416

1 Answers1

2

Currently the only way to detect the initial connect failure of an async client is to pass it one more callback:

private final IMqttActionListener mConnectionCallback = new IMqttActionListener() {
    @Override
    public void onSuccess(IMqttToken asyncActionToken) {
        Log.d(LOG_TAG, "onSuccess " + asyncActionToken);
        // do nothing, this case is handled in mCallback.connectComplete method
    }

    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable ex) {
        Log.d(LOG_TAG, "onFailure " + asyncActionToken, ex);
        // initial connect has failed
    }
};

mClient = new MqttAsyncClient(mBrokerUri, mClientName, new MemoryPersistence());
mClient.setCallback(mCallback);
mClient.connect(connectOptions, null, mConnectionCallback);
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416