aws / aws-iot-device-sdk-java

Java SDK for connecting to AWS IoT from a device.
https://aws.amazon.com/iot/sdk/
Apache License 2.0
211 stars 168 forks source link

SDK 1.3.7 IoT client reconnections and timeouts #154

Closed SOLR4189 closed 3 years ago

SOLR4189 commented 3 years ago

I use IoT Rules on CONNECTED/DISCONNECTED topic (from here). So I want to get email when a device is connected or disconnected. On my device I run next code on startup (only on startup):

iotClient = new AWSIotMqttClient(Configuration.IOT_CLIENT_ENDPOINT,
                    deviceId,
                    keyStore,
                    keystorePass);

iotClient.setKeepAliveInterval(1200000); //20 minutes (maximum)
iotClient.connect();

But I get very strange behavior. I have 3 devices, and on each device I get this stacktrace but due to different reasons:

[pool-8-thread-1] com.amazonaws.services.iot.client.core.AwsIotConnection.onConnectionSuccess Connection successfully established
[pool-8-thread-1] com.amazonaws.services.iot.client.core.AbstractAwsIotClient.onConnectionSuccess Client connection active: <client ID>
[pool-8-thread-1] com.amazonaws.services.iot.client.core.AwsIotConnection.onConnectionFailure Connection temporarily lost
[pool-8-thread-1] com.amazonaws.services.iot.client.core.AbstractAwsIotClient.onConnectionFailure Client connection lost: <client ID>
[pool-8-thread-1] com.amazonaws.services.iot.client.core.AwsIotConnection$1.run Connection is being retried
[pool-8-thread-1] com.amazonaws.services.iot.client.core.AwsIotConnection.onConnectionSuccess Connection successfully established
[pool-8-thread-1] com.amazonaws.services.iot.client.core.AbstractAwsIotClient.onConnectionSuccess Client connection active: <client ID>

Sometimes I get this stacktrace due to DUPLICATE_CLIENTID disconnection reason, or sometimes due to MQTT_KEEP_ALIVE_TIMEOUT disconnection reason (MQTT_KEEP_ALIVE_TIMEOUT happens every 30-35 minutes, DUPLICATE_CLIENTID happens every 10 minutes)

So, I don't understand why do I need to deal with DUPLICATE_CLIENTID if each client has a unique ID, and to deal with MQTT_KEEP_ALIVE_TIMEOUT if there no an intermittent connectivity issue (I get logs every minute to my server, so it isn't WIFI/internet issue). I use the latest AWS IoT SDK from here - https://github.com/aws/aws-iot-device-sdk-java.

How can I solve these issues?

jmklix commented 3 years ago

You can only have one client connected with each client id:

DUPLICATE_CLIENTID | The client is using a client ID that is already in use. In this case, the client that is already connected will be disconnected with this disconnect reason.

For example when you run the PubSub you need to have different -clientId sdk-java. Because when creating a new AWSIotMqttClient you need to have different clientId's to avoid getting disconnected.

Please try fixing that and let me know if you are still getting any other disconnects (MQTT_KEEP_ALIVE_TIMEOUT)

SOLR4189 commented 3 years ago

Each client in my service has a unique CLIENT_ID and function "connect()" is called only once on device startup, so no way the client is using a client ID that is already in use.

jmklix commented 3 years ago

Can you provide a minimum code sample that reproduces this? Also what devices and OS are you testing this on?

SOLR4189 commented 3 years ago

My code (it runs in tomcat on raspberry - OS buster):

iotClient = new AWSIotMqttClient(Configuration.IOT_CLIENT_ENDPOINT,
                    deviceId,
                    keyStore,
                    keystorePass);

iotClient.setKeepAliveInterval(1200000); // or iotClient.setKeepAliveInterval(0);
iotClient.connect();

I'll ask my question in an another way:

What I try to do is to get notifications when a device is connected/disconnected (by this method here)

  1. Can I use AWS IoT Client in JAVA + IoT Rules on CONNECTED/DISCONNECTED topic for automatic notifications if my device is inactive all time through MQTT protocol (but it sends data through HTTPS every time)?
  2. What is it keep-alive=0? Why does it not work how it's mentioned in documentation?

Thanks

jmklix commented 3 years ago
  1. You should be able to do that. You're using AWSIotMqttClient so I'm a little confused when you're saying it sends data through HTTPS, could you please clarify? Below is the topics that get notified when a connection/disconnection occurs.

AWS IoT publishes a message to the following MQTT topics when a client connects or disconnects:

$aws/events/presence/connected/clientId – A client connected to the message broker.

$aws/events/presence/disconnected/clientId – A client disconnected from the message broker.

  1. getKeepAliveInterval:

    Gets the keep-alive interval for the MQTT connection in milliseconds currently configured. Setting this value to 0 will disable the keep-alive function for the connection. The default keep alive interval is 30,000ms.

I think you might be running into the max connection retries which is defaulted to 5. Use iotClient.setMaxConnectionRetries(x); to set a higher number that will allow for longer periods of no connection.

Does this help solve your connection issues and timeouts?

github-actions[bot] commented 3 years ago

Greetings! It looks like this issue hasn’t been active in longer than a week. Because it has been longer than a week since the last update on this, and in the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or add an upvote to prevent automatic closure, or if the issue is already closed, please feel free to open a new one, also please try V2 as this might be solved there too.

SOLR4189 commented 3 years ago

Hello, sorry for a delay.

  1. I mean that my device sends logs over HTTPS every time, so it isn't internet issue.

  2. I think, my client on device didn’t send (or can't send) any ping or MQTT control packets. PINGREQ is for this purpose to avoid disconnection whenever I set keep alive time interval. Maybe you know how can I check it? How can I set more detailed logs?

Thanks

jmklix commented 3 years ago

You might want to check out Cloudwatch. When attaching logs make sure to redact all personal information.

github-actions[bot] commented 3 years ago

Greetings! It looks like this issue hasn’t been active in longer than a week. Because it has been longer than a week since the last update on this, and in the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or add an upvote to prevent automatic closure, or if the issue is already closed, please feel free to open a new one, also please try V2 as this might be solved there too.