4

I am getting "java.io.eof" exception,when i am trying to subscribe mqtt client. I am using eclipse paho library and using mosquitto broker. I am not getting any answer of this,so please help me why this happens ?

Mqtt connection and subscribe

I am using this code for connecting and subscribing to mosquitto

private void buildClient(String clientId){
    log.debug("Connecting... "+clientId);
        try {
            mqttClient = new MqttClient(envConfiguration.getBrokerUrl(), clientId,new MemoryPersistence());
            System.out.println(mqttClient.isConnected());
        } catch (MqttException e) {
            log.debug("build client stopped due to "+e.getCause());
        }

        chatCallback = new ChatCallback(this.userService,this);
        mqttClient.setCallback(chatCallback);
        mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setCleanSession(true);
    }

    @Override
    public void connect(String clientId,String topic) {

        try{
            if(mqttClient == null || !mqttClient.getClientId().equals(clientId)){
                buildClient(clientId);
                mqttClient.connect(mqttConnectOptions);
                subscribe(clientId,topic);
            } 
        }catch (Exception e) {
            log.debug("connection attempt failed "+ e.getCause() + " trying...");
        }
    }

    @Override
    public void subscribe(String clientId,String topic) throws MqttException {
        if(mqttClient != null && mqttClient.isConnected()){

            mqttClient.subscribe(topic,0);
            /*try {
                log.debug("Subscribing... with client id :: " + clientId + "topic");
                mqttClient.subscribe(topic,2);
            } catch (MqttException e) {
                log.debug("subscribing error.."+e.getLocalizedMessage());
            }*/
        }

    }
}

And mqtt call back

@Override
public void connectionLost(Throwable arg0) {
    log.debug("Connection lost... attampting retrying due to "
                    + arg0);
    arg0.printStackTrace();
    // chatServiceimpl.connect();

}

@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
    log.debug("delivered message" + arg0);
    // TODO Auto-generated method stub

}

@Override
public void messageArrived(String arg0, MqttMessage arg1) throws Exception {

    log.debug("Message recived..." + arg1.toString());
    userService.saveChat(arg1.toString());
 }

I am facing this error when i am subscribing to mosquitto

Error logs

2015-11-30/18:19:00.877 [MQTT Call: 25287] DEBUG c.s.s.ChatCallback: Message recived...{ "id":"37153topic25287T1448886285.79573", "from":"37153", "to":"25287", "chatBody":[{"type": "text", "message":"The fact "}]}
2015-11-30/18:19:00.878 [MQTT Call: 25287] DEBUG c.s.s.u.UserService: Saving chat...
2015-11-30/18:19:00.883 [MQTT Call: 25287] DEBUG c.s.s.u.UserService: Get user by id::37153
2015-11-30/18:19:00.885 [MQTT Call: 25287] DEBUG c.s.s.u.UserService: Get user by id::25287
2015-11-30/18:19:00.886 [MQTT Rec: 25287] DEBUG c.s.s.ChatCallback: Connection lost... attampting retrying due to Connection lost (32109) - java.io.EOFException
Connection lost (32109) - java.io.EOFException
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:138)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
    at java.io.DataInputStream.readByte(DataInputStream.java:267)
    at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:56)
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:100)
    ... 1 more

Mosquitto Logs

1448889230: Client 25287 disconnected.
1448889230: New client connected from 192.168.2.63 as 25287 (c0, k60).
1448889231: New connection from 192.168.2.242 on port 1883.
1448889231: Client 25287 already connected, closing old connection.
1448889231: Client 25287 disconnected.
1448889231: New client connected from 192.168.2.242 as 25287 (c1, k60).
1448889231: New connection from 192.168.2.63 on port 1883.
1448889231: Client 25287 already connected, closing old connection.
1448889231: Client 25287 disconnected.
1448889231: New client connected from 192.168.2.63 as 25287 (c0, k60).
1448889269: New connection from 192.168.2.242 on port 1883.
Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
Prabjot Singh
  • 4,491
  • 8
  • 31
  • 51

2 Answers2

20

You have multiple clients connecting to the broker with the same clientid, this is not allowed and as one connects the broker will disconnect the currently connected client.

If both clients have automatic reconnection logic then they will just continue to kick each other off.

Change the client id on one of the clients.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Please one more question,as you saw in my code mqtt client is global,so how can i find this client is already connected. And what is the limit of client connected per broker ? – Prabjot Singh Nov 30 '15 at 13:29
  • You can not see what clients are connected broker, every client needs a unique id. The limit on connected clients is resource dependant on the broker host. – hardillb Nov 30 '15 at 13:33
  • What it mean,i don't understand. please could you tell me in details ? i am worrying about these kind of things in production. – Prabjot Singh Nov 30 '15 at 13:37
  • i facing one more problem here,i want to implement clear chat functionality in my application. but when i subscribe to topic,every time,it is giving me last message.so how can i stop this. please help – Prabjot Singh Dec 08 '15 at 07:42
  • but i used MqttClient.generateClientId() and it generates unique id every time. i am still getting the error – M. Usman Khan Oct 15 '16 at 12:08
  • I have the same issue, I used MqttClient.generateClientId() and it generates unique id every time and I'm still getting the error, Are you solved the problem??? @M.UsmanKhan – Moti Aug 08 '19 at 15:29
  • @Moti It's too old for me to recall.. sorry – M. Usman Khan Aug 08 '19 at 18:52
0

As hardillb mentioned above, you have multiple clients connecting. Server (mosquitto) will disconnect the old connection when it receives a connect request from the same client again.

use the isConnected() method on MqttClient object to know if its connected. for eg.

if (! m_client.isConnected()) {
  // reconnect
}
donnie
  • 2,981
  • 2
  • 16
  • 24