I want to create RSocket request endpoints using plan Java RSocket SDK and without using Spring Framework. I was able to create a sample request with the help of the below code snippet. This works only for the request tcp://localhost:7000
. But I want to create different endpoints similar to this.
public class ServerExample {
public static void main(String[] args) {
Hooks.onErrorDropped(e -> {});
RSocket handler = new RSocket() {
@Override
public Mono<Payload> requestResponse(Payload payload) {
System.out.println("RequestResponse: " + payload.getDataUtf8());
return Mono.just(payload);
}
@Override
public Flux<Payload> requestStream(Payload payload) {
System.out.println("RequestStream: " + payload.getDataUtf8());
return Flux.just("First", "Second").map(DefaultPayload::create);
}
@Override
public Mono<Void> fireAndForget(Payload payload) {
System.out.println("FireAndForget: " + payload.getDataUtf8());
return Mono.empty();
}
};
RSocketServer.create(SocketAcceptor.with(handler))
.bindNow(TcpServerTransport.create("localhost", 7000))
.onClose()
.doOnSubscribe(subscription -> System.out.println("RSocket Server listen on tcp://localhost:7000"))
.block();
}
}
You can leverage the RSocket protocol defined metadata and more explicitly the
Routing
extension metadata.Your server implementation would be updated to respond based on the particular request routing metadata value dynamically switching responders:
Within the client implementation, you need to provide the encoded
routing
metadata as follows: