当使用Azure IoT SDKs for Java时,我们需要手动处理设备断开连接吗?

huangapple go评论55阅读模式
英文:

When using Azure IoT SDKs for Java, do we need to handle device disconnections manually?

问题

I am currently using Azure IoT SDKs for Java in our Android app.

It works fine and I'm able to connect, send messages and use the direct method callback without issue.

I found that if I turn off the internet, the SDK DeviceClient automatically enters DISCONNECTED_RETRYING status, and when the internet is back on, the status changes back to CONNECTED, which is great. However, I encountered a few times where when I lock my phone screen for a while (to have lunch, etc.) and reopen the app, I am still on the app and I didn't call client.close(), but when I try to send a message, it gives the error Cannot send event from a client that is closed. and seems like the status has changed to DISCONNECTED.

Do we need to listen to connection status events and attempt to reconnect back ourselves? The client does seem to retry to connect back by its own, so I am not sure when is the time we need to write our own reconnect logic?

Thanks

英文:

I am currently using Azure IoT SDKs for Java in our Android app.

It works fine and I'm able to connect, send messages and use the direct method callback without issue.

I found that if I turn off internet, the SDK DeviceClient automatically enters DISCONNECTED_RETRYING status, and when internet is back on, the status changes back to CONNECTED, which is great. However I encountered a few times where when I lock my phone screen for a while (to have lunch etc.) and reopen the app, I am still on the app and I didn't call client.close(), but when I try to send a message, it gives the error Cannot send event from a client that is closed. and seems like the status has changed to DISCONNECTED

Do we need to listen to connection status events and attempt to reconnect back ourselves? The client does seem to retry to connect back by its own, so I am not sure when is the time we need to write our own reconnect logic?

Thanks

答案1

得分: 1

SDK确实具有一些基本的重试逻辑,每当失去连接时,它将自行执行。它使用指数退避算法来决定尝试重新连接的频率,并且默认情况下将尝试最多4分钟。一般来说,默认行为不足以保持“始终连接”。

第一个问题是,有些断开连接的持续时间超过4分钟。如果4分钟过去了,客户端未能自行重新连接,它将执行连接状态回调,状态为DISCONNECTED,并停止重试。

对于简单的情况,您可以通过调用java deviceClient.setOperationTimeout(<新超时值>); 更改4分钟超时。

除此之外,我们建议在设备客户端的顶部设置一些重试逻辑,以应对不寻常的情况。我们维护了一些示例代码,介绍了我们在这方面的建议。

该示例代码相当复杂,对于一些情景可能过于繁琐。如果您的代码相当简单,那么只需在while循环中包装您的sendEventAsync调用即可:

DeviceClient deviceClient = new DeviceClient(...);
deviceClient.setOperationTimeout(Long.MAX_VALUE)
deviceClient.open(true);

bool messageSent = false;
while (!messageSent)
{
	try
	{
		deviceClient.sendEventAsync(...);
		messageSent = true;
	}
	catch (IllegalStateException e)
	{
		// 设备需要重新连接
		deviceClient.open(true);
	}
}
英文:

The SDK does have some basic retry logic that will execute on its own whenever you lose connection. It uses an exponential backoff algorithm to decide how frequently to try to reconnect and, by default, will try for up to 4 minutes. In general, the default behavior isn't enough to stay "always connected".

The first problem is that some disconnections last longer than 4 minutes. If the 4 minutes pass and the client fails to reconnect on its own, it will execute the connection status callback with status DISCONNECTED and stop retrying.

For simpler cases you can alter the 4 minute timeout by calling

deviceClient.setOperationTimeout(&lt;new timeout value&gt;);

Beyond that, we recommend having some retry logic sitting on top of the device client as well to catch for unusual cases. We maintain some sample code of what we recommend in this regard.

That sample code is pretty complex, and may be overkill for some scenarios, though. If your code is pretty simple, then you are fine just wrapping your sendEventAsync call in a while loop like

DeviceClient deviceClient = new DeviceClient(...);
deviceClient.setOperationTimeout(Long.MAX_VALUE)
deviceClient.open(true);

bool messageSent = false;
while (!messageSent)
{
	try
	{
		deviceClient.sendEventAsync(...);
		messageSent = true;
	}
	catch (IllegalStateException e)
	{
		// device needs to reconnect
		deviceClient.open(true);
	}
}

huangapple
  • 本文由 发表于 2023年4月19日 16:29:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定