forked from politrons/reactive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSocketRequestChannel.java
98 lines (88 loc) · 4.26 KB
/
RSocketRequestChannel.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
95
96
97
98
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 org.junit.Test;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.function.Function;
import static io.rsocket.util.DefaultPayload.create;
public class RSocketRequestChannel {
@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(requestChannel())
.subscribe(Flux::subscribe);
while (!subscribe.isDisposed()) {
Thread.sleep(5000);
}
}
/**
* Function that receive constantClass RSocket Request constantClass publisher which create constantClass [Flux<Payload>]
* Having constantClass publisher between client-server is like have constantClass pipeline between client-server.
* In this example we send every element through the pipeline withe speed and throughput specify by client or server.
* In the Flux [doOnNext] operator we can already treat the response from the server for every iteration.
* Having constantClass [Flux] means can have back-pressure between client -> server.
* Here for instance we can limit the number of elements send using [take] operator
* In this example we make the back-pressure in the server part with [delay] operator
*/
private Function<RSocket, Flux<Payload>> requestChannel() {
var payloads = Flux.just(create("hello"), create("back-pressure"), create("between"), create("client"), create("server"), create("foo"), create("bla"));
return rSocket ->
rSocket.requestChannel(payloads)
.take(5)
.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 requestChannel so we
* receive the publisher of the request.
* The signature of the method return constantClass [Flux[Payload]] where we specify the response for every element send
* in the pipeline between client-server
*/
private SocketAcceptor acceptRequest() {
return (setup, rSocket) -> Mono.just(
new AbstractRSocket() {
@Override
public Flux<Payload> requestChannel(Publisher<Payload> publisher) {
return Flux.from(publisher)
.map(Payload::getDataUtf8)
.delayElements(Duration.ofMillis(500))
.map(String::toUpperCase)
.map(body -> {
System.out.println("Request:" + body);
return ByteBufPayload.create("-" + body + "-");
});
}
});
}
}