Search code examples
spring-bootrsocketspring-boot-rsocket

How to detect if RSocket connection is successfull?


I have the following program through which I can detect the connection failure i.e doBeforeRetry.

Can someone tell me how to detect the successful connection or reconnection. I want to integrate a Health Check program that monitors this connection, but I am unable to capture the event that informs the connections is successfull.

Thanks

 requester =  RSocketRequester.builder()
                .rsocketConnector(connector -> {
                    connector.reconnect(Retry
                            .fixedDelay(Integer.MAX_VALUE,Duration.ofSeconds(1))
                            .doBeforeRetry(e-> System.out.println("doBeforeRetry===>"+e))
                            .doAfterRetry(e-> System.out.println("doAfterRetry===>"+e))
                    );
                    connector.payloadDecoder(PayloadDecoder.ZERO_COPY);
                }
                ).dataMimeType(MediaType.APPLICATION_CBOR)
                .rsocketStrategies(strategies)
                .tcp("localhost", 7999);

Solution

  • I achieved the detection of successful connection or reconnection with the following approach.

    Client Side (Connection initialization)

       Mono<RSocketRequester> requester =  Mono.just(RSocketRequester.builder()
                     .rsocketConnector(
                       // connector configuration goes here
                     )
                    .dataMimeType(MediaType.APPLICATION_CBOR)
                    .setupRoute("client-handshake") 
                    .setupData("caller-name")
                    .tcp("localhost", 7999)));
    

    One the server side

        @ConnectMapping("client-handshake")
        public void connect(RSocketRequester requester, @Payload String callerName) {
            LOG.info("Client Connection Handshake: [{}]", callerName);
            requester
            .route("server-handshake")
            .data("I am server")
            .retrieveMono(Void.class)
            .subscribe();
        }
    

    On the client side, when I receive the callback on the below method, I detect the connection is successfull.

       @MessageMapping("server-handshake")
        public Mono<ConsumerPreference> handshake(final String response){
                LOG.info("Server Connection Handshake received : Server message [{}]", response.getCallerName());
                connectionSuccess.set(true);
                return Mono.empty();
            }else{
                throw new InitializationException("Invalid response message received from Server");
            }
        }
    

    Additionally, created a application level heartbeat to ensure, the liveliness of the connection.