Top Description Inners Fields Constructors Methods
org.springframework.messaging.tcp.reactor

public Class ReactorNetty2TcpClient<P>

extends Object
implements TcpOperations<P>
Class Inheritance
All Implemented Interfaces
org.springframework.messaging.tcp.TcpOperations
Type Parameters
<P>
the type of payload for in and outbound messages
Imports
java.nio.ByteBuffer, java.time.Duration, java.util.List, java.util.concurrent.CompletableFuture, java.util.function.BiFunction, .Function, io.netty5.buffer.Buffer, io.netty5.channel.ChannelHandlerContext, io.netty5.channel.group.ChannelGroup, .DefaultChannelGroup, io.netty5.handler.codec.ByteToMessageDecoder, io.netty5.util.concurrent.ImmediateEventExecutor, org.apache.commons.logging.Log, .LogFactory, org.reactivestreams.Publisher, reactor.core.publisher.Mono, .Sinks, reactor.core.scheduler.Scheduler, .Schedulers, reactor.netty5.Connection, .NettyInbound, .NettyOutbound, reactor.netty5.resources.ConnectionProvider, .LoopResources, reactor.netty5.tcp.TcpClient, reactor.util.retry.Retry, org.springframework.lang.Nullable, org.springframework.messaging.Message, org.springframework.messaging.tcp.ReconnectStrategy, .TcpConnection, .TcpConnectionHandler, .TcpOperations, org.springframework.util.Assert

Reactor Netty based implementation of TcpOperations.

This class is based on ReactorNettyTcpClient.

Author
Rossen Stoyanchev
Since
6.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static class

Field Summary

Modifier and TypeField and Description
private final ChannelGroup
private final TcpMessageCodec<P>
private Log
private final LoopResources
private final ConnectionProvider
private static final int
private final Scheduler
private volatile boolean
private final TcpClient

Constructor Summary

AccessConstructor and Description
public
ReactorNetty2TcpClient(String
the host to connect to
host
,
int
the port to connect to
port
,
TcpMessageCodec<P>
for encoding and decoding the input/output byte streams
codec
)

Simple constructor with the host and port to use to connect to.

public
ReactorNetty2TcpClient(Function<TcpClient, TcpClient>
the configurer function
clientConfigurer
,
TcpMessageCodec<P>
for encoding and decoding the input/output byte streams
codec
)

A variant of ReactorNetty2TcpClient(String, int, TcpMessageCodec) that still manages the lifecycle of the TcpClient and underlying resources, but allows for direct configuration of other properties of the client through a Function<TcpClient, TcpClient>.

public
ReactorNetty2TcpClient(TcpClient
the TcpClient instance to use
tcpClient
,
TcpMessageCodec<P>
for encoding and decoding the input/output byte streams
codec
)

Constructor with an externally created TcpClient instance whose lifecycle is expected to be managed externally.

Method Summary

Modifier and TypeMethod and Description
public CompletableFuture<Void>
connectAsync(TcpConnectionHandler<P>
a handler to manage the connection
handler
)

Implements org.springframework.messaging.tcp.TcpOperations.connectAsync.

Open a new connection.

public CompletableFuture<Void>
connectAsync(TcpConnectionHandler<P>
a handler to manage the connection
handler
,
ReconnectStrategy
a strategy for reconnecting
strategy
)

Implements org.springframework.messaging.tcp.TcpOperations.connectAsync.

Open a new connection and a strategy for reconnecting if the connection fails.

protected TcpClient

Returns:

the same handler or an updated instance
extendTcpClient
(TcpClient
the candidate TcpClient
tcpClient
,
TcpConnectionHandler<P>
the handler for the TCP connection
handler
)

Provides an opportunity to initialize the TcpClient for the given TcpConnectionHandler which may implement sub-interfaces such as org.springframework.messaging.simp.stomp.StompTcpConnectionHandler that expose further information.

public Log
getLogger()

Return the currently configured Logger.

private CompletableFuture<Void>
private Publisher<? extends Long>
reconnect(Integer attempt, ReconnectStrategy reconnectStrategy)

public void
setLogger(Log
the logger to use
logger
)

Set an alternative logger to use than the one based on the class name.

public CompletableFuture<Void>
shutdownAsync()

Implements org.springframework.messaging.tcp.TcpOperations.shutdownAsync.

Shut down and close any open connections.

private Mono<Void>
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

channelGroupback to summary
private final ChannelGroup channelGroup
Annotations
@Nullable
codecback to summary
private final TcpMessageCodec<P> codec
loggerback to summary
private Log logger
loopResourcesback to summary
private final LoopResources loopResources
Annotations
@Nullable
poolResourcesback to summary
private final ConnectionProvider poolResources
Annotations
@Nullable
PUBLISH_ON_BUFFER_SIZEback to summary
private static final int PUBLISH_ON_BUFFER_SIZE
schedulerback to summary
private final Scheduler scheduler
stoppingback to summary
private volatile boolean stopping
tcpClientback to summary
private final TcpClient tcpClient

Constructor Detail

ReactorNetty2TcpClientback to summary
public ReactorNetty2TcpClient(String host, int port, TcpMessageCodec<P> codec)

Simple constructor with the host and port to use to connect to.

This constructor manages the lifecycle of the TcpClient and underlying resources such as ConnectionProvider, LoopResources, and ChannelGroup.

For full control over the initialization and lifecycle of the TcpClient, use ReactorNetty2TcpClient(TcpClient, TcpMessageCodec).

Parameters
host:String

the host to connect to

port:int

the port to connect to

codec:TcpMessageCodec<P>

for encoding and decoding the input/output byte streams

See Also
org.springframework.messaging.simp.stomp.StompReactorNettyCodec
ReactorNetty2TcpClientback to summary
public ReactorNetty2TcpClient(Function<TcpClient, TcpClient> clientConfigurer, TcpMessageCodec<P> codec)

A variant of ReactorNetty2TcpClient(String, int, TcpMessageCodec) that still manages the lifecycle of the TcpClient and underlying resources, but allows for direct configuration of other properties of the client through a Function<TcpClient, TcpClient>.

Parameters
clientConfigurer:Function<TcpClient, TcpClient>

the configurer function

codec:TcpMessageCodec<P>

for encoding and decoding the input/output byte streams

Since
5.1.3
See Also
org.springframework.messaging.simp.stomp.StompReactorNettyCodec
ReactorNetty2TcpClientback to summary
public ReactorNetty2TcpClient(TcpClient tcpClient, TcpMessageCodec<P> codec)

Constructor with an externally created TcpClient instance whose lifecycle is expected to be managed externally.

Parameters
tcpClient:TcpClient

the TcpClient instance to use

codec:TcpMessageCodec<P>

for encoding and decoding the input/output byte streams

See Also
org.springframework.messaging.simp.stomp.StompReactorNettyCodec

Method Detail

connectAsyncback to summary
public CompletableFuture<Void> connectAsync(TcpConnectionHandler<P> handler)

Implements org.springframework.messaging.tcp.TcpOperations.connectAsync.

Doc from org.springframework.messaging.tcp.TcpOperations.connectAsync.

Open a new connection.

Parameters
handler:TcpConnectionHandler<P>

a handler to manage the connection

Returns:CompletableFuture<Void>

a CompletableFuture that can be used to determine when and if the connection is successfully established

Annotations
@Override
connectAsyncback to summary
public CompletableFuture<Void> connectAsync(TcpConnectionHandler<P> handler, ReconnectStrategy strategy)

Implements org.springframework.messaging.tcp.TcpOperations.connectAsync.

Doc from org.springframework.messaging.tcp.TcpOperations.connectAsync.

Open a new connection and a strategy for reconnecting if the connection fails.

Parameters
handler:TcpConnectionHandler<P>

a handler to manage the connection

strategy:ReconnectStrategy

a strategy for reconnecting

Returns:CompletableFuture<Void>

a CompletableFuture that can be used to determine when and if the initial connection is successfully established

Annotations
@Override
extendTcpClientback to summary
protected TcpClient extendTcpClient(TcpClient tcpClient, TcpConnectionHandler<P> handler)

Provides an opportunity to initialize the TcpClient for the given TcpConnectionHandler which may implement sub-interfaces such as org.springframework.messaging.simp.stomp.StompTcpConnectionHandler that expose further information.

Parameters
tcpClient:TcpClient

the candidate TcpClient

handler:TcpConnectionHandler<P>

the handler for the TCP connection

Returns:TcpClient

the same handler or an updated instance

getLoggerback to summary
public Log getLogger()

Return the currently configured Logger.

Since
5.1
handleShuttingDownConnectFailureback to summary
private CompletableFuture<Void> handleShuttingDownConnectFailure(TcpConnectionHandler<P> handler)
reconnectback to summary
private Publisher<? extends Long> reconnect(Integer attempt, ReconnectStrategy reconnectStrategy)
setLoggerback to summary
public void setLogger(Log logger)

Set an alternative logger to use than the one based on the class name.

Parameters
logger:Log

the logger to use

Since
5.1
shutdownAsyncback to summary
public CompletableFuture<Void> shutdownAsync()

Implements org.springframework.messaging.tcp.TcpOperations.shutdownAsync.

Doc from org.springframework.messaging.tcp.TcpOperations.shutdownAsync.

Shut down and close any open connections.

Returns:CompletableFuture<Void>

a CompletableFuture that can be used to determine when and if the connection is successfully closed

Annotations
@Override
stopSchedulerback to summary
private Mono<Void> stopScheduler()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.messaging.tcp.reactor back to summary

private Class ReactorNetty2TcpClient.ReactorNettyHandler

extends Object
implements BiFunction<NettyInbound, NettyOutbound, Publisher<Void>>
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.tcp.reactor.ReactorNetty2TcpClient.ReactorNettyHandler
All Implemented Interfaces
java.util.function.BiFunction

Field Summary

Modifier and TypeField and Description
private final TcpConnectionHandler<P>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Publisher<Void>
apply(NettyInbound
the first function argument
inbound
,
NettyOutbound
the second function argument
outbound
)

Implements java.util.function.BiFunction.apply.

Applies this function to the given arguments.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectionHandlerback to summary
private final TcpConnectionHandler<P> connectionHandler

Constructor Detail

ReactorNettyHandlerback to summary
pack-priv ReactorNettyHandler(TcpConnectionHandler<P> handler)

Method Detail

applyback to summary
public Publisher<Void> apply(NettyInbound inbound, NettyOutbound outbound)

Implements java.util.function.BiFunction.apply.

Doc from java.util.function.BiFunction.apply.

Applies this function to the given arguments.

Parameters
inbound:NettyInbound

the first function argument

outbound:NettyOutbound

the second function argument

Returns:Publisher<Void>

the function result

Annotations
@Override
@SuppressWarnings:unchecked
org.springframework.messaging.tcp.reactor back to summary

private Class ReactorNetty2TcpClient.StompMessageDecoder<P>

extends ByteToMessageDecoder
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final TcpMessageCodec<P>
Inherited from io.netty5.handler.codec.ByteToMessageDecoder:
COMPOSITE_CUMULATORMERGE_CUMULATOR

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
protected void
decode(ChannelHandlerContext ctx, Buffer
the Buffer from which to read data
buffer
)

Implements abstract io.netty5.handler.codec.ByteToMessageDecoder.decode.

Decode the from one Buffer to another.

Inherited from io.netty5.handler.codec.ByteToMessageDecoder:
actualReadableByteschannelInactivechannelReadchannelReadCompletechannelShutdowndecodeLastdiscardSomeReadByteshandlerAddedhandlerAdded0handlerRemovedhandlerRemoved0internalBufferisSharableisSingleDecodesetSingleDecode

Field Detail

codecback to summary
private final TcpMessageCodec<P> codec

Constructor Detail

StompMessageDecoderback to summary
pack-priv StompMessageDecoder(TcpMessageCodec<P> codec)

Method Detail

decodeback to summary
protected void decode(ChannelHandlerContext ctx, Buffer buffer) throws Exception

Implements abstract io.netty5.handler.codec.ByteToMessageDecoder.decode.

Doc from io.netty5.handler.codec.ByteToMessageDecoder.decode.

Decode the from one Buffer to another. This method will be called till either the input Buffer has nothing to read when return from this method or till nothing was read from the input Buffer.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this ByteToMessageDecoder belongs to

buffer:Buffer

the Buffer from which to read data

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs