AWS IoT Device SDK for Java v2: problems following tutorial on Publish/subscribe local messages #474
-
Hello! I'm following the guide described in https://docs.aws.amazon.com/greengrass/v2/developerguide/ipc-publish-subscribe.html. I've created a Greengrass component with the following Java code: // credentials ipcServerSocketPath and authToken are read correctly from env variables
GreengrassCoreIPCClientV2 greengrassClient = GreengrassCoreIPCClientV2.builder()
.withSocketPath(ipcServerSocketPath)
.withAuthToken(authToken)
.withSocketDomain(SocketOptions.SocketDomain.LOCAL)
.build();
String topic = "test";
byte[] payloadBody = new String("hello").getBytes();
MessageContext messageContext = new MessageContext().withTopic(topic);
BinaryMessage binaryMessage = new BinaryMessage().withMessage(payloadBody).withContext(messageContext);
PublishMessage publishMessage = new PublishMessage().withBinaryMessage(binaryMessage);
PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest().withTopic(topic).withPublishMessage(publishMessage);
greengrassClient.publishToTopic(publishToTopicRequest); Whenever i publish the message I got the following stacktrace: software.amazon.awssdk.aws.greengrass.model.ServiceError
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.google.gson.internal.ConstructorConstructor$4.construct(ConstructorConstructor.java:140)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:211)
at software.amazon.awssdk.eventstreamrpc.EventStreamRPCServiceModel$EventStreamPostFromJsonTypeAdapter.read(EventStreamRPCServiceModel.java:105)
at software.amazon.awssdk.eventstreamrpc.EventStreamRPCServiceModel$EventStreamPostFromJsonTypeAdapter.read(EventStreamRPCServiceModel.java:79)
at com.google.gson.Gson.fromJson(Gson.java:991)
at com.google.gson.Gson.fromJson(Gson.java:956)
at com.google.gson.Gson.fromJson(Gson.java:905)
at com.google.gson.Gson.fromJson(Gson.java:876)
at software.amazon.awssdk.eventstreamrpc.EventStreamRPCServiceModel.fromJson(EventStreamRPCServiceModel.java:357)
at software.amazon.awssdk.eventstreamrpc.EventStreamRPCClient$1.onContinuationMessage(EventStreamRPCClient.java:102)
at software.amazon.awssdk.crt.eventstream.ClientConnectionContinuationHandler.onContinuationMessageShim(ClientConnectionContinuationHandler.java:41) It does not seem a permission/authorization error. Am I missing something? aws-iot-device-sdk version 1.16.0 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
Hello @marcellorinaldo , Best, Yasmine |
Beta Was this translation helpful? Give feedback.
-
Please use the default builder for the client, you should not set any of those options yourself. You're using You need to specify the topic to send to in the publish request, not in the message context. Fully worked examples are provided in the documentation: https://docs.aws.amazon.com/greengrass/v2/developerguide/ipc-publish-subscribe.html#ipc-operation-publishtotopic:~:text=in%20its%20response.-,Examples,-The%20following%20examples |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
Please use the default builder for the client, you should not set any of those options yourself.
GreengrassCoreIPCClientV2.builder().build()
.You're using
MessageContext
which is only used when receiving messages, not when sending messages. The documentation notes "AWS IoT Greengrass Core software sets this context object in messages when you subscribe, and ignores this context object in messages that you publish."You need to specify the topic to send to in the publish request, not in the message context.
Fully worked examples are provided in the documentation: https://docs.aws.amazon.com/greengrass/v2/developerguide/ipc-publish-subscribe.html#ipc-operation-publishtotopic:~:text=in%20its…