awsIot for Java 创建Iot设备客户端服务

使用aws-iot-device服务,需要导入

    com.amazonaws
    aws-iot-device-sdk-java
    1.2.0


    com.amazonaws
    aws-iot-device-sdk-java-samples
    1.2.0
代码开始:

private static String ACCESS_KEY = "你的亚马逊账户访问密钥ID";
private static String SECRET_ACCESS_KEY = "你的亚马逊账户访问密钥内容";

private static String CLIENT_ENDPOINT = "xxxxx.iot.us-east-1.amazonaws.com";
private static String CLIENT_ID = "你的事物名称";
/**
 * 获取iot设备客户端服务连接客户端
 * @param accessKey
 * @param secretAccessKey
 * @return
 */
public static AWSIotMqttClient getAmazonIotClientService(String accessKey,String secretAccessKey,String clientEndpoint,String clientId){
    long startTime = System.currentTimeMillis();
    try {
        if(StringUtils.isNotEmpty(accessKey) && StringUtils.isNotEmpty(secretAccessKey)){
            ACCESS_KEY = accessKey;
            SECRET_ACCESS_KEY = secretAccessKey;
        }
        if(StringUtils.isNotEmpty(clientEndpoint) && StringUtils.isNotEmpty(clientId)){
            CLIENT_ENDPOINT = clientEndpoint;
            CLIENT_ID = clientId;
        }
    }catch (Exception e){
        LOGGER.error("IotServiceUtil.getAmazonIotClientService",e);
    }
    long endTime = System.currentTimeMillis();
    LOGGER.info("IotServiceUtil.getAmazonIotClientService,创建连接时间={},accessKey={},secretAccessKey={}",(endTime - startTime),accessKey,secretAccessKey);
    return createIotClientService();
}
/**
 * 创建iotMqtt客户端服务连接
 */
public static AWSIotMqttClient createIotClientService(){
    AWSIotMqttClient awsIotClient = null;
    try {
        //创建连接
        awsIotClient = new AWSIotMqttClient(CLIENT_ENDPOINT, CLIENT_ID, ACCESS_KEY, SECRET_ACCESS_KEY);

        /*String clientEndpoint = "xxxxxxxx.iot.us-east-1.amazonaws.com";       // replace  and  with your own
        String clientId = "你的iot事物名称";                              // replace with your own client ID. Use unique client IDs for concurrent connections.
        String certificateFile = "D:\\Java_Tools\\Aws\\iot\\2018-09-04\\123.cert.pem";                       // X.509 based certificate file
        String privateKeyFile = "D:\\Java_Tools\\Aws\\iot\\2018-09-04\\456.private.key";

        SampleUtil.KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(certificateFile, privateKeyFile);
        awsIotClient = new AWSIotMqttClient(clientEndpoint, clientId, pair.keyStore, pair.keyPassword);*/

    }catch (Exception e){
        LOGGER.error("IotServiceUtil.createIotClientService aws-iot创建连接异常",e);
        awsIotClient = null;
    }
    return awsIotClient;
}

 

你可能感兴趣的:(awsIot for Java 创建Iot设备客户端服务)