Class TcpOutputStream
java.lang.Object
java.io.OutputStream
org.jboss.logmanager.handlers.TcpOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, FlushableCloseable
- Direct Known Subclasses:
SslTcpOutputStream
An output stream that writes data to a
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.-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final InetAddressprivate booleanprivate booleanprivate static final intprivate static final longprotected final Objectprivate final intprivate Threadprivate static final longprivate Socketprivate final SocketFactory -
Constructor Summary
ConstructorsModifierConstructorDescriptionTcpOutputStream(InetAddress address, int port) Creates a TCP output stream.TcpOutputStream(InetAddress address, int port, boolean blockOnReconnect) Creates a TCP output stream.protectedTcpOutputStream(Socket socket) Deprecated.protectedTcpOutputStream(SocketFactory socketFactory, InetAddress address, int port) Creates a new TCP output stream.protectedTcpOutputStream(SocketFactory socketFactory, InetAddress address, int port, boolean blockOnReconnect) Creates a new TCP output stream. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidprivate voidAttempts to reconnect the socket if required and allowed.voidclose()private Threadvoidflush()Retrieves the errors occurred, if any, during a write or reconnect.booleanIndicates whether or not the output stream is set to block when attempting to reconnect a TCP connection.booleanReturns the connected state of the TCP stream.private booleanInvocations of this method must be locked by theoutputLock.private static voidvoidsetBlockOnReconnect(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) Methods inherited from class OutputStream
nullOutputStream
-
Field Details
-
retryTimeout
private static final long retryTimeout- See Also:
-
maxRetryTimeout
private static final long maxRetryTimeout- See Also:
-
maxErrors
private static final int maxErrors- See Also:
-
outputLock
-
socketFactory
-
address
-
port
private final int port -
errors
-
reconnectThread
-
blockOnReconnect
private boolean blockOnReconnect -
socket
-
connected
private boolean connected
-
-
Constructor Details
-
TcpOutputStream
Creates a TCP output stream. Uses thedefault socket factoryto create the socket.- Parameters:
address- the address to connect toport- the port to connect to- Throws:
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
Creates 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:
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
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(SocketFactory socketFactory, InetAddress address, int port) throws IOException Creates 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:
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(SocketFactory socketFactory, InetAddress address, int port, boolean blockOnReconnect) throws IOException Creates 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:
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 Details
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
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
Retrieves the errors occurred, if any, during a write or reconnect.- Returns:
- a collection of errors or an empty list
-
addError
-
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
-
safeClose
-
TcpOutputStream(javax.net.SocketFactory, java.net.InetAddress, int)