当前位置: 首页 > 知识库问答 >
问题:

(重新)连接设备失败,出现订阅异常

谷泽宇
2023-03-14

我正在使用azure iot设备SDK(1.3.31)连接模拟设备。对于某些设备,我在进行此调用时会出现此异常:

//pseudo code    
deviceInfo.device = registryManager.getDevice(deviceId);
String deviceConnectionString = new StringBuilder()
                .append("HostName=").append(hubHostname)
                .append(";DeviceId=").append(device.getDeviceId())
                .append(";SharedAccessKey=").append(device.getPrimaryKey())
                .toString();

DeviceClient deviceClient = new DeviceClient(deviceConnectionString, IotHubClientProtocol.MQTT);

deviceClient.open();
java.io.IOException: Unable to subscribe to topic :devices/sim3_0001/messages/devicebound/# because java.io.EOFExceptionConnection lost
    at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection.open(MqttIotHubConnection.java:142) ~[iot-device-client-1.2.30.jar:na]
    at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttTransport.open(MqttTransport.java:83) ~[iot-device-client-1.2.30.jar:na]
    at com.microsoft.azure.sdk.iot.device.DeviceIO.open(DeviceIO.java:212) ~[iot-device-client-1.2.30.jar:na]
    at com.microsoft.azure.sdk.iot.device.DeviceClient.open(DeviceClient.java:188) ~[iot-device-client-1.2.30.jar:na]
Connection lost (32109) - java.io.EOFException
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
    at java.lang.Thread.run(Thread.java:745)
Lost connection to the server. Reconnecting 0 time.
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:65)
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
    body: 
  protocol:      Mqtt
  authType:      { "scope": "device", "type": "sas", "issuer": "iothub" }
  time:          2017-06-13T21:13:48.2406702Z
  operationName: deviceConnect
  category:      Connections
  level:         Information
  deviceId:      sim3_0001
  ipAddress:     xx.xx.xxx.XXX
enqueuedTimeUtc:       Tue Jun 13 2017 17:13:53 GMT-0400 (EDT)
offset:                355136
applicationProperties: 
  category:      Connections
  level:         Information
  operationName: deviceConnect
sequenceNumber:        705
annotations: 
  x-opt-sequence-number: 705
  x-opt-offset:          355136
  x-opt-enqueued-time:   Tue Jun 13 2017 17:13:53 GMT-0400 (EDT)
offset:                355536
enqueuedTimeUtc:       Tue Jun 13 2017 17:13:53 GMT-0400 (EDT)
body: 
  protocol:          Mqtt
  authType:          { "scope": "device", "type": "sas", "issuer": "iothub" }
  time:              2017-06-13T21:13:48.3656729Z
  operationName:     deviceDisconnect
  category:          Connections
  level:             Error
  statusCode:        404
  statusType:        404104
  statusDescription: DeviceConnectionClosedRemotely
  deviceId:          sim3_0001
  ipAddress:         xx.xx.xxx.XXX
annotations: 
  x-opt-sequence-number: 706
  x-opt-offset:          355536
  x-opt-enqueued-time:   Tue Jun 13 2017 17:13:53 GMT-0400 (EDT)
applicationProperties: 
  category:      Connections
  level:         Error
  operationName: deviceDisconnect
sequenceNumber:        706

另一个注意:新设备连接/订阅成功,似乎它们在重新连接时更经常失败。如果我将打开的呼叫放在一个循环中,并在睡眠1秒后重试,设备最终会成功连接。

共有1个答案

仲孙宇定
2023-03-14

根据您的错误信息java.io.ioException:无法订阅topic:devices/sim3_0001/messages/devicebound/#因为java.io.eofexceptionConnection丢失,我认为您试图通过使用devices/{device_id}/messages/devicebound/#作为主题筛选器订阅云到设备消息。Azure offical文档的将设备应用程序从AMQP迁移到MQTT&接收云到设备消息使用MQTT协议与IoT hub通信这一小节解释了以下问题的原因。

接收云到设备消息

要从IoT Hub接收消息,设备应该使用devices/{device_id}/messages/devicebound/#作为主题筛选器进行订阅。主题筛选器中的多级通配符#仅用于允许设备接收主题名称中的其他属性。IoT集线器不允许使用#或?子主题筛选的通配符。由于IoT Hub不是一个通用的pub-sub消息传递代理,它只支持文档化的主题名称和主题过滤器。

对于Azure IoT Hub,“MQTT不支持放弃/拒绝,所以我们将只显示从IoTHub收到的消息,并返回完成”。GitHub上有一个官方示例代码,您可以参考它来更改代码并使其与MQTT一起工作。

 类似资料: