I have an iot edge module in java developed with VS code. Up to now everything was working (meaning: I can send messages to the iot hub without problems). Now I want to add the option to receive messages from the iot hub. As this is not directly implemented, I am subscribing for direct method calls. This is seemingly not working correctly in my case. But, as soon as I remove the subscription to the method, everything works fine. So, the act of subscription seems to break something.
When I add the code to subscribe for direct method calls, the module still can send messages to the iot hub, but the connection gets lost and re-established all the time in 5 second intervals. After some time (about 5 minutes) I see a message from the iotHubEventCallback (ERROR):
230162 [MQTT Rec: myEdgeDevice/JavaModule] WARN HbIIoTGateway - Connection Status: Retrying
230995 [MQTT Rec: myEdgeDevice/JavaModule] WARN HbIIoTGateway - Connection Status: Connected
240196 [MQTT Rec: myEdgeDevice/JavaModule] WARN HbIIoTGateway - Connection Status: Retrying
240994 [MQTT Rec: myEdgeDevice/JavaModule] WARN HbIIoTGateway - Connection Status: Connected
250228 [MQTT Rec: myEdgeDevice/JavaModule] WARN HbIIoTGateway - Connection Status: Retrying
250229 [azure-iot-sdk-IotHubSendTask] INFO HbIIoTGateway - Direct method # IoT Hub responded to device method acknowledgement with status: ERROR
251005 [MQTT Rec: myEdgeDevice/JavaModule] WARN HbIIoTGateway - Connection Status: Connected
After that, the connection remains stable. But, I cannot invoke method calls (e.g. from the azure portal), they get a timeout:
Failed to invoke device method: {"message":"GatewayTimeout:{\r\n \"Message\": \"{\\"errorCode\\":504101,\\"trackingId\\":\\"3558e6feadd54b5c9f248bdbf20bd5e0-G:19-TimeStamp:04/26/2019 05:20:00\\",\\"message\\":\\"Timed out waiting for the response from device.\\",\\"info\\":{\\"timeout\\":\\"00:00:10\\"},\\"timestampUtc\\":\\"2019-04-26T05:20:00.6977425Z\\"}\",\r\n \"ExceptionMessage\": \"\"\r\n}"}
The status of the iotedge service seems unproblematic:
Apr 26 05:21:09 peers_docker_dev iotedged[52836]: 2019-04-26T05:21:09Z [INFO] - Checking edge runtime status
Apr 26 05:21:09 peers_docker_dev iotedged[52836]: 2019-04-26T05:21:09Z [INFO] - Edge runtime is running.
Apr 26 05:21:10 peers_docker_dev iotedged[52836]: 2019-04-26T05:21:10Z [INFO] - [mgmt] - - - [2019-04-26 05:21:10.206275755 UTC] "GET /modules?api-version=2018-06-28 HTTP/1.1" 200 OK 1483 "-" "-" pid(52
This is the code in question:
m_client = new ModuleClient(sConnString, IotHubClientProtocol.MQTT);
// Set the callback for messages
m_client.setMessageCallback(INPUT_NAME, new MessageCallback()
{
public IotHubMessageResult execute(Message msg, Object context)
{
App.m_logger.info("Received message from hub: " + new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET));
return IotHubMessageResult.COMPLETE;
}
}, m_client);
// Register the callback for connection state change
m_client.registerConnectionStatusChangeCallback(new IotHubConnectionStatusChangeCallback ()
{
public void execute(IotHubConnectionStatus status, IotHubConnectionStatusChangeReason statusChangeReason, Throwable throwable, Object callbackContext)
{
String statusStr = "Connection Status: %s";
switch (status)
{
case CONNECTED:
App.m_logger.warn(String.format(statusStr, "Connected"));
break;
case DISCONNECTED:
App.m_logger.error(String.format(statusStr, "Disconnected"));
if (throwable != null)
{
throwable.printStackTrace();
}
break;
case DISCONNECTED_RETRYING:
App.m_logger.warn(String.format(statusStr, "Retrying"));
break;
default:
break;
}
}
}, null);
// Open client
m_client.open();
// Register to receive direct method calls.
m_client.subscribeToMethod(new DeviceMethodCallback() {
@Override
public DeviceMethodData call(String methodName, Object methodData, Object context) {
App.m_logger.info("Method called:" + methodName);
return new DeviceMethodData(METHOD_SUCCESS, "Executed direct method " + methodName);
}
}, null, new IotHubEventCallback()
{
public void execute(IotHubStatusCode status, Object context)
{
App.m_logger.info("Direct method # IoT Hub responded to device method acknowledgement with status: " + status.name());
}
}, null);
The actual output is shown above. I would like to have a stable connection and receive method calls. Any ideas what I might be doing wrong?
Thanks for your help!