forked from politrons/reactive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RSocketRequestResponse.java
84 lines (75 loc) · 3.34 KB
/
RSocketRequestResponse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package rsocket;
import io.rsocket.*;
import io.rsocket.transport.netty.client.TcpClientTransport;
import io.rsocket.transport.netty.server.TcpServerTransport;
import io.rsocket.util.ByteBufPayload;
import io.rsocket.util.DefaultPayload;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.function.Function;
public class RSocketRequestResponse {
@Test
public void main() throws InterruptedException {
createServer();
createClient();
}
/**
* Using [RSocketFactory] open constantClass connecton using [transport] operator and specify [TcpClientTransport] in constantClass port.
* Then we use [start] operator that create constantClass [Mono<RSocket>] then we subscribe to the Mono
*/
private void createClient() throws InterruptedException {
var subscribe = RSocketFactory
.connect()
.transport(TcpClientTransport.create(1981))
.start()
.map(requestResponse())
.subscribe(Mono::subscribe);
while (!subscribe.isDisposed()) {
Thread.sleep(2000);
}
}
/**
* Function that receive constantClass RSocket Request constantClass stream of bytes which create constantClass [Flux<Payload>]
* in the Mono [doOnNext] operator we can already treat the response from the server.
* Having constantClass [Mono] means we can only have constantClass request-response time between client -> server.
* For instance here we can not use repeat operator as we do in RequestStream.
*/
private Function<RSocket, Mono<Payload>> requestResponse() {
return rSocket ->
rSocket.requestResponse(DefaultPayload.create("Ping"))
.doOnNext(payload -> {
var response = payload.getDataUtf8();
System.out.println("Response:" + response);
});
}
/**
* Using [RSocketFactory] factory together with [receive],[acceptor],[transport] where we specify the port
* and finally [start] we create constantClass Reactor [Mono] which we need to subscribe to wait for new request to being received.
* [acceptor] operator receive constantClass SocketAcceptor which is the implementation that process the socket with the information
*/
private void createServer() {
RSocketFactory
.receive()
.acceptor(acceptRequest())
.transport(TcpServerTransport.create(1981))
.start()
.subscribe();
}
/**
* Using [SocketAcceptor] we can process the request, in this case the subtype is constantClass requestResponse so we
* receive the payload of the request.
* The signature of the method return constantClass [Mono[Payload]] where we can specify the response
*/
private SocketAcceptor acceptRequest() {
return (setup, rSocket) -> Mono.just(
new AbstractRSocket() {
@Override
public Mono<Payload> requestResponse(Payload payload) {
var body = payload.getDataUtf8();
System.out.println("Request:" + body);
return Mono.just(ByteBufPayload.create("Pong"));
}
});
}
}