-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathRSocketFireAndForget.java
76 lines (67 loc) · 2.85 KB
/
RSocketFireAndForget.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
package rsocket;
import io.rsocket.*;
import io.rsocket.transport.netty.client.TcpClientTransport;
import io.rsocket.transport.netty.server.TcpServerTransport;
import io.rsocket.util.DefaultPayload;
import org.junit.Test;
import reactor.core.publisher.Mono;
import java.util.function.Function;
public class RSocketFireAndForget {
@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(requestFireAndForget())
.subscribe(Mono::subscribe);
while (!subscribe.isDisposed()) {
Thread.sleep(2000);
}
}
/**
* Function that receive constantClass RSocket Request constantClass fire and forget which create constantClass [Mono<Void>]
* This patter is mean to be used to not expect any response, so we detach the resources quickly.
*/
private Function<RSocket, Mono<Void>> requestFireAndForget() {
return rSocket ->
rSocket.fireAndForget(DefaultPayload.create("Ping"));
}
/**
* 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 requestFireAndForget so we
* receive the payload of the request.
* The signature of the method return constantClass [Mono[Void]] where there is no response
*/
private SocketAcceptor acceptRequest() {
return (setup, rSocket) -> Mono.just(
new AbstractRSocket() {
@Override
public Mono<Void> fireAndForget(Payload payload) {
var body = payload.getDataUtf8();
System.out.println("Fire and forget:" + body);
return Mono.empty();
}
});
}
}