java - Eclipse paho Mqtt :Getting java. io.EOF 异常

标签 java mqtt paho

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

Mqtt连接与订阅

我正在使用此代码连接和订阅 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 时遇到了这个错误

错误日志

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

蚊子日志

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 连接到代理,这是不允许的,并且当一个连接时,代理将断开当前连接的客户端。

如果两个客户端都有自动重新连接逻辑,那么它们将继续互相踢掉。

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

关于java - Eclipse paho Mqtt :Getting java. io.EOF 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33998919/

相关文章:

java - 如何在MQTT消息到达之前保持线程

MQTT QoS 降级

go - Paho MQTT golang 用于多个模块?

java - 使用套接字时在 HTML 页面中嵌入 Java JApplet

java - ESAPI日志注入(inject)

python - (Python)如何使用MQTT协议(protocol)订阅Thingspeak.com上的主题并展示?

mqtt - 是否可以在多个使用者之间分发对MQTT主题的阅读?

java - 标记上的类型语法错误、构造错误

Java属性,获取文件路径

makefile - 尝试编译 paho mqtt cpp 示例时找不到 -lpaho-mqttpp3