Eclipse paho Mqtt:获取java.io.EOF异常 [英] Eclipse paho Mqtt:Getting java.io.EOF Exception

查看:835
本文介绍了Eclipse paho Mqtt:获取java.io.EOF异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试订阅mqtt客户端时,我得到java.io.eof异常。我正在使用eclipse paho库并使用mosquitto broker。我没有得到任何答案,所以请帮助我为什么会这样?

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连接和订阅

我使用此代码连接和订阅mosquitto

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());
            }*/
        }

    }
}

并且mqtt回拨

@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());
 }

我在订阅mosquitto时遇到此错误

I am facing this error when i am subscribing to mosquitto

错误日志

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日志

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.


推荐答案

您有多个客户端连接到具有相同clientid的代理,这是不允许的,并且连接代理将断开当前连接的客户端。

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.

更改其中一个客户端的客户端ID。

Change the client id on one of the clients.

这篇关于Eclipse paho Mqtt:获取java.io.EOF异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆