-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathRSocketRequestStream.java
94 lines (84 loc) · 3.76 KB
/
RSocketRequestStream.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
85
86
87
88
89
90
91
92
93
94
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.time.Duration;
import java.util.function.Function;
public class RSocketRequestStream {
@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(requestStream())
.subscribe(Flux::subscribe);
while (!subscribe.isDisposed()) {
Thread.sleep(10000);
}
}
/**
* Function that receive constantClass RSocket Request constantClass stream of bytes which create constantClass [Flux<Payload>]
* in the Flux [doOnNext] operator we can already treat the response from the server.
* Having constantClass [Flux] means can have back-pressure between client -> server.
* For instance here we can repeat this process 5 times, sending data.
* Also here we can delay the emission between request using [delayElements]
*/
private Function<RSocket, Flux<Payload>> requestStream() {
return rSocket ->
rSocket.requestStream(DefaultPayload.create("Ping"))
.doOnNext(payload -> {
var response = payload.getDataUtf8();
System.out.println("Response:" + response);
})
.delayElements(Duration.ofMillis(500))
.repeat(10);
}
/**
* 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 requestStream so we
* receive the payload of the request.
* The signature of the method return constantClass [Flux[Payload]] where we specify the response
*/
private SocketAcceptor acceptRequest() {
return (setup, rSocket) -> Mono.just(
new AbstractRSocket() {
@Override
public Flux<Payload> requestStream(Payload payload) {
var body = payload.getDataUtf8();
System.out.println("Request:" + body);
return Flux.generate(
sink -> {
var response = ByteBufPayload.create("Pong");
sink.next(DefaultPayload.create(response));
sink.complete();
});
}
});
}
}