39
39
import reactor .core .publisher .MonoProcessor ;
40
40
import reactor .core .scheduler .Scheduler ;
41
41
import reactor .core .scheduler .Schedulers ;
42
- import reactor .ipc .netty .FutureMono ;
43
- import reactor .ipc .netty .NettyContext ;
44
- import reactor .ipc .netty .NettyInbound ;
45
- import reactor .ipc .netty .NettyOutbound ;
46
- import reactor .ipc .netty .options .ClientOptions ;
47
- import reactor .ipc .netty .resources .LoopResources ;
48
- import reactor .ipc .netty .resources .PoolResources ;
49
- import reactor .ipc .netty .tcp .TcpClient ;
42
+ import reactor .netty .Connection ;
43
+ import reactor .netty .FutureMono ;
44
+ import reactor .netty .NettyInbound ;
45
+ import reactor .netty .NettyOutbound ;
46
+ import reactor .netty .resources .ConnectionProvider ;
47
+ import reactor .netty .resources .LoopResources ;
48
+ import reactor .netty .tcp .TcpClient ;
50
49
51
50
import org .springframework .lang .Nullable ;
52
51
import org .springframework .messaging .Message ;
@@ -83,7 +82,7 @@ public class ReactorNettyTcpClient<P> implements TcpOperations<P> {
83
82
private LoopResources loopResources ;
84
83
85
84
@ Nullable
86
- private PoolResources poolResources ;
85
+ private ConnectionProvider poolResources ;
87
86
88
87
private final Scheduler scheduler = Schedulers .newParallel ("tcp-client-scheduler" );
89
88
@@ -98,57 +97,18 @@ public class ReactorNettyTcpClient<P> implements TcpOperations<P> {
98
97
* @see org.springframework.messaging.simp.stomp.StompReactorNettyCodec
99
98
*/
100
99
public ReactorNettyTcpClient (String host , int port , ReactorNettyCodec <P > codec ) {
101
- this (builder -> builder .host (host ).port (port ), codec );
102
- }
103
-
104
- /**
105
- * Constructor with a {@link ClientOptions.Builder} that can be used to
106
- * customize Reactor Netty client options.
107
- *
108
- * <p><strong>Note: </strong> this constructor manages the lifecycle of the
109
- * {@link TcpClient} and its underlying resources. Please do not customize
110
- * any of the following options:
111
- * {@link ClientOptions.Builder#channelGroup(ChannelGroup) ChannelGroup},
112
- * {@link ClientOptions.Builder#loopResources(LoopResources) LoopResources}, and
113
- * {@link ClientOptions.Builder#poolResources(PoolResources) PoolResources}.
114
- * You may set the {@link ClientOptions.Builder#disablePool() disablePool}
115
- * option if you simply want to turn off pooling.
116
- *
117
- * <p>For full control over the initialization and lifecycle of the TcpClient,
118
- * see {@link #ReactorNettyTcpClient(TcpClient, ReactorNettyCodec)}.
119
- *
120
- * @param optionsConsumer consumer to customize client options
121
- * @param codec the code to use
122
- * @see org.springframework.messaging.simp.stomp.StompReactorNettyCodec
123
- */
124
- public ReactorNettyTcpClient (Consumer <ClientOptions .Builder <?>> optionsConsumer ,
125
- ReactorNettyCodec <P > codec ) {
126
-
127
- Assert .notNull (optionsConsumer , "Consumer<ClientOptions.Builder<?> is required" );
100
+ Assert .notNull (host , "host is required" );
101
+ Assert .notNull (port , "port is required" );
128
102
Assert .notNull (codec , "ReactorNettyCodec is required" );
129
103
130
104
this .channelGroup = new DefaultChannelGroup (ImmediateEventExecutor .INSTANCE );
131
-
132
- Consumer <ClientOptions .Builder <?>> builtInConsumer = builder -> {
133
-
134
- Assert .isTrue (!builder .isLoopAvailable () && !builder .isPoolAvailable (),
135
- "The provided ClientOptions.Builder contains LoopResources and/or PoolResources. " +
136
- "Please, use the constructor that accepts a TcpClient instance " +
137
- "for full control over initialization and lifecycle." );
138
-
139
- builder .channelGroup (this .channelGroup );
140
- builder .preferNative (false );
141
-
142
- this .loopResources = LoopResources .create ("tcp-client-loop" );
143
- builder .loopResources (this .loopResources );
144
-
145
- if (!builder .isPoolDisabled ()) {
146
- this .poolResources = PoolResources .elastic ("tcp-client-pool" );
147
- builder .poolResources (this .poolResources );
148
- }
149
- };
150
-
151
- this .tcpClient = TcpClient .create (optionsConsumer .andThen (builtInConsumer ));
105
+ this .loopResources = LoopResources .create ("tcp-client-loop" );
106
+ this .poolResources = ConnectionProvider .elastic ("tcp-client-pool" );
107
+ this .tcpClient = TcpClient .create (poolResources )
108
+ .host (host )
109
+ .port (port )
110
+ .runOn (loopResources , false )
111
+ .doOnConnected (c -> channelGroup .add (c .channel ()));
152
112
this .codec = codec ;
153
113
}
154
114
@@ -181,7 +141,8 @@ public ListenableFuture<Void> connect(final TcpConnectionHandler<P> handler) {
181
141
}
182
142
183
143
Mono <Void > connectMono = this .tcpClient
184
- .newHandler (new ReactorNettyHandler (handler ))
144
+ .handle (new ReactorNettyHandler (handler ))
145
+ .connect ()
185
146
.doOnError (handler ::afterConnectFailure )
186
147
.then ();
187
148
@@ -201,11 +162,12 @@ public ListenableFuture<Void> connect(TcpConnectionHandler<P> handler, Reconnect
201
162
MonoProcessor <Void > connectMono = MonoProcessor .create ();
202
163
203
164
this .tcpClient
204
- .newHandler (new ReactorNettyHandler (handler ))
165
+ .handle (new ReactorNettyHandler (handler ))
166
+ .connect ()
205
167
.doOnNext (updateConnectMono (connectMono ))
206
168
.doOnError (updateConnectMono (connectMono ))
207
169
.doOnError (handler ::afterConnectFailure ) // report all connect failures to the handler
208
- .flatMap (NettyContext :: onClose ) // post-connect issues
170
+ .flatMap (Connection :: onDispose ) // post-connect issues
209
171
.retryWhen (reconnectFunction (strategy ))
210
172
.repeatWhen (reconnectFunction (strategy ))
211
173
.subscribe ();
@@ -302,14 +264,16 @@ private class ReactorNettyHandler implements BiFunction<NettyInbound, NettyOutbo
302
264
@ Override
303
265
@ SuppressWarnings ("unchecked" )
304
266
public Publisher <Void > apply (NettyInbound inbound , NettyOutbound outbound ) {
305
- if (logger .isDebugEnabled ()) {
306
- logger .debug ("Connected to " + inbound .remoteAddress ());
307
- }
267
+ inbound .withConnection (c -> {
268
+ if (logger .isDebugEnabled ()) {
269
+ logger .debug ("Connected to " + c .address ());
270
+ }
271
+ });
308
272
DirectProcessor <Void > completion = DirectProcessor .create ();
309
273
TcpConnection <P > connection = new ReactorNettyTcpConnection <>(inbound , outbound , codec , completion );
310
274
scheduler .schedule (() -> connectionHandler .afterConnected (connection ));
311
275
312
- inbound .context () .addHandler (new StompMessageDecoder <>(codec ));
276
+ inbound .withConnection ( c -> c .addHandler (new StompMessageDecoder <>(codec ) ));
313
277
314
278
inbound .receiveObject ()
315
279
.cast (Message .class )
0 commit comments