public class TcpOutputStream extends java.io.OutputStream implements FlushableCloseable
socket
.
If an IOException
occurs during a write(byte[], int, int)
and a socket factory
was defined the stream will attempt to reconnect indefinitely. By default
additional writes are discarded when reconnecting. If you set the block on
reconnect
to true
, then the reconnect will indefinitely block until the TCP stream is reconnected.
You can optionally get a collection of the errors that occurred during a write or reconnect.Modifier and Type | Class and Description |
---|---|
private class |
TcpOutputStream.RetryConnector |
Modifier and Type | Field and Description |
---|---|
private java.net.InetAddress |
address |
private boolean |
blockOnReconnect |
private boolean |
connected |
private java.util.Deque<java.lang.Exception> |
errors |
private static int |
maxErrors |
private static long |
maxRetryTimeout |
protected java.lang.Object |
outputLock |
private int |
port |
private java.lang.Thread |
reconnectThread |
private static long |
retryTimeout |
private java.net.Socket |
socket |
private javax.net.SocketFactory |
socketFactory |
Modifier | Constructor and Description |
---|---|
|
TcpOutputStream(java.net.InetAddress address,
int port)
Creates a TCP output stream.
|
|
TcpOutputStream(java.net.InetAddress address,
int port,
boolean blockOnReconnect)
Creates a TCP output stream.
|
protected |
TcpOutputStream(java.net.Socket socket)
|
protected |
TcpOutputStream(javax.net.SocketFactory socketFactory,
java.net.InetAddress address,
int port)
Creates a new TCP output stream.
|
protected |
TcpOutputStream(javax.net.SocketFactory socketFactory,
java.net.InetAddress address,
int port,
boolean blockOnReconnect)
Creates a new TCP output stream.
|
Modifier and Type | Method and Description |
---|---|
private void |
addError(java.lang.Exception e) |
private void |
checkReconnect()
Attempts to reconnect the socket if required and allowed.
|
void |
close() |
private java.lang.Thread |
createThread() |
void |
flush() |
java.util.Collection<java.lang.Exception> |
getErrors()
Retrieves the errors occurred, if any, during a write or reconnect.
|
boolean |
isBlockOnReconnect()
Indicates whether or not the output stream is set to block when attempting to reconnect a TCP connection.
|
boolean |
isConnected()
Returns the connected state of the TCP stream.
|
private boolean |
isReconnectAllowed()
Invocations of this method must be locked by the
outputLock . |
private static void |
safeClose(java.io.Closeable closeable) |
void |
setBlockOnReconnect(boolean blockOnReconnect)
Enables or disables blocking when attempting to reconnect the socket.
|
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
private static final long retryTimeout
private static final long maxRetryTimeout
private static final int maxErrors
protected final java.lang.Object outputLock
private final javax.net.SocketFactory socketFactory
private final java.net.InetAddress address
private final int port
private final java.util.Deque<java.lang.Exception> errors
private java.lang.Thread reconnectThread
private boolean blockOnReconnect
private java.net.Socket socket
private boolean connected
public TcpOutputStream(java.net.InetAddress address, int port) throws java.io.IOException
default socket factory
to create the socket.address
- the address to connect toport
- the port to connect tojava.io.IOException
- no longer throws an exception. If an exception occurs while attempting to connect the socket
a reconnect will be attempted on the next write.public TcpOutputStream(java.net.InetAddress address, int port, boolean blockOnReconnect) throws java.io.IOException
Uses the default socket factory
to create the socket.
address
- the address to connect toport
- the port to connect toblockOnReconnect
- true
to block when attempting to reconnect the socket or false
to
reconnect asynchronouslyjava.io.IOException
- no longer throws an exception. If an exception occurs while attempting to connect the socket
a reconnect will be attempted on the next write.@Deprecated protected TcpOutputStream(java.net.Socket socket)
socket
- the socket used to write the output toprotected TcpOutputStream(javax.net.SocketFactory socketFactory, java.net.InetAddress address, int port) throws java.io.IOException
socket
from the socketFactory
argument.socketFactory
- the factory used to create the socketaddress
- the address to connect toport
- the port to connect tojava.io.IOException
- no longer throws an exception. If an exception occurs while attempting to connect the socket
a reconnect will be attempted on the next write.protected TcpOutputStream(javax.net.SocketFactory socketFactory, java.net.InetAddress address, int port, boolean blockOnReconnect) throws java.io.IOException
Creates a socket
from the socketFactory
argument.
socketFactory
- the factory used to create the socketaddress
- the address to connect toport
- the port to connect toblockOnReconnect
- true
to block when attempting to reconnect the socket or false
to
reconnect asynchronouslyjava.io.IOException
- no longer throws an exception. If an exception occurs while attempting to connect the socket
a reconnect will be attempted on the next write.public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] b) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
public boolean isBlockOnReconnect()
true
if blocking is enabled, otherwise false
public void setBlockOnReconnect(boolean blockOnReconnect)
true
the write
methods will block when attempting to reconnect. This is only advisable
to be set to true
if using an asynchronous handler.blockOnReconnect
- true
to block when reconnecting or false
to reconnect asynchronously
discarding any new messages coming inpublic boolean isConnected()
IOException
occurs during a write. Otherwise a
stream is considered connected.true
if the stream is connected, otherwise false
public java.util.Collection<java.lang.Exception> getErrors()
private void addError(java.lang.Exception e)
private boolean isReconnectAllowed()
outputLock
.private void checkReconnect()
outputLock
.private java.lang.Thread createThread()
private static void safeClose(java.io.Closeable closeable)