title: AWS JAVA IoT client reconnections and timeouts
body: <p>I use IoT Rules on CONNECTED/DISCONNECTED topic (from <a href="https://docs.aws.amazon.com/iot/latest/developerguide/iot-lambda-rule.html" rel="nofollow noreferrer">here</a>). 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):</p>
  <pre><code>iotClient = new AWSIotMqttClient(Configuration.IOT_CLIENT_ENDPOINT,
                      deviceId,
                      keyStore,
                      keystorePass);
              
  iotClient.setKeepAliveInterval(1200000); //20 minutes (maximum)
  iotClient.connect();
  </code></pre>
  <p>But I get very strange behavior. I have 3 devices, and on each device I get this stacktrace but due to different reasons:</p>
  <pre><code>[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: &lt;client ID&gt;
  [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: &lt;client ID&gt;
  [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: &lt;client ID&gt;
  </code></pre>
  <p>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)</p>
  <p>So, I don't understand why do I need to deal with DUPLICATE_CLIENTID if <strong>each client has a unique ID</strong>, and to deal with MQTT_KEEP_ALIVE_TIMEOUT if <strong>there no an intermittent connectivity issue</strong> (I get logs every minute to my server, so it isn't WIFI/internet issue). I use the latest AWS IoT SDK from here - <a href="https://github.com/aws/aws-iot-device-sdk-java" rel="nofollow noreferrer">https://github.com/aws/aws-iot-device-sdk-java</a>.</p>
  <p>How can I solve these issues?</p>
  <p><strong>MY TRICKY SOLUTION:</strong></p>
  <p>I added a scheduled thread that sends empty messages to topic - ${iot:Connection.Thing.ThingName}/ping every 20 minutes:</p>
  <pre><code>scheduledExecutor.scheduleAtFixedRate(() -&gt; {
      try {
          iotClient.publish(String.format(Configuration.PING_TOPIC, deviceId), AWSIotQos.QOS0, &quot;&quot;);
      } catch (AWSIotException e) {
          LOGGER.error(&quot;Failed to send ping&quot;, e);
      }
  }, Configuration.PING_INITIAL_DELAY_IN_MINUTES, Configuration.PING_PERIOD_IN_MINUTES, TimeUnit.MINUTES);
  </code></pre>
  <p>So this solution solves inactive issue, but I still want to find a more elegant solution...</p>