Package org.jboss.logmanager.handlers
Class TcpOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.jboss.logmanager.handlers.TcpOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable,FlushableCloseable
- Direct Known Subclasses:
SslTcpOutputStream
public class TcpOutputStream extends java.io.OutputStream implements FlushableCloseable
An output stream that writes data to asocket. If anIOExceptionoccurs during awrite(byte[], int, int)and asocket factorywas defined the stream will attempt to reconnect indefinitely. By default additional writes are discarded when reconnecting. If you set theblock on reconnecttotrue, 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classTcpOutputStream.RetryConnector
-
Field Summary
Fields Modifier and Type Field Description private java.net.InetAddressaddressprivate booleanblockOnReconnectprivate booleanconnectedprivate java.util.Deque<java.lang.Exception>errorsprivate static intmaxErrorsprivate static longmaxRetryTimeoutprotected java.lang.ObjectoutputLockprivate intportprivate java.lang.ThreadreconnectThreadprivate static longretryTimeoutprivate java.net.Socketsocketprivate javax.net.SocketFactorysocketFactory
-
Constructor Summary
Constructors Modifier Constructor 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.protectedTcpOutputStream(java.net.Socket socket)protectedTcpOutputStream(javax.net.SocketFactory socketFactory, java.net.InetAddress address, int port)Creates a new TCP output stream.protectedTcpOutputStream(javax.net.SocketFactory socketFactory, java.net.InetAddress address, int port, boolean blockOnReconnect)Creates a new TCP output stream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddError(java.lang.Exception e)private voidcheckReconnect()Attempts to reconnect the socket if required and allowed.voidclose()private java.lang.ThreadcreateThread()voidflush()java.util.Collection<java.lang.Exception>getErrors()Retrieves the errors occurred, if any, during a write or reconnect.booleanisBlockOnReconnect()Indicates whether or not the output stream is set to block when attempting to reconnect a TCP connection.booleanisConnected()Returns the connected state of the TCP stream.private booleanisReconnectAllowed()Invocations of this method must be locked by theoutputLock.private static voidsafeClose(java.io.Closeable closeable)voidsetBlockOnReconnect(boolean blockOnReconnect)Enables or disables blocking when attempting to reconnect the socket.voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)
-
-
-
Field Detail
-
retryTimeout
private static final long retryTimeout
- See Also:
- Constant Field Values
-
maxRetryTimeout
private static final long maxRetryTimeout
- See Also:
- Constant Field Values
-
maxErrors
private static final int maxErrors
- See Also:
- Constant Field Values
-
outputLock
protected final java.lang.Object outputLock
-
socketFactory
private final javax.net.SocketFactory socketFactory
-
address
private final java.net.InetAddress address
-
port
private final int port
-
errors
private final java.util.Deque<java.lang.Exception> errors
-
reconnectThread
private java.lang.Thread reconnectThread
-
blockOnReconnect
private boolean blockOnReconnect
-
socket
private java.net.Socket socket
-
connected
private boolean connected
-
-
Constructor Detail
-
TcpOutputStream
public TcpOutputStream(java.net.InetAddress address, int port) throws java.io.IOExceptionCreates a TCP output stream. Uses thedefault socket factoryto create the socket.- Parameters:
address- the address to connect toport- the port to connect to- Throws:
java.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.
-
TcpOutputStream
public TcpOutputStream(java.net.InetAddress address, int port, boolean blockOnReconnect) throws java.io.IOExceptionCreates a TCP output stream.Uses the
default socket factoryto create the socket.- Parameters:
address- the address to connect toport- the port to connect toblockOnReconnect-trueto block when attempting to reconnect the socket orfalseto reconnect asynchronously- Throws:
java.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.
-
TcpOutputStream
@Deprecated protected TcpOutputStream(java.net.Socket socket)
Creates a new TCP output stream. Using this constructor does not allow for any kind of reconnect if the connection is dropped.- Parameters:
socket- the socket used to write the output to
-
TcpOutputStream
protected TcpOutputStream(javax.net.SocketFactory socketFactory, java.net.InetAddress address, int port) throws java.io.IOExceptionCreates a new TCP output stream. Creates asocketfrom thesocketFactoryargument.- Parameters:
socketFactory- the factory used to create the socketaddress- the address to connect toport- the port to connect to- Throws:
java.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.
-
TcpOutputStream
protected TcpOutputStream(javax.net.SocketFactory socketFactory, java.net.InetAddress address, int port, boolean blockOnReconnect) throws java.io.IOExceptionCreates a new TCP output stream.Creates a
socketfrom thesocketFactoryargument.- Parameters:
socketFactory- the factory used to create the socketaddress- the address to connect toport- the port to connect toblockOnReconnect-trueto block when attempting to reconnect the socket orfalseto reconnect asynchronously- Throws:
java.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.
-
-
Method Detail
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException
-
isBlockOnReconnect
public boolean isBlockOnReconnect()
Indicates whether or not the output stream is set to block when attempting to reconnect a TCP connection.- Returns:
trueif blocking is enabled, otherwisefalse
-
setBlockOnReconnect
public void setBlockOnReconnect(boolean blockOnReconnect)
Enables or disables blocking when attempting to reconnect the socket. If set totruethewritemethods will block when attempting to reconnect. This is only advisable to be set totrueif using an asynchronous handler.- Parameters:
blockOnReconnect-trueto block when reconnecting orfalseto reconnect asynchronously discarding any new messages coming in
-
isConnected
public boolean isConnected()
Returns the connected state of the TCP stream. The stream is said to be disconnected when anIOExceptionoccurs during a write. Otherwise a stream is considered connected.- Returns:
trueif the stream is connected, otherwisefalse
-
getErrors
public java.util.Collection<java.lang.Exception> getErrors()
Retrieves the errors occurred, if any, during a write or reconnect.- Returns:
- a collection of errors or an empty list
-
addError
private void addError(java.lang.Exception e)
-
isReconnectAllowed
private boolean isReconnectAllowed()
Invocations of this method must be locked by theoutputLock.
-
checkReconnect
private void checkReconnect()
Attempts to reconnect the socket if required and allowed. Invocations of this method must be locked by theoutputLock.
-
createThread
private java.lang.Thread createThread()
-
safeClose
private static void safeClose(java.io.Closeable closeable)
-
-