Skip to content

How does the reconnect for software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder actually work?

0

We way we use use AwsIotMqttConnectionBuilder resembles this sample code

In the github repository I read the following:

    /**
     * Configures the minimum and maximum reconnect timeouts.
     *
     * The time between reconnect attempts will start at min and multiply by 2 until max is reached.
     *
     * @param minTimeoutSecs The timeout to start with
     * @param maxTimeoutSecs The highest allowable wait time between reconnect attempts
     * @return {@link AwsIotMqttConnectionBuilder}
     */
    public AwsIotMqttConnectionBuilder withReconnectTimeoutSecs(long minTimeoutSecs, long maxTimeoutSecs) {

We don't set these and let the connection use whatever defaults and most of the times it does reconnect within minutes but sometimes it just doesn't.

In MqttConnectionConfig I can see that both min and max are set to 0L

    private long minReconnectTimeoutSecs = 0L;
    private long maxReconnectTimeoutSecs = 0L;

Can someone please explain what the default behaviour is and what are the circumstances when it will give up reconnecting. Also what are the expected adjustments that need to be made in order to always reconnect and never give up under any circumstances - it seems just using the defaults is not enough.

1 Answer
1

The default behavior of the AWS IoT MQTT connection builder for reconnection is designed to be resilient, but it may not guarantee indefinite reconnection attempts under all circumstances.

By default, when you don't set specific reconnection timeout values, the SDK uses internal default values. These defaults are not explicitly stated in the provided sources, but they are likely to be reasonable values that work for most use cases.

The reconnection mechanism works as follows:

  1. When a disconnection occurs, the SDK will attempt to reconnect.
  2. The time between reconnection attempts starts at the minimum timeout and doubles after each unsuccessful attempt, up to the maximum timeout.
  3. This exponential backoff strategy helps to avoid overwhelming the server with rapid reconnection attempts.

However, there are circumstances where the connection might not be re-established:

  1. If there are persistent network issues or the IoT Core endpoint is unreachable.
  2. If the device's credentials are no longer valid.
  3. If there are security group or firewall rules blocking the connection.

To make the connection more resilient and attempt to reconnect indefinitely:

  1. Set appropriate minimum and maximum reconnection timeouts:

    builder.withReconnectTimeoutSecs(1, 60);

    This example sets a minimum of 1 second and a maximum of 60 seconds between reconnection attempts.

  2. Implement a custom reconnection strategy in your application logic. This could involve:

    • Monitoring the connection state
    • Implementing your own retry mechanism with longer intervals
    • Logging disconnection events for troubleshooting
  3. Consider implementing a keep-alive mechanism to detect disconnections early:

    builder.withKeepAliveSeconds(30);

    This sets a 30-second keep-alive interval to help detect disconnections more quickly.

  4. Handle disconnection events in your application and initiate a new connection if needed:

    connection.onDisconnection((disconnectionData) -> {
        // Log the disconnection
        // Attempt to create a new connection
    });

Remember that while these strategies can improve resilience, they may not guarantee a connection under all circumstances, especially in cases of persistent network issues or invalid credentials. It's important to implement proper error handling and logging in your application to manage these scenarios effectively.
Sources
https://docs.aws.amazon.com/embedded-csdk/202012.00/lib-ref/libraries/standard/coreMQTT/docs/doxygen/output/html/mqtt_timeouts.html
https://docs.aws.amazon.com/embedded-csdk/202103.00/lib-ref/libraries/standard/coreMQTT/docs/doxygen/output/html/mqtt_timeouts.html
TcpKeepAliveConfiguration.Builder (AWS SDK for Java - 2.21.29)

answered a year ago
EXPERT
reviewed a year ago
  • This does not clarify if it will give up reconnecting under certain circumstances. I was hoping it never gives up but in practice I'm seeing the contrary.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.