Giter Site home page Giter Site logo

aws-iot-device-sdk-java's Introduction

New Version Available

A new AWS IoT Device SDK is now available. It is a complete rework, built to improve reliability, performance, and security. We invite your feedback!

This SDK will no longer receive feature updates, but will receive security updates.

AWS IoT Device SDK for Java

The AWS IoT Device SDK for Java enables Java developers to access the AWS IoT Platform through MQTT or MQTT over the WebSocket protocol. The SDK is built with AWS IoT device shadow support, providing access to thing shadows (sometimes referred to as device shadows) using shadow methods, including GET, UPDATE, and DELETE. It also supports a simplified shadow access model, which allows developers to exchange data with their shadows by just using getter and setter methods without having to serialize or deserialize any JSON documents.

To get started, use the Maven repository or download the latest JAR file.

Overview

This document provides instructions for installing and configuring the AWS IoT device SDK for Java. It also includes some examples that demonstrate the use of different APIs.

MQTT Connection Types

The SDK is built on top of the Paho MQTT Java client library. Developers can choose from two types of connections to connect to the AWS IoT service:

  • MQTT (over TLS 1.2) with X.509 certificate-based mutual authentication
  • MQTT over WebSocket with AWS Signature Version 4 authentication

For MQTT over TLS (port 8883), a valid certificate and private key are required for authentication. For MQTT over WebSocket (port 443), a valid AWS Identity and Access Management (IAM) access key ID and secret access key pair is required for authentication.

Thing Shadows

A thing shadow represents the cloud counterpart of a physical device or thing. Although a device is not always online, its thing shadow is. A thing shadow stores data in and out of the device in a JSON based document. When the device is offline, its shadow document is still accessible to the application. When the device comes back online, the thing shadow publishes the delta to the device (which the device didn't see while it was offline).

The SDK implements the protocol for applications to retrieve, update, and delete shadow documents mentioned here. When you use the simplified access model, you have the option to enable strict document versioning. To reduce the overhead of subscribing to shadow topics for each method requested, the SDK automatically subscribes to all of the method topics when a connection is established.

Simplified Shadow Access Model

Unlike the shadow methods, which operate on JSON documents, the simplified shadow access model allows developers to access their shadows with getter and setter methods.

To use this feature, you must extend the device class AWSIotDevice, use the annotation AWSIotDeviceProperty to mark class member variables to be managed by the SDK, and provide getter and setter methods for accessing these variables. The getter methods will be used by the SDK to report to the shadow periodically. The setter methods will be invoked whenever there is a change to the desired state of the shadow document. For more information, see Use the SDK later in this document.

Install the SDK

Minimum Requirements

To use the SDK, you will need Java 1.7+.

Install the SDK Using Maven

The recommended way to use the AWS IoT Device SDK for Java in your project is to consume it from Maven. Simply add the following dependency to the POM file of your Maven project.

<dependencies>
  <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-iot-device-sdk-java</artifactId>
    <version>1.3.9</version>
  </dependency>
</dependencies>

The sample applications included with the SDK can also be installed using the following dependency definition.

<dependencies>
  <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-iot-device-sdk-java-samples</artifactId>
    <version>1.3.9</version>
  </dependency>
</dependencies>

Install the SDK Using the Latest JAR

The latest JAR files can be downloaded here. You can simply extract and copy the JAR files to your project's library directory, and then update your IDE to include them to your library build path.

You will also need to add two libraries the SDK depends on:

Build the SDK from the GitHub Source

You can build both the SDK and its sample applications from the source hosted at GitHub.

$ git clone https://github.com/aws/aws-iot-device-sdk-java.git
$ cd aws-iot-device-sdk-java
$ mvn clean install -Dgpg.skip=true

Use the SDK

The following sections provide some basic examples of using the SDK to access the AWS IoT service over MQTT. For more information about each API, see the API documentation.

Initialize the Client

To access the AWS IoT service, you must initialize AWSIotMqttClient. The way in which you initialize the client depends on the connection type (MQTT or MQTT over WebSocket) you choose. In both cases, a valid client endpoint and client ID are required for setting up the connection.

  • Initialize the Client with MQTT (over TLS 1.2): For this MQTT connection type (port 8883), the AWS IoT service requires TLS mutual authentication, so a valid client certificate (X.509) and RSA keys are required. You can use the AWS IoT console or the AWS command line tools to generate certificates and keys. For the SDK, only a certificate file and private key file are required.
String clientEndpoint = "<prefix>-ats.iot.<region>.amazonaws.com";   // use value returned by describe-endpoint --endpoint-type "iot:Data-ATS"
String clientId = "<unique client id>";                              // replace with your own client ID. Use unique client IDs for concurrent connections.
String certificateFile = "<certificate file>";                       // X.509 based certificate file
String privateKeyFile = "<private key file>";                        // PKCS#1 or PKCS#8 PEM encoded private key file

// SampleUtil.java and its dependency PrivateKeyReader.java can be copied from the sample source code.
// Alternatively, you could load key store directly from a file - see the example included in this README.
KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(certificateFile, privateKeyFile);
AWSIotMqttClient client = new AWSIotMqttClient(clientEndpoint, clientId, pair.keyStore, pair.keyPassword);

// optional parameters can be set before connect()
client.connect();
  • Initialize the Client with MQTT Over WebSocket: For this MQTT connection type (port 443), you will need valid IAM credentials to initialize the client. This includes an AWS access key ID and secret access key. There are a number of ways to get IAM credentials (for example, by creating permanent IAM users or by requesting temporary credentials through the Amazon Cognito service). For more information, see the developer guides for these services.

As a best practice for application security, do not embed credentials directly in the source code.

String clientEndpoint = "<prefix>-ats.iot.<region>.amazonaws.com";   // use value returned by describe-endpoint --endpoint-type "iot:Data-ATS"
String clientId = "<unique client id>";                              // replace with your own client ID. Use unique client IDs for concurrent connections.

// AWS IAM credentials could be retrieved from AWS Cognito, STS, or other secure sources
AWSIotMqttClient client = new AWSIotMqttClient(clientEndpoint, clientId, awsAccessKeyId, awsSecretAccessKey, sessionToken);

// optional parameters can be set before connect()
client.connect();

Publish and Subscribe

After the client is initialized and connected, you can publish messages and subscribe to topics.

To publish a message using a blocking API:

String topic = "my/own/topic";
String payload = "any payload";

client.publish(topic, AWSIotQos.QOS0, payload);

To publish a message using a non-blocking API:

public class MyMessage extends AWSIotMessage {
    public MyMessage(String topic, AWSIotQos qos, String payload) {
        super(topic, qos, payload);
    }

    @Override
    public void onSuccess() {
        // called when message publishing succeeded
    }

    @Override
    public void onFailure() {
        // called when message publishing failed
    }

    @Override
    public void onTimeout() {
        // called when message publishing timed out
    }
}

String topic = "my/own/topic";
AWSIotQos qos = AWSIotQos.QOS0;
String payload = "any payload";
long timeout = 3000;                    // milliseconds

MyMessage message = new MyMessage(topic, qos, payload);
client.publish(message, timeout);

To subscribe to a topic:

public class MyTopic extends AWSIotTopic {
    public MyTopic(String topic, AWSIotQos qos) {
        super(topic, qos);
    }

    @Override
    public void onMessage(AWSIotMessage message) {
        // called when a message is received
    }
}

String topicName = "my/own/topic";
AWSIotQos qos = AWSIotQos.QOS0;

MyTopic topic = new MyTopic(topicName, qos);
client.subscribe(topic);

Note: all operations (publish, subscribe, unsubscribe) will not timeout unless you define a timeout when performing the operation. If no timeout is defined, then a value of 0 is used, meaning the operation will never timeout and, in rare cases, wait forever for the server to respond and block the calling thread indefinitely. It is recommended to set a timeout for QoS1 operations if your application expects responses within a fixed duration or if there is the possibility the server you are communicating with may not respond.

Shadow Methods

To access a shadow using a blocking API:

String thingName = "<thing name>";                    // replace with your AWS IoT Thing name

AWSIotDevice device = new AWSIotDevice(thingName);

client.attach(device);
client.connect();

// Delete existing shadow document
device.delete();

// Update shadow document
State state = "{\"state\":{\"reported\":{\"sensor\":3.0}}}";
device.update(state);

// Get the entire shadow document
String state = device.get();

To access a shadow using a non-blocking API:

public class MyShadowMessage extends AWSIotMessage {
    public MyShadowMessage() {
        super(null, null);
    }

    @Override
    public void onSuccess() {
        // called when the shadow method succeeded
        // state (JSON document) received is available in the payload field
    }

    @Override
    public void onFailure() {
        // called when the shadow method failed
    }

    @Override
    public void onTimeout() {
        // called when the shadow method timed out
    }
}

String thingName = "<thing name>";      // replace with your AWS IoT Thing name

AWSIotDevice device = new AWSIotDevice(thingName);

client.attach(device);
client.connect();

MyShadowMessage message = new MyShadowMessage();
long timeout = 3000;                    // milliseconds
device.get(message, timeout);

Simplified Shadow Access Model

To use the simplified shadow access model, you need to extend the device class AWSIotDevice, and then use the annotation class AWSIotDeviceProperty to mark the device attributes and provide getter and setter methods for them. The following very simple example has one attribute, someValue, defined. The code will report the attribute to the shadow, identified by thingName every 5 seconds, in the reported section of the shadow document. The SDK will call the setter method setSomeValue() whenever there's a change to the desired section of the shadow document.

public class MyDevice extends AWSIotDevice {
    public MyDevice(String thingName) {
        super(thingName);
    }

    @AWSIotDeviceProperty
    private String someValue;

    public String getSomeValue() {
        // read from the physical device
    }

    public void setSomeValue(String newValue) {
        // write to the physical device
    }
}

MyDevice device = new MyDevice(thingName);

long reportInterval = 5000;            // milliseconds. Default interval is 3000.
device.setReportInterval(reportInterval);

client.attach(device);
client.connect();

Other Topics

Enable Logging

The SDK uses java.util.logging for logging. To change the logging behavior (for example, to change the logging level or logging destination), you can specify a property file using the JVM property java.util.logging.config.file. It can be provided through JVM arguments like so:

-Djava.util.logging.config.file="logging.properties"

To change the console logging level, the property file logging.properties should contain the following lines:

# Override of console logging level
java.util.logging.ConsoleHandler.level=INFO

Load KeyStore from File to Initialize the Client

You can load a KeyStore object directly from JKS-based keystore files. You will first need to import X.509 certificate and the private key into the keystore file like so:

$ openssl pkcs12 -export -in <certificate-file> -inkey <private-key-file> -out p12.keystore -name alias
(type in the export password)

$ keytool -importkeystore -srckeystore p12.keystore -srcstoretype PKCS12 -srcstorepass <export-password> -alias alias -deststorepass <keystore-password> -destkeypass <key-password> -destkeystore my.keystore

After the keystore file my.keystore is created, you can use it to initialize the client like so:

String keyStoreFile = "<my.keystore>";                               // replace with your own key store file
String keyStorePassword = "<keystore-password>";                     // replace with your own key store password
String keyPassword = "<key-password>"                                // replace with your own key password

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(keyStoreFile), keyStorePassword.toCharArray());

String clientEndpoint = "<prefix>.iot.<region>.amazonaws.com";       // replace <prefix> and <region> with your own
String clientId = "<unique client id>";                              // replace with your own client ID. Use unique client IDs for concurrent connections.

AWSIotMqttClient client = new AWSIotMqttClient(clientEndpoint, clientId, keyStore, keyPassword);

Use ECC-Based Certificates

You can use Elliptic Curve Cryptography (ECC)-based certificates to initialize the client. To create an ECC key and certificate, see this blog post. After you have created and registered the key and certificate, use the following command to convert the ECC key file to PKCK#8 format.

$ openssl pkcs8 -topk8 -nocrypt -in ecckey.key -out ecckey-pk8.key
(type in the key password)

You can then use the instruction described in this section to initialize the client with just this one change.

// SampleUtil.java and its dependency PrivateKeyReader.java can be copied from the sample source code.
// Alternatively, you could load key store directly from a file - see the example included in this README.
KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(certificateFile, privateKeyFile, "EC");

Increase in-flight publish limit (too many publishes in Progress error)

If you are getting a too many publishes in Progress error this means that your application has more operations in-flight (meaning they have not succeeded or failed, but they are waiting for a response from the server) than Paho supports by default. By default, the Paho client supports a maximum of 10 in-flight operations.

The recommended way to resolve this issue is to track how many QoS1 operations you have sent that are in-flight and when you reach the limit of 10, you add any further operations into a queue. Then as the QoS1 operations are no longer in-flight you grab QoS1 operations from the queue until it is empty or until you have hit the maximum of 10 in-flight operations. You then repeat this process until all the operations are sent. This will prevent your application from ever trying to send too many operations at once and exceeding the maximum in-flight limit of the Paho client.

Another way to help reduce this issue is to increase the maximum number of in-flight operations that the Paho client can process. To do this, you will need to modify the source code to increase this limit. Download the source code from GitHub, navigate to the AwsIotMqttConnection.java file, and add the following line of code in the buildMqttConnectOptions function just under the line options.setKeepAliveInterval(client.getKeepAliveInterval() / 1000); (around line 151):

options.setMaxInflight(100);

Then compile the source code and use the compiled Jar in your application.

This will increase Paho's in-flight limit to 100 and allow you to have more in-flight at the same time, giving additional room for sending larger volumes of QoS1 operations. Note that these in-flight operations still need to be acknowledged by the server or timeout before they are no longer in-flight, you can just have up to 100 in-flight rather than the default of 10.

For AWS IoT Core, you can only send a maximum of 100 QoS1 operations per second. Any operations sent after the first 100 per second will be ignored by AWS IoT Core. For this reason, it is highly recommended you perform all operations with a timeout if you increase the maximum in-flight limit, to prevent a situation where you send more than 100 QoS1 operations per second and are waiting on an operation to get an acknowledgement from the sever that will never come.

Sample Applications

There are three samples applications included with the SDK. The easiest way to run these samples is through Maven, which will take care of getting the dependencies.

  • Publish/Subscribe sample: This sample consists of two publishers publishing one message per second to a topic. One subscriber subscribing to the same topic receives and prints the messages.

  • Shadow sample: This sample consists of a simple demo of the simplified shadow access model. The device contains two attributes: window state and room temperature. Window state can be modified (therefore, controlled) remotely through desired state. To demonstrate this control function, you can use the AWS IoT console to modify the desired window state, and then see its change from the sample output.

  • Shadow echo sample: This sample consists of a simple demo that uses Shadow methods to send a shadow update and then retrieve it back every second.

Arguments for the Sample Applications

To run the samples, you will also need to provide the following arguments through the command line:

  • clientEndpoint: client endpoint, obtained via calling describe-endpoint
  • clientId: client ID
  • thingName: AWS IoT thing name (not required for the Publish/Subscribe sample)

You will also need to provide either set of the following arguments for authentication. For an MQTT connection, provide these arguments:

  • certificateFile: X.509 based certificate file (For Just-in-time registration, this is the concatenated file from both the device certificate and CA certificate. For more information about Just-in-Time Registration, please see this blog.
  • privateKeyFile: private key file
  • keyAlgorithm: (optional) RSA or EC. If not specified, RSA is used.

For an MQTT over WebSocket connection, provide these arguments:

  • awsAccessKeyId: IAM access key ID
  • awsSecretAccessKey: IAM secret access key
  • sessionToken: (optional) if temporary credentials are used

Run the Sample Applications

You can use the following commands to execute the sample applications (assuming TLS mutual authentication is used).

  • To run the Publish/Subscribe sample, use the following command:
$ mvn exec:java -pl aws-iot-device-sdk-java-samples -Dexec.mainClass="com.amazonaws.services.iot.client.sample.pubSub.PublishSubscribeSample" -Dexec.args="-clientEndpoint <prefix>-ats.iot.<region>.amazonaws.com -clientId <unique client id> -certificateFile <certificate file> -privateKeyFile <private key file>"
  • To run the Shadow sample, use the following command:
$ mvn exec:java -pl aws-iot-device-sdk-java-samples -Dexec.mainClass="com.amazonaws.services.iot.client.sample.shadow.ShadowSample" -Dexec.args="-clientEndpoint <prefix>-ats.iot.<region>.amazonaws.com -clientId <unique client id> -thingName <thing name> -certificateFile <certificate file> -privateKeyFile <private key file>"
  • To run the Shadow echo sample, use the following command:
$ mvn exec:java -pl aws-iot-device-sdk-java-samples -Dexec.mainClass="com.amazonaws.services.iot.client.sample.shadowEcho.ShadowEchoSample" -Dexec.args="-clientEndpoint <prefix>-ats.iot.<region>.amazonaws.com -clientId <unique client id> -thingName <thing name> -certificateFile <certificate file> -privateKeyFile <private key file>"

Sample Source Code

You can get the sample source code either from the GitHub repository as described here or from the latest SDK binary. They both provide you with Maven project files that you can use to build and run the samples from the command line or import them into an IDE, such as Eclipse.

The sample source code included with the latest SDK binary is shipped with a modified Maven project file (pom.xml) that allows you to build the sample source indepedently, without the need to reference the parent POM file as with the GitHub source tree.

API Documentation

You'll find the API documentation for the SDK here.

License

This SDK is distributed under the Apache License, Version 2.0. For more information, see LICENSE.txt and NOTICE.txt.

Support

If you have technical questions about the AWS IoT Device SDK, use the AWS IoT Forum. For any other questions about AWS IoT, contact AWS Support.

aws-iot-device-sdk-java's People

Contributors

bretambrose avatar davidogunsaws avatar dependabot[bot] avatar fengsongaws avatar graebm avatar hyandell avatar jmklix avatar jonathanhenson avatar justinboswell avatar kaibalopez avatar kellertk avatar rongsaws avatar sbstevek avatar somayab avatar tingdaok avatar twistedtwigleg avatar xiazhvera avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-iot-device-sdk-java's Issues

com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure MqttException (0) - java.net.SocketTimeoutException: connect timed out

mvn exec:java -pl aws-iot-device-sdk-java-samples -Dexec.mainClass="com.amazonaws.services.iot.client.sample.pubSub.PublishSubscribeSample" -Dexec.args="-clientEndpoint a3b3bee1v25lqs.iot.us-west-2.amazonaws.com -clientId sdk-java -certificateFile ../caokun.cert.pem -privateKeyFile ../caokun.private.key"
[INFO] Scanning for projects...
[INFO] Inspecting build with total of 1 modules...
[INFO] Installing Nexus Staging features:
[INFO] ... total of 1 executions of maven-deploy-plugin replaced with nexus-staging-maven-plugin
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building aws-iot-device-sdk-java-samples 1.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.5.0:java (default-cli) @ aws-iot-device-sdk-java-samples ---
Dec 19, 2016 8:07:25 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.net.SocketTimeoutException: connect timed out
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:86)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
... 1 more

Dec 19, 2016 8:07:25 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Dec 19, 2016 8:07:25 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: sdk-java
Dec 19, 2016 8:07:28 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Dec 19, 2016 8:08:28 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.net.SocketTimeoutException: connect timed out
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:86)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
... 1 more

Dec 19, 2016 8:08:28 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Dec 19, 2016 8:08:28 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: sdk-java
Dec 19, 2016 8:08:34 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Dec 19, 2016 8:09:34 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.net.SocketTimeoutException: connect timed out
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:86)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
... 1 more

Dec 19, 2016 8:09:34 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Dec 19, 2016 8:09:34 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: sdk-java
Dec 19, 2016 8:09:46 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Dec 19, 2016 8:10:48 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.net.SocketTimeoutException: connect timed out
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:86)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
... 1 more

Dec 19, 2016 8:10:48 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Dec 19, 2016 8:10:48 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: sdk-java
Dec 19, 2016 8:11:12 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Dec 19, 2016 8:12:12 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.net.SocketTimeoutException: connect timed out
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:86)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
... 1 more

Dec 19, 2016 8:12:12 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Dec 19, 2016 8:12:12 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: sdk-java
Dec 19, 2016 8:12:42 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Dec 19, 2016 8:13:42 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.net.SocketTimeoutException: connect timed out
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:86)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
... 1 more

Dec 19, 2016 8:13:42 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Dec 19, 2016 8:13:42 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection retry cancelled or exceeded maximum retries
[WARNING]
Dec 19, 2016 8:13:42 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionClosed
INFO: Client connection closed: sdk-java
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:294)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.amazonaws.services.iot.client.AWSIotException
at com.amazonaws.services.iot.client.core.AwsIotCompletion.get(AwsIotCompletion.java:213)
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:112)
at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:501)
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:93)
at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:463)
at com.amazonaws.services.iot.client.sample.pubSub.PublishSubscribeSample.main(PublishSubscribeSample.java:137)
... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 07:21 min
[INFO] Finished at: 2016-12-19T20:13:42+08:00
[INFO] Final Memory: 17M/193M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:java (default-cli) on project aws-iot-device-sdk-java-samples: An exception occured while executing the Java class. null: InvocationTargetException: AWSIotException -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Client does not recognize network connection disruptions

AWSIotMqttClient does not acknowledge the client network connectivity has been disrupted, even if Paho recognizes the timeout.

Below, I have a scheduled task to check connection status and reestablish as necessary. Unfortunately, awsClient.getConnectionStatus() continues to return AWSIotConnectionStatus.CONNECTED even when the WiFi is off. Hence the client cannot recover after WiFi is back on.

I would appreciate any help how to do this properly, if the SDK behavior is intentional.

The code;

@FXML
public void initialize() {
    <code skipped ...>
    awsClient = new MyAWSIotMqttClient(AWSHOST, AWSCLIENTID, AWSCLIENTKEY, AWSCLIENTSECRET);
    mqttConnectTask = new Timer();
    mqttConnectTask.schedule(new TimerTask() {
        @Override
        public void run() {
            System.out.println("\n(I) >>> " + System.currentTimeMillis() / 1000 + ": Scheduled AWS connection checker");
            System.out.println(awsClient.getConnectionStatus());
            if (awsClient.getConnectionStatus() != AWSIotConnectionStatus.CONNECTED) {
                System.out.println("\n(I) >>> " + System.currentTimeMillis() / 1000 + ": attempting to reconnect to AWS...");
                mqttConnect();
            }
        }
    }, 0, 15000);
}

void mqttConnect() {
    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            try {
                awsClient.connect();
                if (awsClient.getConnectionStatus() == AWSIotConnectionStatus.CONNECTED)
                    updateProgress(1L, 1L);

                awsClient.subscribe(new TopicListener(AWSSHADOWGETACCEPT, true), true);
                awsClient.subscribe(new TopicListener(AWSSHADOWUPDACCEPT, true), true);

                System.out.println("\n(I) >>> " + System.currentTimeMillis() / 1000 + ": Sending shadow get request...");
                awsClient.publish(new IotMessage(AWSSHADOWGET, AWSIotQos.QOS0, ""));
            } 
            catch (AWSIotException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    task.progressProperty().addListener((observable, oldValue, newValue) ->  {
        if ( newValue.longValue() == 1L) {
            fLedAwsConnIndicator.setLedColor(Color.GREEN);
            fLedAwsConnIndicator.setOn(true);				
        }
    });
    new Thread(task).start();
}

The output:


(I) >>> 1520919481: Scheduled AWS connection checker
DISCONNECTED

(I) >>> 1520919481: attempting to reconnect to AWS...
Mar 13, 2018 8:38:03 AM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: Connection successfully established

Mar 13, 2018 8:38:03 AM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: Client connection active: demouser

(I) >>> 1520919484: Sending shadow get request...

(I) <<< 1520919484: Message arrived
[$aws/things/<name skipped>/shadow/get/accepted]
{"state":{"reported":{"connected" <rest skipped ...>

(I) >>> 1520919496: Scheduled AWS connection checker
CONNECTED

*****  Turned WIFI OFF at this point

(I) >>> 1520919511: Scheduled AWS connection checker
CONNECTED

(I) >>> 1520919526: Scheduled AWS connection checker
CONNECTED

(I) >>> 1520919541: Scheduled AWS connection checker
CONNECTED

Mar 13, 2018 8:39:04 AM org.eclipse.paho.client.mqttv3.internal.ClientState checkForActivity
SEVERE: demouser: Timed out as no activity, keepAlive=30,000 lastOutboundActivity=1,520,919,514,095 lastInboundActivity=1,520,919,484,319 time=1,520,919,544,101 lastPing=1,520,919,514,095

(I) >>> 1520919556: Scheduled AWS connection checker
CONNECTED

(I) >>> 1520919571: Scheduled AWS connection checker
CONNECTED

*****  Turned WIFI ON at this point

(I) >>> 1520919586: Scheduled AWS connection checker
CONNECTED

*****  Terminated the application

com.amazonaws.services.iot.client.AWSIotException: Client is currently disconnecting (32102)
	at com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnection.closeConnection(AwsIotMqttConnection.java:77)
	at com.amazonaws.services.iot.client.core.AwsIotConnection.disconnect(AwsIotConnection.java:263)
	at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.disconnect(AbstractAwsIotClient.java:130)
	at com.amazonaws.services.iot.client.AWSIotMqttClient.disconnect(AWSIotMqttClient.java:557)
	at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.disconnect(AbstractAwsIotClient.java:125)
	at com.amazonaws.services.iot.client.AWSIotMqttClient.disconnect(AWSIotMqttClient.java:536)
	at com.innodron.awsclient.MainAppController.shutdown(MainAppController.java:227)
	at com.innodron.awsclient.Main.lambda$0(Main.java:27)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
	at javafx.event.Event.fireEvent(Event.java:198)
	at com.sun.javafx.stage.WindowPeerListener.closing(WindowPeerListener.java:88)
	at com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:122)
	at com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:40)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.tk.quantum.GlassWindowEventHandler.lambda$handleWindowEvent$422(GlassWindowEventHandler.java:151)
	at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
	at com.sun.javafx.tk.quantum.GlassWindowEventHandler.handleWindowEvent(GlassWindowEventHandler.java:149)
	at com.sun.glass.ui.Window.handleWindowEvent(Window.java:1270)
	at com.sun.glass.ui.Window.notifyClose(Window.java:1174)
Caused by: Client is currently disconnecting (32102)
	at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
	at org.eclipse.paho.client.mqttv3.internal.ClientComms.disconnect(ClientComms.java:455)
	at org.eclipse.paho.client.mqttv3.MqttAsyncClient.disconnect(MqttAsyncClient.java:632)
	at com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnection.closeConnection(AwsIotMqttConnection.java:75)
	... 25 more

Connect fails org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1

Today suddenly AWSIotMqttClient::connect started failing with:
com.amazonaws.services.iot.client.AWSIotException: null
at com.amazonaws.services.iot.client.core.AwsIotCompletion.get(AwsIotCompletion.java:213) ~[hdm-push-server-1.3.1-standalone.jar:1.3.1]
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:112) ~[hdm-push-server-1.3.1-standalone.jar:1.3.1]
at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:501) ~[hdm-push-server-1.3.1-standalone.jar:1.3.1]
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:93) ~[hdm-push-server-1.3.1-standalone.jar:1.3.1]
at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:463) ~[hdm-push-server-1.3.1-standalone.jar:1.3.1]

It seems org.eclipse.paho.client.mqttv3 got upgraded from 1.1.0 to 1.1.1. When forced back to 1.1.0 the connection works again

AWSIoTTopic constant disconnected/reconnected messages

Hi, I'm using the SDK on a Raspberry PI 3, Java 8 and having issues with listening on a Topic.

Basically, my code is:

public class TestTopicListener extends AWSIotTopic {

    public TestTopicListener(String topic, AWSIotQos qos) {
        super(topic, qos);
        System.out.println("Listener: Connecting to " + topic);
    }

    @Override
    public void onMessage(AWSIotMessage message) {
        System.out.println(System.currentTimeMillis() + ": <<< " + message.getStringPayload());
        if ( message.getStringPayload().equalsIgnoreCase("restart"))
                System.exit(0);
    }

}

And I'm just getting the following messages on the console:
Jul 19, 2016 11:07:07 AM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure INFO: Connection temporarily lost Jul 19, 2016 11:07:07 AM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure INFO: Client connection lost: rpi001 Jul 19, 2016 11:07:10 AM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run INFO: Connection is being retried Jul 19, 2016 11:07:11 AM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess INFO: Connection successfully established Jul 19, 2016 11:07:11 AM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess INFO: Client connection active: rpi001 Jul 19, 2016 11:07:12 AM com.amazonaws.services.iot.client.mqtt.AwsIotMqttMessageListener onFailure WARNING: Request failed for topic $aws/things/rpi001/shadow/update: Connection lost (32109) - java.io.EOFException Jul 19, 2016 11:07:12 AM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure INFO: Connection temporarily lost Jul 19, 2016 11:07:12 AM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure INFO: Client connection lost: rpi001 Jul 19, 2016 11:07:15 AM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run INFO: Connection is being retried Jul 19, 2016 11:07:15 AM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess INFO: Connection successfully established
I'm subscribing to $aws/things/rpi001/shadow/update. It does seem like the very first message I publish on that topic from the client, the client also receives. But after that one, nothing...

The strange thing is that my publications to another topic seem to be working fine along with publishing to $aws/things/rpi001/shadow/update from the client. I'm seeing the updates in the IoT console.

Any help would be appreciated!

Thanks,
Mac

Mqtt SSL error: No subject alternative names matching IP address

Hi,
I'm using the java SDK to create an Mqtt client to publish and subscribe.
While attempting to connect, I receive the following error:
FINE: sdk-java: connect failed: unexpected exception javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names matching IP address 35.162.54.21 found at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1506) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979) at sun.security.ssl.Handshaker.process_record(Handshaker.java:914) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387) at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:93) at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650) at java.lang.Thread.run(Thread.java:745) Caused by: java.security.cert.CertificateException: No subject alternative names matching IP address 35.162.54.21 found at sun.security.util.HostnameChecker.matchIP(HostnameChecker.java:167) at sun.security.util.HostnameChecker.match(HostnameChecker.java:93) at sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:455) at sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:436) at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:200) at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1488) ... 10 more

I obtained the client certificates from the AWS thing creation procedure, and I'm using them in the sample client founded on github. Like exmplained in the guide, I'm using a clientEndpoint like <prefix>.iot.<region>.amazonaws.com. The example I'm using can be found at https://github.com/aws/aws-iot-device-sdk-java/blob/master/aws-iot-device-sdk-java-samples/src/main/java/com/amazonaws/services/iot/client/sample/pubSub/PublishSubscribeSample.java

I verified on the server certificate (via openssl) if the hostname used was supported, and i found Inc./CN=*.iot.us-west-2.amazonaws.com, that lead me to think that all is ok.

In the debugger, I followed the hostname field, and I verified that it is correctly passed to the Mqtt library.
Usually, that error is related to the use of an IP instead of an hostname, so I suppose that can be a problem to the SDK.

Client Application stops receiving messages after processing 2-3 million messages

Hi,
We have been doing some performance testing and facing an issue almost on daily basis. After close to 2-3 million message processing via MQTT broker using AWS Java based Client SDK, our application stops receiving messages.
In CloudWatch Management Logs we are able to see below messages.

image

Please provide inputs why this may be happening. We are Using AWSIOT Connect with timeout and Subscribe method with timeout and blocking as shown below:

awsIotClient.connect(timeout);
if(logger.isDebugEnabled()) {
logger.debug("Successfully Connected to AWS Broker");
}
awsIotClient.subscribe(mqttMessageCallback, timeout, blocking);

-Sumit

AWSIotMqttClient Continously Connecting & Losing Connections

I am trying to establish the connection with AWSIotMqttClient, the connection is established successfully. But I am constantly seeing the log's of connection lost, established, retired in console as mentioned below. I am currently in Texas regions.

I tried establishing the connection using both ways certificates as well as using access Key ID and secret access key. In both cases I am seeing the same problem.

Security Policy which is attached my Device certification ->

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "iot:*", "Resource": "*" } ] }

AWSIotMqttClient connection Java Code ->

`public AWSIotMqttClient getAWSIoTClient() {
String clientEndpoint = getClientEndpoint();
String clientId = getClientId();

    String certificateFile = getCertificateFile();
    String privateKeyFile = getPrivateKeyFile();
    if (certificateFile != null && privateKeyFile != null) {
        AWSUtil.KeyStorePasswordPair pair = AWSUtil.getKeyStorePasswordPair(certificateFile, privateKeyFile);
        awsIotClient = new AWSIotMqttClient(clientEndpoint, clientId, pair.keyStore, pair.keyPassword);
        if(awsIotClient.getConnectionStatus().equals(AWSIotConnectionStatus.DISCONNECTED))
            try {
                awsIotClient.connect();
            } catch (AWSIotException e) {
                throw new IllegalArgumentException("Failed to connect to AWS IoT Client.");
            }
    }

    if (awsIotClient == null) {
        throw new IllegalArgumentException("Failed to construct client due to missing certificate or credentials.");
    }
    return awsIotClient;
}`

Console Logs ->

`2017-08-09 18:42:15.896  INFO [-,,,] 85753 --- [pool-3-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection successfully established
2017-08-09 18:42:15.896  INFO [-,,,] 85753 --- [pool-3-thread-1] c.a.s.i.c.core.AbstractAwsIotClient      : Client connection active: DEVICE_IOT_PLATFORM
2017-08-09 18:42:15.940  INFO [-,,,] 85753 --- [pool-2-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection temporarily lost
2017-08-09 18:42:15.940  INFO [-,,,] 85753 --- [pool-2-thread-1] c.a.s.i.c.core.AbstractAwsIotClient      : Client connection lost: DEVICE_IOT_PLATFORM
2017-08-09 18:42:16.541  INFO [-,,,] 85753 --- [pool-1-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection is being retried
2017-08-09 18:42:16.872  INFO [-,,,] 85753 --- [pool-1-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection successfully established
2017-08-09 18:42:16.872  INFO [-,,,] 85753 --- [pool-1-thread-1] c.a.s.i.c.core.AbstractAwsIotClient      : Client connection active: DEVICE_IOT_PLATFORM
2017-08-09 18:42:16.898  INFO [-,,,] 85753 --- [pool-3-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection temporarily lost
2017-08-09 18:42:16.898  INFO [-,,,] 85753 --- [pool-3-thread-1] c.a.s.i.c.core.AbstractAwsIotClient      : Client connection lost: DEVICE_IOT_PLATFORM
2017-08-09 18:42:18.945  INFO [-,,,] 85753 --- [pool-2-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection is being retried
2017-08-09 18:42:19.415  INFO [-,,,] 85753 --- [pool-2-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection successfully established
2017-08-09 18:42:19.415  INFO [-,,,] 85753 --- [pool-2-thread-1] c.a.s.i.c.core.AbstractAwsIotClient      : Client connection active: DEVICE_IOT_PLATFORM
2017-08-09 18:42:19.504  INFO [-,,,] 85753 --- [pool-1-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection temporarily lost
2017-08-09 18:42:19.504  INFO [-,,,] 85753 --- [pool-1-thread-1] c.a.s.i.c.core.AbstractAwsIotClient      : Client connection lost: DEVICE_IOT_PLATFORM
2017-08-09 18:42:19.901  INFO [-,,,] 85753 --- [pool-3-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection is being retried
2017-08-09 18:42:20.245  INFO [-,,,] 85753 --- [pool-3-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection successfully established
2017-08-09 18:42:20.245  INFO [-,,,] 85753 --- [pool-3-thread-1] c.a.s.i.c.core.AbstractAwsIotClient      : Client connection active: DEVICE_IOT_PLATFORM
2017-08-09 18:42:20.291  INFO [-,,,] 85753 --- [pool-2-thread-1] c.a.s.iot.client.core.AwsIotConnection   : Connection temporarily lost
2017-08-09 18:42:20.291  INFO [-,,,] 85753 --- [pool-2-thread-1] c.a.s.i.c.core.AbstractAwsIotClient      : Client connection lost: DEVICE_IOT_PLATFORM`

Unhandled java.util.concurrent.RejectedExecutionException

I receive an unhandled java.util.concurrent.RejectedExecutionException when:

  1. I set client.setMaxConnectionRetries(2)
  2. I call client.connect(10000)
  3. The connection fails - e.g. bad endpoint parameter
  4. I call client.connect(10000) again

The issue appears to be similar to this one with aws-sdk-android.

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@758c83d8 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@129b4fe2[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 5] at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830) at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:326) at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:533) at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.scheduleTimeoutTask(AbstractAwsIotClient.java:392) at com.amazonaws.services.iot.client.core.AwsIotCompletion.get(AwsIotCompletion.java:187) at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:112) at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:501) at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:101) at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:480)

Request/ Response pattern implementation using AWS IOT topic ?

Hi,

I have below usecase 👍

Connecting to AWS iot client
Publishing to a topic
the message being published to topic has been subscribed by another client and do the processing and publishes the response on to another topic .I will have to subscribe to that topic to get response and send the response to front end .All this flow should be synchronous as I have to wait for certain time interval until I recieve response after I publish the request on to initial topic .Could you help with some sample example how to solve this issue ?

Regards,
Brahmaiah

Unable to create AWSIotMqttClient instance

I'm trying to use the java sdk to connect to the aws iot. The following code line is throwing error:

AWSIotMqttClient client = new AWSIotMqttClient(clientEndpoint, clientId, awsAccessKeyId, awsSecretAccessKey);

Error:
java.lang.IllegalArgumentException: wss://a2c6oqrhqoygg.iot.us-west-2.amazonaws.com:443
at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:470)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:273)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:167)
at com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnection.(AwsIotMqttConnection.java:49)
at com.amazonaws.services.iot.client.core.AwsIotWebsocketConnection.(AwsIotWebsocketConnection.java:40)
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.(AbstractAwsIotClient.java:74)
at com.amazonaws.services.iot.client.AWSIotMqttClient.(AWSIotMqttClient.java:156)
at com.pramati.aws.iot.demo.testrunner.main(testrunner.java:18)

WebSocket Response header: Incorrect connection header

Hi,

i am following the tutorial https://github.com/aws/aws-iot-device-sdk-java/blob/master/README.md to connect from Java to IoT through MQTT over WebSocket. I am using the simple main:

	AWSIotMqttClient client = new AWSIotMqttClient(clientEndpoint, clientId, awsAccessKeyId, awsSecretAccessKey);
		client.connect();

But i get error:
WARNING: Connect request failure
MqttException (0) - java.io.IOException: WebSocket Response header: Incorrect connection header
....

I am using my AWS credential. My user is also admin and i have policy for full access to IoT:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:",
"Resource": "
"
}
]
}

can you pls help?
thanks lot.

NullPointerException in receiveHandshakeResponse() missing upgrade header.

I received this exception multiple times when I disconnect and reconnect the internet on my computer.
After this appears, the client can no longer receive push notifications. Is there anything I can do to prevent this without having to recompile the source code into my own library? Thanks.

Error:

com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.lang.NullPointerException
        at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
        at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
        at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:133)
        at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:74)
        at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:77)
        at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
        ... 1 more

Code: (upgradeHeader is null)

String upgradeHeader = (String) headerMap.get(HTTP_HEADER_UPGRADE);
if(!upgradeHeader.toLowerCase().contains(HTTP_HEADER_UPGRADE_WEBSOCKET)){
    throw new IOException("WebSocket Response header: Incorrect upgrade.");
}

Steps to reproduce:

  • Connect the client
  • Connection is successful
  • Disconnect the internet (leave the client running)
  • Wait a few minutes
  • The client will attempt to reconnect and fail as expected.
...
com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
...
  • Reconnect the internet.
  • The exception above appears.
  • The client will no longer receive push notifications.

mqtt "Timed out as no activity"

For a couple of weeks now I've been fighting with AWS IoT to get it to work somewhat reliably - w/o success so far. My sensor is publishing results every 15 minutes or so via MQTT over TLS. After a few hours I get:

10:48:04.467 [MQTT Ping: pi] ERROR o.e.p.c.m.i.ClientState - pi: Timed out as no activity, keepAlive=30,000 lastOutboundActivity=1,486,032,454,464 lastInboundActivity=1,486,032,424,570 time=1,486,032,484,445 lastPing=1,486,032,454,464
10:48:04.516 [pool-1-thread-1] INFO c.a.s.i.c.c.AwsIotConnection - Connection temporarily lost
10:48:04.521 [pool-1-thread-1] INFO c.a.s.i.c.c.AwsIotConnection - Connection retry cancelled or exceeded maximum retries
10:48:04.525 [pool-1-thread-1] INFO c.a.s.i.c.c.AbstractAwsIotClient - Client connection closed: pi

After this error, publish() always fails with:

11:01:04.847 [PmMeter] WARN c.o.s.a.AWSIotDataCollector - cannot publish result
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@1c4089b rejected from java.util.concurrent.ScheduledThreadPoolExecutor@1fb9ffb[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 141]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)

This happens despite awsIotClient.setMaxConnectionRetries(Integer.MAX_VALUE);
Now, I have no idea how to recover from such error except creating a new AwsIotClient instance - because previous executor thread pool was shutdown and it is useless.

Please, test AWS IoT java sdk with long-lasting connections with rare inactivity.
This is a very often scenario for IoT.

Make the AWSIotMqttClient with WebSocket working behind a proxy

Hi everyone,
is there a way to make the AWSIotMqttClient with WebSocket working behind a proxy?

The client is behind an HTTPS proxy and the settings are passed to the application through the http.proxyHost and http.proxyPort JVM options.

The problem we are facing right now is that the client is unable to resolve the AWS Iot Endpoint, receiving an UnknownHostException. Is there a way to solve this issue without local access to a DNS?

The Simplified Shadow Access Model does not support nested JSON objects.

When I partly change a nested JSON object in the desired state, the simplified shadow access model cannot correctly deserialize the corresponding delta.

{
  "desired": {
    "foobar": {
      "foo": true,
      "bar": true
    }
  },
  "reported": {
    "foobar": {
      "foo": false,
      "bar": true
    }
  },
  "delta": {
    "foobar": {
      "foo": true
    }
  }
}

For the first level the SDK will use "AwsIotJsonDeserializer" to map individual fields to setters of annotated properties (@AWSIotDeviceProperty), but anything deeper will be deserialized as a whole.

Object value = jsonObjectMapper.treeToValue(node, field.getType());

When a partial delta is deserialized all the fields that are not represented in the delta will contain the default values as specified in the class. This causes all kinds of problems like an endless loop where the reported state can never match the desired state.

ClassNotFoundException: com.amazonaws.services.iot.client.sample.pubSub.PublishSubscribeSample

https://us-west-2.console.aws.amazon.com/iotv2/home?region=us-west-2#/connectdevice/

To configure and test the device, perform the following steps.

Step 1: Unzip the connection kit on the device
unzip connect_device_package.zip
Step 2: Add execution permissions
chmod +x start.sh
Step 3: Run the start script. Messages from your thing will appear below
./start.sh

when run the ./start.sh
....
Downloaded: http://repo2.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar (0 B at 0.0 KB/sec)
Downloaded: http://repo2.maven.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar (0 B at 0.0 KB/sec)
[WARNING]
java.lang.ClassNotFoundException: com.amazonaws.services.iot.client.sample.pubSub.PublishSubscribeSample
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:281)
at java.lang.Thread.run(Thread.java:745)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 56.256 s
[INFO] Finished at: 2016-12-19T15:56:43+08:00
[INFO] Final Memory: 16M/130M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:java (default-cli) on project aws-iot-device-sdk-java-samples: An exception occured while executing the Java class. com.amazonaws.services.iot.client.sample.pubSub.PublishSubscribeSample -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Lombok makes source unreadable

While I'm sure Lombok is very clever, I don't think its a great idea to use it in the way you do in the IoT SDK in this kind of open source project. Coming to this fresh, you can't just import the project into Eclipse to see how the classes are put together. Instead, I find that nothing compiles, and I'm forced to read the pom files, and then understand how the Lombok library works to figure out that basically someone couldn't be bothered to add getter/setter methods to their classes. That's just lazy and in no way makes the code more readable or maintainable.

java.lang.VerifyError: com/amazonaws/services/iot/client/mqtt/AwsIotMqttConnection

I am using this code in my android project for IoT
AWSIotMqttClient a = new AWSIotMqttClient(clientEndpoint, clientId, awsAccessKeyId,awsSecretAccessKey);
Below is stackTrace I am getting while running my App on the above line
java.lang.VerifyError: com/amazonaws/services/iot/client/mqtt/AwsIotMqttConnection
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.(AbstractAwsIotClient.java:74)
at com.amazonaws.services.iot.client.AWSIotMqttClient.(AWSIotMqttClient.java:156)
at com.synerzip.rohitd.CameraSpyActivity.onCreate(CameraSpyActivity.java:207)
at android.app.Activity.performCreate(Activity.java:5242)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(Native Method)
Can you help me to solve this issue?

Option to set Max Inflight for AWS Mqtt Client

I am using aws iot sdk on a device which needs to send out messages at a very high rate, say 100 messages per second.

Initially I used the blocking publish(QOS 1), but I observed that it takes around 200ms for one publish which was not acceptable for my use case. Next I used the non blocking publish which gave me a performance of around 50 messages per sec, but the rest of the messages started throwing exceptions saying too many publishes.

I understand that this is a limitation caused by the default 10 maxInflight of underlying Mqtt paho client but they have provided an option to change this max inflight, wheras aws iot sdk has just absctracted it.

I would hence like to raise an issue to be provided with such an option to set the max inflight queue size as I feel it would help achieve a way higher rate of sending messages.

.AWSIotException: Too many publishes in progress (32202)

com.amazonaws.services.iot.client.AWSIotException: Too many publishes in progress (32202)
        at com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnection.publishMessage(AwsIotMqttConnection.java:92)
        at com.amazonaws.services.iot.client.core.AwsIotConnection.publish(AwsIotConnection.java:161)
        at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.publish(AbstractAwsIotClient.java:186)
        at com.amazonaws.services.iot.client.AWSIotMqttClient.publish(AWSIotMqttClient.java:793)
        at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.publish(AbstractAwsIotClient.java:181)

this is how i'm sending messages to shadow. i'm executing the method which sends out data to the aws for 3 times without an time interval, and i'm getting this error saying too many publishes, is there a solution for this error.

                                    deviceMsg.setQos(AWSIotQos.QOS0);
				deviceMsg.setStringPayload(shadow);
				deviceMsg.setTopic(updateTopic);
				awsIotClient.publish(deviceMsg);

AWS Iot MQTT subscribe with role?

Hi! Our AWS support team has changed its policy, so our teams are no longer able to create users in the AWS console. All access should be handled by roles, which generally seems like a good idea.

But our backend system (hosted in AWS ECS) needs to subscribe to a MQTT topic that is used as a command channel. To establish this subscription, I'm using AWSIotMqttClient, which is my only choice for MQTT subscriptions, as far as I understand. But to establish this connection I either need AWS credentials or certificates, and to use certificates for internal communication seems like a hassle.

Do I need to try to get an exception from their new policy, so I can create users, or is there something in my reasoning that is wrong? Or is there some updates in the near future that should resolve this, e.g. AWS SDK's getting support for MQTT over roles etc.?

Adding a RootCA to the java aws iot MQTT TLS 1.2

I have tried connecting to an MQTT endpoint with the java sdk but always get a connection refused. I tried using the python and c sdk's and they are able to connect, but both of those libraries have a parameter for the RootCA for TLS authentication. I've looked through the java sdk and I don't see any way to include RootCA as a parameter.

Can anyone tell me if this is something I am overlooking or if the java MQTT over TLS is not possible in it's current implementation?

-Max

AWSIotMqttClient Continously Retrying Connections

Hi All,
I am using aws-iot-device-sdk-java-1.1.1 and org.eclipse.paho.client.mqttv3-1.1.0.jar. I am able to connect to AWS MQTT broker using awsAccessKeyId and awsSecretAccessKey.

But when i try to use certificateFile and privateKeyFile, i am getting below errors consistently:

Cert file:696f7b626b-certificate.pem.crt Private key: 696f7b626b-private.pem.key
Oct 11, 2017 10:44:45 AM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
... 1 more

Oct 11, 2017 10:44:45 AM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Oct 11, 2017 10:44:45 AM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: sampleopc2
Oct 11, 2017 10:44:48 AM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried

After following few threads i decided to upgrade paho jar to org.eclipse.paho.client.mqttv3-1.2.0.jar (Java Client), but getting same error consistently.

After eclipse paho jar upgrade even websocket connectivity stops working and getting error as below:

Oct 11, 2017 10:42:32 AM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.io.IOException: WebSocket Response header: Incorrect upgrade.
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:715)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: WebSocket Response header: Incorrect upgrade.
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:148)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:76)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:63)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:701)
... 7 more

Oct 11, 2017 10:42:32 AM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Oct 11, 2017 10:42:32 AM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: sampleopc2
Oct 11, 2017 10:42:35 AM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried

For the time being i can live with org.eclipse.paho.client.mqttv3-1.1.0.jar since atleast awsAccessKeyId and awsSecretAccessKey works.

Sample Code:

String clientEndpoint = arguments.getNotNull("clientEndpoint", SampleUtil.getConfig("clientEndpoint"));
String clientId = arguments.getNotNull("clientId", SampleUtil.getConfig("clientId"));

    String certificateFile = arguments.get("certificateFile", SampleUtil.getConfig("certificateFile"));
    String privateKeyFile = arguments.get("privateKeyFile", SampleUtil.getConfig("privateKeyFile"));
    if (awsIotClient == null && certificateFile != null && privateKeyFile != null) {
        String algorithm = arguments.get("keyAlgorithm", SampleUtil.getConfig("keyAlgorithm"));

        KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(certificateFile, privateKeyFile, algorithm);

        awsIotClient = new AWSIotMqttClient(clientEndpoint, clientId, pair.keyStore, pair.keyPassword);
        awsIotClient.setKeepAliveInterval(30000);
    }

    if (awsIotClient == null) {
        String awsAccessKeyId = arguments.get("awsAccessKeyId", SampleUtil.getConfig("awsAccessKeyId"));
        String awsSecretAccessKey = arguments.get("awsSecretAccessKey", SampleUtil.getConfig("awsSecretAccessKey"));
        String sessionToken = arguments.get("sessionToken", SampleUtil.getConfig("sessionToken"));

        if (awsAccessKeyId != null && awsSecretAccessKey != null) {
            awsIotClient = new AWSIotMqttClient(clientEndpoint, clientId, awsAccessKeyId, awsSecretAccessKey,
                    sessionToken);
        }
    }

    if (awsIotClient == null) {
        throw new IllegalArgumentException("Failed to construct client due to missing certificate or credentials.");
    }

awsIotClient.connect();
AWSIotTopic topic = new TestTopicListener(TestTopic, TestTopicQos);
awsIotClient.subscribe(topic, true);

See policy as :
image

Certificate as :

image

Unable to stablish 2 connections at the same time

Hi, Im using this sdk examples to the AWS IoT.
I'm having a problem when trying to connect two clients.
My point is to make one application "subscriber" the messages the other application "publisher" publishes to a certain topic.
My problem is that when one client connects the other disconnects, and so on.
The message is never received by the "subscriber" application.

Does anyone has a possible solution for this problem?
Some configuration needed to make this approach works?

Thanks in advance!

AWS IOT Java SDK Samples - not working

I have been trying to run the AWS IOT samples. After doing whatever was necessary on the AWS IOT site and using the AWS MQTT client to test publish and subscribe, I tried to connect from my laptop using both nodejs and java. Both do not seem to work and I am unable to figure out what is wrong. While nodsjs just hangs with no clue what is going on, java throws the following on the console. I have no idea what to try or do.

\aws-iot-device-sdk-java-master>mvn exec:java -pl aws-iot-device-sdk-java-samples -Dexec.mainClass="com.amazonaws.services.iot.client.sample.pubSub.PublishSubscribeSample" -Dexec.args="-clientEndpoint a18ddgj0v6owjf.iot.us-west-2.amazonaws.com:8883 -clientId AKIAI4QCZFKU7LECMIEA -certificateFile ..\cert\f2539b98b0-certificate.pem.crt -privateKeyFile ..\cert\f2539b98b0-private.pem.key"
[INFO] Scanning for projects...
[INFO] Inspecting build with total of 1 modules...
[INFO] Installing Nexus Staging features:
[INFO] ... total of 1 executions of maven-deploy-plugin replaced with nexus-staging-maven-plugin
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building aws-iot-device-sdk-java-samples 1.1.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ aws-iot-device-sdk-java-samples ---
Cert file:..\cert\f2539b98b0-certificate.pem.crt Private key: ..\cert\f2539b98b0-private.pem.key
Aug 29, 2017 2:19:50 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
... 1 more

Aug 29, 2017 2:19:50 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Aug 29, 2017 2:19:50 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: AKIAI4QCZFKU7LECMIEA
Aug 29, 2017 2:19:53 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Aug 29, 2017 2:19:56 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
... 1 more

Aug 29, 2017 2:19:56 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Aug 29, 2017 2:19:56 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: AKIAI4QCZFKU7LECMIEA
Aug 29, 2017 2:20:02 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Aug 29, 2017 2:20:04 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
... 1 more

Aug 29, 2017 2:20:04 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Aug 29, 2017 2:20:04 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: AKIAI4QCZFKU7LECMIEA
Aug 29, 2017 2:20:16 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Aug 29, 2017 2:20:19 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
... 1 more

Aug 29, 2017 2:20:19 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Aug 29, 2017 2:20:19 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: AKIAI4QCZFKU7LECMIEA
Aug 29, 2017 2:20:43 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Aug 29, 2017 2:20:46 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
... 1 more

Aug 29, 2017 2:20:46 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Aug 29, 2017 2:20:46 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: AKIAI4QCZFKU7LECMIEA
Aug 29, 2017 2:21:16 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Aug 29, 2017 2:21:18 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
... 1 more

Aug 29, 2017 2:21:18 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Aug 29, 2017 2:21:18 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection retry cancelled or exceeded maximum retries
[Aug 29, 2017 2:21:18 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionClosed
INFO: Client connection closed: AKIAI4QCZFKU7LECMIEA
WARNING]
com.amazonaws.services.iot.client.AWSIotException
at com.amazonaws.services.iot.client.core.AwsIotCompletion.get(AwsIotCompletion.java:213)
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:112)
at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:501)
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:93)
at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:463)
at com.amazonaws.services.iot.client.sample.pubSub.PublishSubscribeSample.main(PublishSubscribeSample.java:138)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
at java.lang.Thread.run(Thread.java:745)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:34 min
[INFO] Finished at: 2017-08-29T14:21:18+05:30
[INFO] Final Memory: 19M/184M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project aws-iot-device-sdk-java-samples: An exception occured while executing the Java class. null: AWSIotException -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

updateThingShadow does not throw exception when regions are wrong

The IOT Hub with all the things was created on us-west-2 region and the code to create the iot client used us-east-1 by mistake.
The update shadow simply returned ok-200 and obviously nothing changed on the shadow but finding why it didn't work was very painful.
I would expect to get an exception if an update to a shadow with a thingName does not exist in a specific region that the client was created.

iotClient = AWSIotClientBuilder.standard().
            withRegion(Regions.US_EAST_1).
            withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey))).build();

    iotDataClient = AWSIotDataClientBuilder.standard().
            withRegion(Regions.US_EAST_1).
            withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey))).build();

    mqttClient = new AWSIotMqttClient(awsClientEndpoint, awsClientId, awsAccessKeyId, awsSecretAccessKey);
private void updateShadow(String thingName, Map<String, ?> attributes) {
    try {
        UpdateThingShadowResult result = iotDataClient.updateThingShadow(new UpdateThingShadowRequest().withThingName(thingName.trim()).withPayload(ByteBuffer.wrap(
                new JSONObject().put("state", new JSONObject().put("desired", new JSONObject(attributes))).toString().getBytes())));
        System.out.println(result.toString());

    } catch (ConflictException ex) {
        ex.printStackTrace();
    }
}

Certificate file is not found Android

I'm using this library for android. I have an all certificates but I can't implement. I need your help.


        String  certificateFile = "/Users/Yusuf/AwsCert/xxxxxxxx-certificate.pem.crt"                      
        String privateKeyFile = "/Users/Yusuf/AwsCert/xxxxxxxx-private.pem.key"           
        val pair = SampleUtil.getKeyStorePasswordPair(certificateFile,privateKeyFile)
//pair object getting is null
        val client = AWSIotMqttClient(clientEndpoint, clientId, pair.keyStore, pair.keyPassword)
        client.connect()

I'm getting this error:
Private key file not found: /Users/Yusuf/AwsCert/xxxxxxxx-private.pem.key
Certificate file: /Users/Yusuf/AwsCert/xxxxxxxx-certificate.pem.crt is not found.

Where is my mistake ?

“Connection permanently closed” caused by too many publishes in progress (32202)

The problem we are experiencing is that the MQTT connection is permanently lost after a successful reconnect. This only happens if we filled the offline message queue while disconnected.

After deep diving into the software stack I noticed the following. After the internet connection is back the IoT client will correctly try to reconnect and onConnectionSuccess() is called in com.amazonaws.services.iot.client.core.AwsIotConnection.java.

@Override
    public void onConnectionSuccess() {
        LOGGER.info("Connection successfully established");
        connectionStatus = AWSIotConnectionStatus.CONNECTED;
        retryTimes = 0;
        cancelRetry();
        // process offline messages
        try {
            while (subscribeQueue.size() > 0) {
                AWSIotMessage message = subscribeQueue.poll();
                subscribeTopic(message);
            }
            while (unsubscribeQueue.size() > 0) {
                AWSIotMessage message = unsubscribeQueue.poll();
                unsubscribeTopic(message);
            }
            while (publishQueue.size() > 0) {
                AWSIotMessage message = publishQueue.poll();
                publishMessage(message);
            }
        } catch (AWSIotException | AwsIotRetryableException e) {
            // should close the connection if we can't send message when
            // connection is good
            LOGGER.log(Level.WARNING, "Failed to send queued messages, will disconnect", e);
            try {
                closeConnection(null);
            } catch (AWSIotException ie) {
                LOGGER.log(Level.WARNING, "Failed to disconnect", ie);
            }
        }
        client.onConnectionSuccess();
        if (connectCallback != null) {
            connectCallback.onSuccess();
            connectCallback = null;
        }
    }

In that callback it will try to loop on the messages stored in the publishQueue and send them. These are the messages that were tried to be send during the no internet connection period. If we get an exception during that phase, the catch section will be executed and closeConnection(null) will be called. That will consequently close the MQTT connection. The exception that causes the catch section to execute is: "too many publishes in progress (32202)"

At some point the callback onSucess() from AwsIotMqttConnectionListener.java will be called and since the MQTT connection is closed it will call client.getConnection().onConnectionClosed() which is implemented in com.amazonaws.services.iot.client.core. onConnectionClosed.
onConnectionClosed will permanently close the aws iot connection.

If I understand correctly what is happening is that the actual in flight messages in Paho are more than the maxInFlight messages. That is caused when AWS tries to send all the messages stored in the offline queue (publishQueue) asynchronously, without any delay. Since we cannot change the MqttOptions from AWS I have no control over the maxInFlight variable.

Do you have any solution for this problem?

maxOfflineMqttQueueSize: 64 (default value)
maxInflight: 10 (default value)

Cannot connect

I cannot get my sdk to connect properly to AWS IoT, either with my own code or with the samples. Here is my aws-iot-sdk-samples.properties file:

# Client endpoint, e.g. <prefix>.iot.us-east-1.amazonaws.com
clientEndpoint=xxx.iot.us-east-1.amazonaws.com

# Client ID, unique client ID per connection
clientId=thing1

# Client certificate file
certificateFile=/Users/Ken/workspace/IotTest/src/main/resources/certs/thing1/1215d8c24d-certificate.pem.crt

# Client private key file
privateKeyFile=/Users/Ken/workspace/IotTest/src/main/resources/certs/thing1/1215d8c24d-private.pem.key

# Thing name
thingName=thing1

The certs were generated in the IoT console. They are active and have this policy attached:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:*",
      "Resource": "arn:aws:iot:us-east-1:123456789:topic/test/*"
    }
  ]
}

My endpoint has ACTIVE status.

This is the error I get locally:

[INFO] --- exec-maven-plugin:1.5.0:java (default-cli) @ aws-iot-device-sdk-java-samples ---
Feb 15, 2017 12:10:06 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
Connection lost (32109) - java.io.EOFException
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
        at java.io.DataInputStream.readByte(DataInputStream.java:267)
        at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
        ... 1 more

Cloudwatch logs say
[INFO] EVENT:MQTT Client Connect MESSAGE:Connect Status: AUTHORIZATION_ERROR

I think I'm using the SDK properly. However, there doesn't seem to be a way to use the CA cert with the SDK (I used IoT's default root VeriSign cert).

Sorry for the noob question, but what am I missing here?

About AWS MQTT Client connection loss event and Clean Session

Hi,

I have two questions for the AWS MQTT Java client. Since I read from the AWS guide https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html that AWS MQTT broker does not support clean session = false, does it mean that once connection lost I have to re-subscribe to all the topics again?

Besides, is there any callback method we can invoke on connection lost? So that I will have the event triggered by the client instance.

Thanks
Jason

Feb 01, 2018 4:03:11 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure WARNING: Connect request failure MqttException (0) - java.lang.NullPointerException at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)

Hi All,
Since mqtt ports are blocked in my org I am trying to use websockets. I tested it with telnet and it is works fine.
I am able to run the same application with websockets from my laptop and it works fine but when i try to run from raspberry pi i am getting the following error.
I tried to change versions of paho mqtt lib and aws sdks but no use.
the following lib versions used.

aws-iot-device-sdk-java-1.1.0
org.eclipse.paho.client.mqttv3-1.1.0

trying to connect to AWS IOT
Feb 01, 2018 4:03:11 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:133)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:74)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:77)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
... 1 more

Feb 01, 2018 4:03:11 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Feb 01, 2018 4:03:11 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: TrackAndTraceClientId
Feb 01, 2018 4:03:14 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Feb 01, 2018 4:03:17 PM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
MqttException (0) - java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:133)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:74)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:77)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
... 1 more

Feb 01, 2018 4:03:17 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Feb 01, 2018 4:03:17 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: TrackAndTraceClientId

Losing Java client connection to AWS IoT

Hi,

The first time I ran the Java subscriber it ran fine and read the data from "MyThing". I published to "MyThing" using a Python script. But after that anytime I run it it keeps losings connection and retrying to establish connection. Would anybody be able to help me?

Mar 10, 2017 9:05:20 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: Connection successfully established
Mar 10, 2017 9:05:20 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: Client connection active: JavaClient
Listener: Connecting to MyThing
Mar 10, 2017 9:05:21 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Mar 10, 2017 9:05:21 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: JavaClient
Mar 10, 2017 9:05:24 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Mar 10, 2017 9:05:24 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: Connection successfully established
Mar 10, 2017 9:05:24 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: Client connection active: JavaClient
Mar 10, 2017 9:05:25 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Mar 10, 2017 9:05:25 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: JavaClient
Mar 10, 2017 9:05:28 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Mar 10, 2017 9:05:28 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: Connection successfully established
Mar 10, 2017 9:05:28 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: Client connection active: JavaClient
Mar 10, 2017 9:05:29 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Mar 10, 2017 9:05:29 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: JavaClient
Mar 10, 2017 9:05:32 PM com.amazonaws.services.iot.client.core.AwsIotConnection$1 run
INFO: Connection is being retried
Mar 10, 2017 9:05:32 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionSuccess
INFO: Connection successfully established
Mar 10, 2017 9:05:32 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionSuccess
INFO: Client connection active: JavaClient
Mar 10, 2017 9:05:33 PM com.amazonaws.services.iot.client.core.AwsIotConnection onConnectionFailure
INFO: Connection temporarily lost
Mar 10, 2017 9:05:33 PM com.amazonaws.services.iot.client.core.AbstractAwsIotClient onConnectionFailure
INFO: Client connection lost: JavaClient

Thanks,
Jack :)

Client gets stuck when publishing a msg in a onMessage callback

Problem

Publishing a message in a response to a message is very typical case but AWSIotMqttClient#publish stucks in ScheduledThreadPoolExecutor if it's run inside AWSIotMessage#onMethod callback for AWSIotMqttClient#subscribe:

"pool-2-thread-1@1516" prio=5 tid=0x13 nid=NA waiting
  java.lang.Thread.State: WAITING
	  at java.lang.Object.wait(Object.java:-1)
	  at java.lang.Object.wait(Object.java:502)
	  at com.amazonaws.services.iot.client.core.AwsIotCompletion.get(AwsIotCompletion.java:203)
	  at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.publish(AbstractAwsIotClient.java:154)
	  at com.amazonaws.services.iot.client.AWSIotMqttClient.publish(AWSIotMqttClient.java:651)
	  at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.publish(AbstractAwsIotClient.java:144)
	  at com.amazonaws.services.iot.client.AWSIotMqttClient.publish(AWSIotMqttClient.java:627)
	  at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.publish(AbstractAwsIotClient.java:135)
	  at com.amazonaws.services.iot.client.AWSIotMqttClient.publish(AWSIotMqttClient.java:580)
	  at com.amazon.aws.iot.SdkBugMain.sendHelloWordlSync(SdkBugMain.java:45)
	  at com.amazon.aws.iot.SdkBugMain.access$000(SdkBugMain.java:15)
	  at com.amazon.aws.iot.SdkBugMain$1.onMessage(SdkBugMain.java:37)
	  at com.amazonaws.services.iot.client.core.AbstractAwsIotClient$1.run(AbstractAwsIotClient.java:293)
	  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	  at java.lang.Thread.run(Thread.java:748)

Scenario

public class SdkBugMain {
    
    private static final String KEYSTORE_RESOURCE_FILE = "keystore";
    private static final String KEYSTORE_PASSWORD = "password";
    private static final String ENDPOINT = "idididididididid.iot.eu-central-1.amazonaws.com";
    private static final AWSIotQos QOS = AWSIotQos.QOS1; // doesn't matter
    
    private static final String INBOUND_TOPIC = "in";
    private static final String OUTBOUND_TOPIC = "out";
    
    public static void main(String[] args) throws Exception {
        InputStream keystoreStream = resourceAsStream(KEYSTORE_RESOURCE_FILE);
        KeyStore keyStore = readKeyStore(keystoreStream, KEYSTORE_PASSWORD);

        AWSIotMqttClient mqttClient = new AWSIotMqttClient(ENDPOINT, UUID.randomUUID().toString(), keyStore, KEYSTORE_PASSWORD);
        
        mqttClient.connect();

        mqttClient.subscribe(new AWSIotTopic(INBOUND_TOPIC, QOS) {
            @Override
            public void onMessage(AWSIotMessage message) {
                System.out.println("Msg arrived = " + message.getStringPayload() + " from topic = " + INBOUND_TOPIC);
                sendHelloWordlSync(mqttClient);
                //sendHelloWordlAsync(mqttClient); //works fine
            }
        });
    }
    
    private static void sendHelloWordlSync(AWSIotMqttClient client) {
        try {
            client.publish(OUTBOUND_TOPIC, "HELLO WORLD");
            System.out.println("Hello sent to topic = " + OUTBOUND_TOPIC); // won't be printed if Sync
        } catch (AWSIotException e) {
            e.printStackTrace();
        }
    }

    private static void sendHelloWordlAsync(AWSIotMqttClient client) {
        ForkJoinPool.commonPool().execute(() -> sendHelloWordlSync(client));
    }
    
    private static InputStream resourceAsStream(String filename) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
    }

    private static KeyStore readKeyStore(InputStream in, String pass) {
        try {
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            keyStore.load(in, pass.toCharArray());
            return keyStore;
        } catch (Exception e) {
            throw new RuntimeException("Failed to read keystore", e);
        }
    }
}

Expected result

  • onMessage callback is called on each message arrival to a 'in' topic
  • 'HELLO WORLD' is sent to an 'out' topic on any incoming from the 'in' topic.

Actual result:

  • onMessage reacts on the first message arrived only
  • 'HELLO WORLD' is sent to the 'out' topic only once
  • onMessage thread is blocked by AwsIotCompletion#get:203 wait
  • incoming messages (handler executions) are stacked in ScheduledExecutorService by AbstractAwsIotClient#dispatch but won't be ever processed.

Walkaround

Run a publish method in a separate thread (uncomment sendHelloWordlAsync, comment sendHelloWordlSync).

IotDevice update shadow only on changes.

Is there a reason why device doesn't update shadow only on changes.
Seams like inefficient and implementation doesn't provide simple way to override behavior.

No option to use custom SSLContext/SocketFactory

AWSIotTlsSocketFactory has a constructor to use a custom SocketFactory, but the front end class AWSIotMqttClient has no constructors that use this functionality. Is it possible to create extra constructors to add this feature as in #52 ??

No virtual method setEndpointIdentificationAlgorithm

Hello guys. I found one problem.

Lib crashed if it used on Android API less than 24.

Calling this constructor:

    public AWSIotMqttClient(String clientEndpoint, String clientId, KeyStore keyStore, String keyPassword) {
        super(clientEndpoint, clientId, keyStore, keyPassword);
    }

Will cause following exception:

java.lang.NoSuchMethodError: No virtual method setEndpointIdentificationAlgorithm(Ljava/lang/String;)V in class Ljavax/net/ssl/SSLParameters; or its super classes (declaration of 'javax.net.ssl.SSLParameters' appears in /system/framework/core-libart.jar)
                      at com.amazonaws.services.iot.client.util.AwsIotTlsSocketFactory.ensureTls(AwsIotTlsSocketFactory.java:124)
                      at com.amazonaws.services.iot.client.util.AwsIotTlsSocketFactory.createSocket(AwsIotTlsSocketFactory.java:80)
                      at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:69)
                      at org.eclipse.paho.client.mqttv3.internal.SSLNetworkModule.start(SSLNetworkModule.java:86)
                      at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
                      at java.lang.Thread.run(Thread.java:818)

if you will look into the AwsIotTlsSocketFactory#ensureTls method, you'll see next code:

    private Socket ensureTls(Socket socket) {
        if (socket != null && (socket instanceof SSLSocket)) {
            ((SSLSocket) socket).setEnabledProtocols(new String[] { TLS_V_1_2 });

            // Ensure hostname is validated againt the CN in the certificate
            SSLParameters sslParams = new SSLParameters();
            sslParams.setEndpointIdentificationAlgorithm("HTTPS");
            ((SSLSocket) socket).setSSLParameters(sslParams);
        }
        return socket;
    }

So lib crashed in line: sslParams.setEndpointIdentificationAlgorithm("HTTPS"); if app was launched on android api less than 24.
Here is official docs: SSLParameters#setEndpointIdentificationAlgorithm(java.lang.String)
Direct link to file in repo

Unexpected warning

Hi,

things are working for me, but I see warnings like this:

Dec 13, 2016 11:30:29 AM com.amazonaws.services.iot.client.shadow.AwsIotDeviceCommandManager onCommandAck
WARNING: Unknown command received from topic $aws/things/3d621cd1-aeac-4fa3-8a7f-560ebca7473f/shadow/update/accepted

The problem seems to originate in AwsIotDeviceCommandManager, line 268, where it returns null because it can't find a particular command in pendingCommands. Is this normal? Everything seems to work as I expect otherwise.

awsIotClient.connect() failed

Hi

I am trying to run the PublishSubscribeSample. I set up everything in the aws-iot-sdk-samples.properties.
It failed on line 137 awsIotClient.connect();

The configuration is pretty straight forward. Could you help me check if I missed anything, please?

Sep 08, 2016 3:37:06 AM com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnectionListener onFailure
WARNING: Connect request failure
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)
... 1 more

Java ME SDK

Hi! I'm working a project that connect to aws iot over Embedded Java J2ME. Did you a plan for release SDK for Java Me?

Getting Invalid Key Store Errors while using MQTT over WebSocket with AWS Signature Version 4 authentication

Use Case - My Client application has a TrustStore Loaded in its Java Application Thread which has nothing to do with Certificates required for AWS IOT connectivity.

I am Trying to connect to AWS IoT MQTT Broker as "MQTT over WebSocket with AWS Signature Version 4 authentication".

But i am getting below error:

Exception in thread "main" com.amazonaws.services.iot.client.AWSIotException: MqttException (0) - java.io.IOException: Invalid keystore format
at com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnection.openConnection(AwsIotMqttConnection.java:68)
at com.amazonaws.services.iot.client.core.AwsIotConnection.connect(AwsIotConnection.java:246)
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:111)
at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:501)
at com.amazonaws.services.iot.client.core.AbstractAwsIotClient.connect(AbstractAwsIotClient.java:93)
at com.amazonaws.services.iot.client.AWSIotMqttClient.connect(AWSIotMqttClient.java:463)
at com.emeter.PublishSubscribeSample.main(PublishSubscribeSample.java:110)
Caused by: MqttException (0) - java.io.IOException: Invalid keystore format
at org.eclipse.paho.client.mqttv3.internal.security.SSLSocketFactoryFactory.getSSLContext(SSLSocketFactoryFactory.java:1284)
at org.eclipse.paho.client.mqttv3.internal.security.SSLSocketFactoryFactory.createSocketFactory(SSLSocketFactoryFactory.java:1344)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.createNetworkModule(MqttAsyncClient.java:446)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.createNetworkModules(MqttAsyncClient.java:349)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.connect(MqttAsyncClient.java:557)
at com.amazonaws.services.iot.client.mqtt.AwsIotMqttConnection.openConnection(AwsIotMqttConnection.java:66)
... 6 more
Caused by: java.io.IOException: Invalid keystore format
at sun.security.provider.JavaKeyStore.engineLoad(Unknown Source)
at sun.security.provider.JavaKeyStore$JKS.engineLoad(Unknown Source)
at sun.security.provider.KeyStoreDelegator.engineLoad(Unknown Source)
at sun.security.provider.JavaKeyStore$DualFormatJKS.engineLoad(Unknown Source)
at java.security.KeyStore.load(Unknown Source)
at org.eclipse.paho.client.mqttv3.internal.security.SSLSocketFactoryFactory.getSSLContext(SSLSocketFactoryFactory.java:1259)
... 11 more

After our analysis we found eclipse paho tries to create SSLSocketFactoryFactory and tries to read KeyStore and TrustStore. In getSSLContext() method of SSLSocketFactoryFactory its doing so. Please go through code.

We also found in the constructor of AwsIotWebsocketConnection as shown below, SocketFactory is passed as null.:

public AwsIotWebsocketConnection(AbstractAwsIotClient client, String awsAccessKeyId, String awsSecretAccessKey,
String sessionToken) throws AWSIotException {
super(client, null, "wss://" + client.getClientEndpoint() + ":443");

    // Port number must be included in the endpoint for signing otherwise
    // the signature verification will fail. This is because the Paho client
    // library always includes port number in the host line of the
    // HTTP request header, e.g "Host: data.iot.us-east-1.amazonaws.com:443".
    urlSigner = new AwsIotWebSocketUrlSigner(client.getClientEndpoint() + ":443");
    urlSigner.updateCredentials(awsAccessKeyId, awsSecretAccessKey, sessionToken);
}

Due to Null socketFactory, eclipse paho tries to create a socket factory which in turn leads to this problem. Ideally AWS must have provided way to input socket factory.

I have already tried not setting TrustStore which is my application trust store certificate file. After removing trust store everythign works.

Support for event based device reports from IoT SDK

Currently the device reporting to the shadow is time-based. For e.g, the device SDK would 'sync' with the shadow based on a time-interval set using setReportInterval(reportInterval).

However, there may be cases where such time-interval based syncing is not required.
Its possible to turn periodic syncing off by setting reportInterval = 0.
But by doing so, one has to then first manually build the JSON message (including the other headers likeversion, state, reported ... ) & later may be use --
public void update(String jsonState, long timeout) to update the shadow.

private void sendDeviceReport(long reportVersion, String jsonState) method already does the functionality of building the JSON message & updating the shadow.
It would be beneficial if a similar method is made (public) & is made available to use for event-based reporting.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.