Storing retained messages externally

I want to store retained messages externally (Azure Blob storage). In the HiveMQ extension SDK I can’t find an appropriate callback method that is called when HiveMQ receives a message that should be retained.

What is the ideal way to implement this?

Hello Amrem,

You can create your custom HiveMQ Broker Extension and register a custom PublishInboundInterceptor, that checks the retain property of the message and performs your custom actions:

public class myInterceptor implements PublishInboundInterceptor {

    @Override
    public void onInboundPublish(
            final @NotNull PublishInboundInput publishInboundInput,
            final @NotNull PublishInboundOutput publishInboundOutput) {
        
        if (publishInboundOutput.getPublishPacket().getRetain()) {
            // your code to store retained message
        }
    }
}

You can find a good open source example of creating a custom HiveMQ Extension in our HiveMQ 4 Hello World Extension repository.

I hope this will help you to implement your use case successfully.

Kind regards,
Dasha from HiveMQ Team

2 Likes