I’m new to HiveMQ and have a few questions regarding Publish Outbound Interceptor:
I got to know from the documentation that we can use Publish Outbound Interceptor to prevent the delivery of a PUBLISH message at the moment it is sent to its subscriber.
Is there a way to retrieve all the intended recipients/subscribers of this message at this point?
If this message has multiple subscribers, does preventPublishDelivery() of PublishOutboundOutput prevent the message delivery to all its subscribers?
I’m looking to write an extension that intercepts every message going out from HiveMQ broker to the subscribers and prevents the message delivery to certain subscribers only.
Can this be done using preventPublishDelivery() of PublishOutboundOutput? Please could anyone shed some light on this?
Welcome to the HiveMQ Community Forum and thanks for your interest in developing extensions.
All interceptors are called on a “per client” basis. That means for the PublishOutboundInterceptor that it would be called for every subscriber of the topic.
If you call preventPublishDelivery() it will only affect the client for which the interceptor was called.
To find out which client it is, you can check the client id by calling getClientInformation().getClientId() on the PublishOutboundInput
Here a simple example interceptor which prevents every publish to topic “not/for/A” for clients that contain an “A” in there clientID.
final PublishOutboundInterceptor myInterceptor = new PublishOutboundInterceptor() {
@Override
public void onOutboundPublish(final @NotNull PublishOutboundInput publishOutboundInput, final @NotNull PublishOutboundOutput publishOutboundOutput) {
final String clientId = publishOutboundInput.getClientInformation().getClientId();
final String topic = publishOutboundInput.getPublishPacket().getTopic();
if (clientId.contains("A") && topic.equals("not/for/A")) {
log.info("publish prevented for client '{}' and topic '{}'", clientId, topic);
publishOutboundOutput.preventPublishDelivery();
return;
}
log.info("publish delivered for client '{}' and topic '{}'", clientId, topic);
}
};