Package org.apache.thrift.transport
Class TNonblockingSocket
- java.lang.Object
-
- org.apache.thrift.transport.TTransport
-
- org.apache.thrift.transport.TNonblockingTransport
-
- org.apache.thrift.transport.TNonblockingSocket
-
public class TNonblockingSocket extends TNonblockingTransport
Transport for use with async client.
-
-
Constructor Summary
Constructors Constructor Description TNonblockingSocket(java.lang.String host, int port)
TNonblockingSocket(java.lang.String host, int port, int timeout)
Create a new nonblocking socket transport that will be connected to host:port.TNonblockingSocket(java.nio.channels.SocketChannel socketChannel)
Constructor that takes an already created socket.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the socket.boolean
finishConnect()
Non-blocking connection completion.void
flush()
Noop.java.nio.channels.SocketChannel
getSocketChannel()
Returns a reference to the underlying SocketChannel.boolean
isOpen()
Checks whether the socket is connected.void
open()
Do not call, the implementation provides its own lazy non-blocking connect.int
read(byte[] buf, int off, int len)
Reads from the underlying input stream if not null.int
read(java.nio.ByteBuffer buffer)
Perform a nonblocking read into buffer.java.nio.channels.SelectionKey
registerSelector(java.nio.channels.Selector selector, int interests)
Register the new SocketChannel with our Selector, indicating we'd like to be notified when it's ready for I/O.void
setTimeout(int timeout)
Sets the socket timeout, although this implementation never uses blocking operations so it is unused.boolean
startConnect()
Non-blocking connection initialization.void
write(byte[] buf, int off, int len)
Writes to the underlying output stream if not null.int
write(java.nio.ByteBuffer buffer)
Perform a nonblocking write of the data in buffer;-
Methods inherited from class org.apache.thrift.transport.TTransport
consumeBuffer, getBuffer, getBufferPosition, getBytesRemainingInBuffer, peek, readAll, write
-
-
-
-
Constructor Detail
-
TNonblockingSocket
public TNonblockingSocket(java.lang.String host, int port) throws java.io.IOException
- Throws:
java.io.IOException
-
TNonblockingSocket
public TNonblockingSocket(java.lang.String host, int port, int timeout) throws java.io.IOException
Create a new nonblocking socket transport that will be connected to host:port.- Parameters:
host
-port
-- Throws:
TTransportException
java.io.IOException
-
TNonblockingSocket
public TNonblockingSocket(java.nio.channels.SocketChannel socketChannel) throws java.io.IOException
Constructor that takes an already created socket.- Parameters:
socketChannel
- Already created SocketChannel object- Throws:
java.io.IOException
- if there is an error setting up the streams
-
-
Method Detail
-
registerSelector
public java.nio.channels.SelectionKey registerSelector(java.nio.channels.Selector selector, int interests) throws java.io.IOException
Register the new SocketChannel with our Selector, indicating we'd like to be notified when it's ready for I/O.- Specified by:
registerSelector
in classTNonblockingTransport
- Parameters:
selector
-- Returns:
- the selection key for this socket.
- Throws:
java.io.IOException
-
setTimeout
public void setTimeout(int timeout)
Sets the socket timeout, although this implementation never uses blocking operations so it is unused.- Parameters:
timeout
- Milliseconds timeout
-
getSocketChannel
public java.nio.channels.SocketChannel getSocketChannel()
Returns a reference to the underlying SocketChannel.
-
isOpen
public boolean isOpen()
Checks whether the socket is connected.- Specified by:
isOpen
in classTTransport
- Returns:
- True if the transport is open.
-
open
public void open() throws TTransportException
Do not call, the implementation provides its own lazy non-blocking connect.- Specified by:
open
in classTTransport
- Throws:
TTransportException
- if the transport could not be opened
-
read
public int read(java.nio.ByteBuffer buffer) throws java.io.IOException
Perform a nonblocking read into buffer.- Specified by:
read
in classTNonblockingTransport
- Throws:
java.io.IOException
-
read
public int read(byte[] buf, int off, int len) throws TTransportException
Reads from the underlying input stream if not null.- Specified by:
read
in classTTransport
- Parameters:
buf
- Array to read intooff
- Index to start reading atlen
- Maximum number of bytes to read- Returns:
- The number of bytes actually read
- Throws:
TTransportException
- if there was an error reading data
-
write
public int write(java.nio.ByteBuffer buffer) throws java.io.IOException
Perform a nonblocking write of the data in buffer;- Specified by:
write
in classTNonblockingTransport
- Throws:
java.io.IOException
-
write
public void write(byte[] buf, int off, int len) throws TTransportException
Writes to the underlying output stream if not null.- Specified by:
write
in classTTransport
- Parameters:
buf
- The output data bufferoff
- The offset to start writing fromlen
- The number of bytes to write- Throws:
TTransportException
- if there was an error writing data
-
flush
public void flush() throws TTransportException
Noop.- Overrides:
flush
in classTTransport
- Throws:
TTransportException
- if there was an error writing out data.
-
close
public void close()
Closes the socket.- Specified by:
close
in classTTransport
-
startConnect
public boolean startConnect() throws java.io.IOException
Non-blocking connection initialization.- Specified by:
startConnect
in classTNonblockingTransport
- Throws:
java.io.IOException
- See Also:
SocketChannel.connect(SocketAddress remote)
-
finishConnect
public boolean finishConnect() throws java.io.IOException
Non-blocking connection completion.- Specified by:
finishConnect
in classTNonblockingTransport
- Throws:
java.io.IOException
- See Also:
SocketChannel.finishConnect()
-
-