How to add a ConnectInboundInterceptor?

I am building an HiveMQ Extension and want to register an ConnectInboundInterceptor. When I take a look at the examples and documentation of interceptors I can find an example of a PublishInboundInterceptor which is registered like this:

final ClientInitializer clientInitializer = 
    (initializerInput, clientContext) -> {
        clientContext.addPublishInboundInterceptor(
            publishInboundInterceptor
        );
};

But I can’t find a way to add an ConnectInboundInterceptor. There is no addConnectInboundInterceptor(ConnectInboundInterceptor cii) method which I would expect looking at the example.

How do I add an ConnectInboundInterceptor?

You find the documentation here: Interceptors :: HiveMQ Documentation

Connect interceptor works a little bit different than the other interceptors, because at the time a CONNECT message is sent you don’t have a client context yet (only after a CONNACK with success code is sent back).

Here is a code example from the linked docu:

public void extensionStart(final @NotNull ExtensionStartInput extensionStartInput, final @NotNull ExtensionStartOutput extensionStartOutput) {

    try {
        final ConnectInboundInterceptorA interceptorA = new ConnectInboundInterceptorA();
        final ConnectInboundInterceptorB interceptorB = new ConnectInboundInterceptorB();
        Services.interceptorRegistry().setConnectInboundInterceptorProvider(input -> {
            final String clientId = input.getClientInformation().getClientId();
            if (clientId.startsWith("a")) {
                return interceptorA;
            } else {
                return interceptorB;
            }
        });

    } catch (Exception e) {
        log.error("Exception thrown at extension start: ", e);
    }
}

Greetings,
Michael

1 Like

Thanks Michael, got it to work with you comment!