英文:
java.io.EOFException (32109) when running Eclipse Paho simulator with example Hono client
问题
我正在使用使用Java Eclipse Paho库的MQTT模拟器脚本。根据Hono入门指南中的说明,我已经完成了设备注册,并手动设置了设备ID、租户ID,然后使用git bash终端启动了示例hono客户端,并运行了这个模拟器代码。
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class MqttPublishHeartRate {
    public static void main(String[] args) {
        String topic = "telemetry";
        String content;
        // int qos = 2;
        String mqttAdaptorIp = "tcp://10.103.102.133:1883";
        String tenantId = "f9ea5afe-2c11-4ab8-aa72-f2dd5df028a9";
        String deviceId = "713af6cc-f06c-4b1d-bfd2-2a2e0811a35e";
        String password = "password";
        MemoryPersistence persistence = new MemoryPersistence();
        try {
            while (true) {
                wait(10000);
                content = "{\"heartrate\":" + heartRateRandom() + "}";
                MqttClient sampleClient = new MqttClient(mqttAdaptorIp, tenantId, persistence);
                MqttConnectOptions connOpts = new MqttConnectOptions();
                connOpts.setUserName(deviceId);
                connOpts.setPassword(password.toCharArray());
                connOpts.setKeepAliveInterval(15);
                connOpts.setConnectionTimeout(30);
                System.out.println("Connecting to broker: " + mqttAdaptorIp);
                sampleClient.connect(connOpts);
                System.out.println("Connected");
                System.out.println("Publishing message: " + content);
                MqttMessage message = new MqttMessage(content.getBytes());
                // message.setQos(qos);
                sampleClient.publish(topic, message);
                System.out.println("Message published");
                sampleClient.disconnect();
                System.out.println("Disconnected");
            }
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }
    }
    
    public static void wait(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
    
    public static int heartRateRandom() {
        int heartRate = (int) Math.floor(Math.random() * 40) + 60;
        return heartRate;
    }
}
但是我收到了这个异常:
Connecting to broker: tcp://10.103.102.133:1883
reason 32109
msg Connection lost
loc Connection lost
cause java.io.EOFException
excep Connection lost (32109) - java.io.EOFException
Connection lost (32109) - java.io.EOFException
	at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:181)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.io.EOFException
	at java.base/java.io.DataInputStream.readByte(DataInputStream.java:272)
	at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:92)
	at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:133)
	... 6 more
我的Hono设置在本地minikube中,这是从kubectl get service获取的输出。
c:\Users\HP>kubectl get service -n hono
NAME                                            TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)                           AGE
eclipse-hono-adapter-amqp-vertx                 LoadBalancer   10.107.174.140   10.107.174.140   5672:32672/TCP,5671:32671/TCP     3d17h
eclipse-hono-adapter-amqp-vertx-headless        ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-adapter-http-vertx                 LoadBalancer   10.101.233.249   10.101.233.249   8080:30080/TCP,8443:30443/TCP     3d17h
eclipse-hono-adapter-http-vertx-headless        ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-adapter-mqtt-vertx                 LoadBalancer   10.103.102.133   10.103.102.133   1883:31883/TCP,8883:30883/TCP     3d17h
eclipse-hono-adapter-mqtt-vertx-headless        ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-artemis                            ClusterIP      10.105.136.215   <none>           5671/TCP                          3d17h
eclipse-hono-dispatch-router                    ClusterIP      10.103.15.52     <none>           5673/TCP                          3d17h
eclipse-hono-dispatch-router-ext                LoadBalancer   10.104.151.34    10.104.151.34    15671:30671/TCP,15672:30672/TCP   3d17h
eclipse-hono-grafana                            ClusterIP      10.110.248.10    <none>           3000/TCP                          3d17h
eclipse-hono-prometheus-server                  ClusterIP      10.110.243.239   <none>           9090/TCP                          3d17h
eclipse-hono-service-auth                       ClusterIP      10.105.228.68    <none>           5671/TCP                          3d17h
eclipse-hono-service-auth-headless              ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-service-device-
<details>
<summary>英文:</summary>
I am using MQTT simulator script that uses the Java [Eclipse Paho][1] library. I did the device registration, set the device-id, tenant-id manually using the git bash terminal as per the instructions in the [Hono getting started guide][2], started the example hono client also with the instructions from the getting started guide and then ran this simulator code.
```java
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class MqttPublishHeartRate {
    public static void main(String[] args) {
        String topic = "telemetry";
        String content;
        // int qos = 2;
        String mqttAdaptorIp = "tcp://10.103.102.133:1883";
        String tenantId = "f9ea5afe-2c11-4ab8-aa72-f2dd5df028a9";
        String deviceId = "713af6cc-f06c-4b1d-bfd2-2a2e0811a35e";
        String password = "password";
        MemoryPersistence persistence = new MemoryPersistence();
        try {
            while (true) {
                wait(10000);
                content = "{\"heartrate\":" + heartRateRandom() + "}";
                MqttClient sampleClient = new MqttClient(mqttAdaptorIp, tenantId, persistence);
                MqttConnectOptions connOpts = new MqttConnectOptions();
                connOpts.setUserName(deviceId);
                connOpts.setPassword(password.toCharArray());
                connOpts.setKeepAliveInterval(15);
                connOpts.setConnectionTimeout(30);
                System.out.println("Connecting to broker: " + mqttAdaptorIp);
                sampleClient.connect(connOpts);
                System.out.println("Connected");
                System.out.println("Publishing message: " + content);
                MqttMessage message = new MqttMessage(content.getBytes());
                // message.setQos(qos);
                sampleClient.publish(topic, message);
                System.out.println("Message published");
                sampleClient.disconnect();
                System.out.println("Disconnected");
            }
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }
    }
    
    public static void wait(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
    
    public static int heartRateRandom() {
        int heartRate = (int) Math.floor(Math.random() * 40) + 60;
        return heartRate;
    }
}
But I get this exception:
Connecting to broker: tcp://10.103.102.133:1883
reason 32109
msg Connection lost
loc Connection lost
cause java.io.EOFException
excep Connection lost (32109) - java.io.EOFException
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:181)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.io.EOFException
at java.base/java.io.DataInputStream.readByte(DataInputStream.java:272)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:92)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:133)
... 6 more
My Hono setup is in a local minikube and this is the output from kubectl get service.
c:\Users\HP>kubectl get service -n hono
NAME                                            TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)                           AGE
eclipse-hono-adapter-amqp-vertx                 LoadBalancer   10.107.174.140   10.107.174.140   5672:32672/TCP,5671:32671/TCP     3d17h
eclipse-hono-adapter-amqp-vertx-headless        ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-adapter-http-vertx                 LoadBalancer   10.101.233.249   10.101.233.249   8080:30080/TCP,8443:30443/TCP     3d17h
eclipse-hono-adapter-http-vertx-headless        ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-adapter-mqtt-vertx                 LoadBalancer   10.103.102.133   10.103.102.133   1883:31883/TCP,8883:30883/TCP     3d17h
eclipse-hono-adapter-mqtt-vertx-headless        ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-artemis                            ClusterIP      10.105.136.215   <none>           5671/TCP                          3d17h
eclipse-hono-dispatch-router                    ClusterIP      10.103.15.52     <none>           5673/TCP                          3d17h
eclipse-hono-dispatch-router-ext                LoadBalancer   10.104.151.34    10.104.151.34    15671:30671/TCP,15672:30672/TCP   3d17h
eclipse-hono-grafana                            ClusterIP      10.110.248.10    <none>           3000/TCP                          3d17h
eclipse-hono-prometheus-server                  ClusterIP      10.110.243.239   <none>           9090/TCP                          3d17h
eclipse-hono-service-auth                       ClusterIP      10.105.228.68    <none>           5671/TCP                          3d17h
eclipse-hono-service-auth-headless              ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-service-device-registry            ClusterIP      10.101.64.187    <none>           5671/TCP,8443/TCP                 3d17h
eclipse-hono-service-device-registry-ext        LoadBalancer   10.105.144.218   10.105.144.218   28080:31080/TCP,28443:31443/TCP   3d17h
eclipse-hono-service-device-registry-headless   ClusterIP      None             <none>           <none>                            3d17h
Please let know how to fix this.
答案1
得分: 1
明白了,密码应该采用 auth-id@tenant 的形式,例如 sensor1@DEFAULT_TENANT (https://www.eclipse.org/hono/docs/user-guide/mqtt-adapter/)
所以修改了代码如下:
String tenantId = "DEFAULT_TENANT";
String authId = "sensor1";
String username = authId + "@" + tenantId;
在这里放出来,以防其他人可能会遇到类似情况。
英文:
Figured out, The password should be in the form of auth-id@tenant, e.g. sensor1@DEFAULT_TENANT (https://www.eclipse.org/hono/docs/user-guide/mqtt-adapter/)
So modified the code to:
String tenantId = "DEFAULT_TENANT";
String authId = "sensor1";
String username = authId+"@"+tenantId;
Putting it here just in case anyone else may face this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论