Package org.apache.avro.ipc.netty
Class NettyTransceiver
java.lang.Object
org.apache.avro.ipc.Transceiver
org.apache.avro.ipc.netty.NettyTransceiver
- All Implemented Interfaces:
Closeable,AutoCloseable
A Netty-based
Transceiver implementation.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classAvro client handler for the Netty transportprotected static classCreates threads with unique names based on a specified name prefix.protected static classA ChannelFutureListener for channel write operations that notifies aCallbackif an error occurs while writing to the channel. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a NettyTransceiver, and attempts to connect to the given address.NettyTransceiver(InetSocketAddress addr, Integer connectTimeoutMillis) Creates a NettyTransceiver, and attempts to connect to the given address.NettyTransceiver(InetSocketAddress addr, Integer connectTimeoutMillis, Consumer<io.netty.channel.socket.SocketChannel> initializer) Creates a NettyTransceiver, and attempts to connect to the given address.NettyTransceiver(InetSocketAddress addr, Integer connectTimeoutMillis, Consumer<io.netty.channel.socket.SocketChannel> initializer, Consumer<io.netty.bootstrap.Bootstrap> bootStrapInitialzier) Creates a NettyTransceiver, and attempts to connect to the given address.NettyTransceiver(InetSocketAddress addr, Consumer<io.netty.channel.socket.SocketChannel> initializer) Creates a NettyTransceiver, and attempts to connect to the given address. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this transceiver and disconnects from the remote peer.voidclose(boolean awaitCompletion) Closes this transceiver and disconnects from the remote peer.protected io.netty.channel.ChannelInboundHandlerCreates a Netty ChannelUpstreamHandler for handling events on the Netty client channel.Returns the protocol passed toTransceiver.setRemote(Protocol).booleanTrue if a handshake has been completed for this connection.voidNetty channels are thread-safe, so there is no need to acquire locks.Called by the default definition ofTransceiver.transceive(List).voidCalled with the remote protocol when a handshake has been completed.transceive(List<ByteBuffer> request) Override as non-synchronized method because the method is thread safe.voidtransceive(List<ByteBuffer> request, Callback<List<ByteBuffer>> callback) Called byRequestor.request(String,Object,Callback)for two-way messages using callbacks.voidNetty channels are thread-safe, so there is no need to acquire locks.voidwriteBuffers(List<ByteBuffer> buffers) Called byRequestor.request(String,Object)for one-way messages.
-
Field Details
-
DEFAULT_CONNECTION_TIMEOUT_MILLIS
public static final int DEFAULT_CONNECTION_TIMEOUT_MILLISIf not specified, the default connection timeout will be used (60 sec).- See Also:
-
NETTY_CONNECT_TIMEOUT_OPTION
- See Also:
-
NETTY_TCP_NODELAY_OPTION
- See Also:
-
NETTY_KEEPALIVE_OPTION
- See Also:
-
DEFAULT_TCP_NODELAY_VALUE
public static final boolean DEFAULT_TCP_NODELAY_VALUE- See Also:
-
-
Constructor Details
-
NettyTransceiver
Creates a NettyTransceiver, and attempts to connect to the given address.DEFAULT_CONNECTION_TIMEOUT_MILLISis used for the connection timeout.- Parameters:
addr- the address to connect to.- Throws:
IOException- if an error occurs connecting to the given address.
-
NettyTransceiver
Creates a NettyTransceiver, and attempts to connect to the given address.- Parameters:
addr- the address to connect to.connectTimeoutMillis- maximum amount of time to wait for connection establishment in milliseconds, or null to useDEFAULT_CONNECTION_TIMEOUT_MILLIS.- Throws:
IOException- if an error occurs connecting to the given address.
-
NettyTransceiver
public NettyTransceiver(InetSocketAddress addr, Consumer<io.netty.channel.socket.SocketChannel> initializer) throws IOException Creates a NettyTransceiver, and attempts to connect to the given address.- Parameters:
addr- the address to connect to.initializer- Consumer function to apply initial setup to the SocketChannel. Useablet to set things like SSL requirements, compression, etc...- Throws:
IOException- if an error occurs connecting to the given address.
-
NettyTransceiver
public NettyTransceiver(InetSocketAddress addr, Integer connectTimeoutMillis, Consumer<io.netty.channel.socket.SocketChannel> initializer) throws IOException Creates a NettyTransceiver, and attempts to connect to the given address.- Parameters:
addr- the address to connect to.connectTimeoutMillis- maximum amount of time to wait for connection establishment in milliseconds, or null to useDEFAULT_CONNECTION_TIMEOUT_MILLIS.initializer- Consumer function to apply initial setup to the SocketChannel. Usable to set things like SSL requirements, compression, etc...- Throws:
IOException- if an error occurs connecting to the given address.
-
NettyTransceiver
public NettyTransceiver(InetSocketAddress addr, Integer connectTimeoutMillis, Consumer<io.netty.channel.socket.SocketChannel> initializer, Consumer<io.netty.bootstrap.Bootstrap> bootStrapInitialzier) throws IOException Creates a NettyTransceiver, and attempts to connect to the given address.- Parameters:
addr- the address to connect to.connectTimeoutMillis- maximum amount of time to wait for connection establishment in milliseconds, or null to useDEFAULT_CONNECTION_TIMEOUT_MILLIS.initializer- Consumer function to apply initial setup to the SocketChannel. Usable to set things like SSL requirements, compression, etc...bootStrapInitialzier- Consumer function to apply initial setup to the Bootstrap. Usable to set things like tcp connection properties, nagle algorithm, etc...- Throws:
IOException- if an error occurs connecting to the given address.
-
-
Method Details
-
createNettyClientAvroHandler
protected io.netty.channel.ChannelInboundHandler createNettyClientAvroHandler()Creates a Netty ChannelUpstreamHandler for handling events on the Netty client channel.- Returns:
- the ChannelUpstreamHandler to use.
-
lockChannel
public void lockChannel()Netty channels are thread-safe, so there is no need to acquire locks. This method is a no-op.- Overrides:
lockChannelin classTransceiver
-
unlockChannel
public void unlockChannel()Netty channels are thread-safe, so there is no need to acquire locks. This method is a no-op.- Overrides:
unlockChannelin classTransceiver
-
close
public void close()Closes this transceiver and disconnects from the remote peer. Cancels all pending RPCs, sends an IOException to all pending callbacks, and blocks until the close has completed.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classTransceiver
-
close
public void close(boolean awaitCompletion) Closes this transceiver and disconnects from the remote peer. Cancels all pending RPCs and sends an IOException to all pending callbacks.- Parameters:
awaitCompletion- if true, will block until the close has completed.
-
getRemoteName
- Specified by:
getRemoteNamein classTransceiver- Throws:
IOException
-
transceive
Override as non-synchronized method because the method is thread safe.- Overrides:
transceivein classTransceiver- Throws:
IOException
-
transceive
public void transceive(List<ByteBuffer> request, Callback<List<ByteBuffer>> callback) throws IOException Description copied from class:TransceiverCalled byRequestor.request(String,Object,Callback)for two-way messages using callbacks.- Overrides:
transceivein classTransceiver- Throws:
IOException
-
writeBuffers
Description copied from class:TransceiverCalled byRequestor.request(String,Object)for one-way messages.- Specified by:
writeBuffersin classTransceiver- Throws:
IOException
-
readBuffers
Description copied from class:TransceiverCalled by the default definition ofTransceiver.transceive(List).- Specified by:
readBuffersin classTransceiver- Throws:
IOException
-
getRemote
Description copied from class:TransceiverReturns the protocol passed toTransceiver.setRemote(Protocol). Throws IllegalStateException by default.- Overrides:
getRemotein classTransceiver
-
isConnected
public boolean isConnected()Description copied from class:TransceiverTrue if a handshake has been completed for this connection. Used to determine whether a handshake need be completed prior to a one-way message. Requests and responses are always prefixed by handshakes, but one-way messages. If the first request sent over a connection is one-way, then a handshake-only response is returned. Subsequent one-way messages over the connection will have no response data sent. Returns false by default.- Overrides:
isConnectedin classTransceiver
-
setRemote
Description copied from class:TransceiverCalled with the remote protocol when a handshake has been completed. After this has been called and while a connection is maintained,Transceiver.isConnected()should return true and #getRemote() should return this protocol. Does nothing by default.- Overrides:
setRemotein classTransceiver
-