0

尝试使用新的 JITP 设备证书连接到 AWS IoT Core 时出现以下异常:

software.amazon.awssdk.crt.mqtt.MqttException:连接意外关闭。

我已经以编程方式实现了与官方教程等效的 AWS IoT JITP ( https://aws.amazon.com/premiumsupport/knowledge-center/aws-iot-core-jitp-setup/ )

提供部分工作正常,我在其中生成设备证书。AWS 教程随后指出,您需要对 AWS IoT 进行“虚拟”发布调用,以启动新 IoT 事物的注册过程。此启动由运行触发:

mosquitto_pub --cafile awsRootCA.pem --cert deviceCertAndCACert.crt --key deviceCert.key -h a27icbrpsxxx-ats.iot.us-east-2.amazonaws.com -p 8883 -q 1 -t  foo/bar -i  anyclientID --tls-version tlsv1.2 -m "Hello" -d

如果我在生成设备证书之后和第一次尝试连接之前手动运行上述 mosquitto_pub 命令,则连接成功。

我想知道是否有办法使用适用于 Java 的 AWS IoT 设备开发工具包来执行与此“mosquitto_pub”命令等效的操作,换句话说:在 AWS IoT Core 中启动注册过程。

这是我用来尝试连接的代码(请注意,如果我先运行 mosquitto_pub 命令,它会起作用):

    try(EventLoopGroup eventLoopGroup = new EventLoopGroup(1);
        HostResolver resolver = new HostResolver(eventLoopGroup);
        ClientBootstrap clientBootstrap = new ClientBootstrap(eventLoopGroup, resolver);
        AwsIotMqttConnectionBuilder builder = AwsIotMqttConnectionBuilder.newMtlsBuilderFromPath(CERT_PATH, KEY_PATH)) {

        builder.withCertificateAuthorityFromPath(null, AWS_ROOT_CA_PATH)
                .withEndpoint(CLIENT_ENDPOINT)
                .withClientId(THING_NAME)
                .withBootstrap(clientBootstrap)
                .withConnectionEventCallbacks(callbacks);

        try (MqttClientConnection connection = builder.build()) {
            CompletableFuture<Boolean> connected = connection.connect();
            try {
                boolean sessionPresent = connected.get();
                System.out.println("Connected to " + (!sessionPresent ? "new" : "existing") + " session!");
            } catch (Exception ex) {
                throw new RuntimeException("Exception occurred during connect", ex);
            }
4

0 回答 0