I’d like to generate several Publish message(s) to a specific topic in response to a successful subscription to that same specific topic. Right now I’ve created an extension that implements the SubackOutboundInterceptor interface. It works, but I have to hard code the topic name in the publish message.
Is there any way to access the name of the topic in the related to the Suback message in this context?
@Override
public void onOutboundSuback(@NotNull SubackOutboundInput subackOutboundInput,
@NotNull SubackOutboundOutput subackOutboundOutput) {
final SubackPacket subackPacket = subackOutboundInput.getSubackPacket();
SubackReasonCode firstReasonCode = subackPacket.getReasonCodes().get(0);
// make sure the reason code of the response is one of the GRANTED flavors
if(firstReasonCode.equals(SubackReasonCode.GRANTED_QOS_0) ||
firstReasonCode.equals(SubackReasonCode.GRANTED_QOS_1) ||
firstReasonCode.equals(SubackReasonCode.GRANTED_QOS_2)) {
final PublishService publishService = Services.publishService();
final ByteBuffer payload = ByteBuffer.wrap("My Sample Message".getBytes());
Publish message = Builders.publish()
.topic("user/c2ace553-0959-4bce-b674-0c7ca015dc02") // <----- HOW TO DERIVE HIS FROM THE SubackPacket
.payload(payload)
.build();
final CompletableFuture<Void> future = publishService.publish(message);
future.whenComplete((aVoid, throwable) -> {
if (throwable == null) {
System.out.println("Publish sent successfully");
} else {
//please use more sophisticated logging
throwable.printStackTrace();
}
});
}
}