Search code examples
spring-bootspring-messagingrsocketspring-rsocket

How do you add reactive interceptors to Spring Boot annotated controllers?


I've set up rsocket metrics using rsocket-micrometer on the CLIENT side, by configuring the RSocketConnector with interceptors, like this (Kotlin):

    rSocketReqesterBuilder.rsocketConnector { configureConnector(it) }

   // ...

    private fun configureConnector(rSocketConnector: RSocketConnector) {
        rSocketConnector.interceptors { iRegistry ->
            // This gives us the rsocket.* counter metrics, like rsocket.frame
            iRegistry.forResponder(MicrometerRSocketInterceptor(registry, *localTags.toArray()))
            iRegistry.forRequester(MicrometerRSocketInterceptor(registry, *localTags.toArray()))
            iRegistry.forConnection(MicrometerDuplexConnectionInterceptor(registry, *localTags.toArray()))
        }
    }

But on the SERVER side, I'm using an annotated (@MessageMapping) Spring Boot RSocket Controller, like this (Java):

    @MessageMapping("replace-channel-controller")
    public Flux<TransformResponse> replace(Flux<String> texts) ...

Here, I'm not explicitly in control of the connector.
How do I add interceptors on the server side?


Solution

  • @Configuration
    public class RSocketConfig implements RSocketServerCustomizer {
    
        private final MeterRegistry registry;
    
        public RSocketConfig(MeterRegistry registry) {
            this.registry = registry;
        }
    
        @Override
        public void customize(RSocketServer rSocketServer) {
            rSocketServer.interceptors(
                    iRegistry -> {
                        log.info("Adding RSocket interceptors...");
                        iRegistry.forResponder(new MicrometerRSocketInterceptor(registry, tags));
                        iRegistry.forRequester(new MicrometerRSocketInterceptor(registry, tags));
                        iRegistry.forConnection(new MicrometerDuplexConnectionInterceptor(registry, tags));
                    }
            );
        }
    }