Top Description Inners Fields Constructors Methods
org.jboss.logmanager.handlers

public Class TcpOutputStream

extends OutputStream
implements AutoCloseable, Flushable
Class Inheritance
All Implemented Interfaces
java.io.Flushable, java.lang.AutoCloseable
Known Direct Subclasses
org.jboss.logmanager.handlers.SslTcpOutputStream
Annotations
@SuppressWarnings:unused, WeakerAccess
Imports
java.io.Closeable, .Flushable, .IOException, .OutputStream, java.net.InetAddress, .Socket, .SocketException, java.util.ArrayDeque, .ArrayList, .Collection, .Collections, .Deque, .List, java.util.concurrent.TimeUnit, java.util.concurrent.locks.ReentrantLock, javax.net.SocketFactory

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.

Author
James R. Perkins

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private boolean
private boolean
private final Deque<Exception>
private static final int
private static final long
protected final ReentrantLock
private Thread
private static final long
private Socket
private final ClientSocketFactory

Constructor Summary

AccessConstructor and Description
public
TcpOutputStream(final InetAddress
the address to connect to
address
,
final int
the port to connect to
port
)

Creates a TCP output stream.

public
TcpOutputStream(final InetAddress
the address to connect to
address
,
final int
the port to connect to
port
,
final boolean
true to block when attempting to reconnect the socket or false to reconnect asynchronously
blockOnReconnect
)

Creates a TCP output stream.

protected
TcpOutputStream(final Socket
the socket used to write the output to
socket
)

Creates a new TCP output stream.

protected
TcpOutputStream(final SocketFactory
the factory used to create the socket
socketFactory
,
final InetAddress
the address to connect to
address
,
final int
the port to connect to
port
)

Creates a new TCP output stream.

protected
TcpOutputStream(final SocketFactory
the factory used to create the socket
socketFactory
,
final InetAddress
the address to connect to
address
,
final int
the port to connect to
port
,
final boolean
true to block when attempting to reconnect the socket or false to reconnect asynchronously
blockOnReconnect
)

Creates a new TCP output stream.

public
TcpOutputStream(final ClientSocketFactory
the socket factory used to create TCP sockets
socketFactory
,
final boolean
true to block when attempting to reconnect the socket or false to reconnect asynchronously
blockOnReconnect
)

Creates a new TCP stream which uses the ClientSocketFactory#createSocket() to create the socket.

Method Summary

Modifier and TypeMethod and Description
private void
private void
checkReconnect()

Attempts to reconnect the socket if required and allowed.

public void
close()

Overrides java.io.OutputStream.close.

Implements java.lang.AutoCloseable.close, java.io.Closeable.close.

Closes this resource, relinquishing any underlying resources.

private Thread
public void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.

public Collection<Exception>

Returns:

a collection of errors or an empty list
getErrors
()

Retrieves the errors occurred, if any, during a write or reconnect.

public boolean

Returns:

true if blocking is enabled, otherwise false
isBlockOnReconnect
()

Indicates whether or not the output stream is set to block when attempting to reconnect a TCP connection.

public boolean

Returns:

true if the stream is connected, otherwise false
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(final Closeable closeable)

public void
setBlockOnReconnect(final boolean
true to block when reconnecting or false to reconnect asynchronously discarding any new messages coming in
blockOnReconnect
)

Enables or disables blocking when attempting to reconnect the socket.

public void
write(final int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.

public void
write(final byte[]
the data.
b
)

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream.

public void
write(final byte[]
the data.
b
,
final int
the start offset in the data.
off
,
final int
the number of bytes to write.
len
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

Inherited from java.io.OutputStream:
nullOutputStream

Field Detail

blockOnReconnectback to summary
private boolean blockOnReconnect
connectedback to summary
private boolean connected
errorsback to summary
private final Deque<Exception> errors
maxErrorsback to summary
private static final int maxErrors
maxRetryTimeoutback to summary
private static final long maxRetryTimeout
outputLockback to summary
protected final ReentrantLock outputLock
reconnectThreadback to summary
private Thread reconnectThread
retryTimeoutback to summary
private static final long retryTimeout
socketback to summary
private Socket socket
socketFactoryback to summary
private final ClientSocketFactory socketFactory

Constructor Detail

TcpOutputStreamback to summary
public TcpOutputStream(final InetAddress address, final int port) throws IOException

Creates a TCP output stream.

Uses the default socket factory to create the socket.

Parameters
address:InetAddress

the address to connect to

port:int

the port to connect to

Exceptions
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.
TcpOutputStreamback to summary
public TcpOutputStream(final InetAddress address, final int port, final boolean blockOnReconnect) throws IOException

Creates a TCP output stream.

Uses the default socket factory to create the socket.

Parameters
address:InetAddress

the address to connect to

port:int

the port to connect to

blockOnReconnect:boolean

true to block when attempting to reconnect the socket or false to reconnect asynchronously

Exceptions
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.
TcpOutputStreamback to summary
protected TcpOutputStream(final 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:Socket

the socket used to write the output to

Annotations
@Deprecated
TcpOutputStreamback to summary
protected TcpOutputStream(final SocketFactory socketFactory, final InetAddress address, final int port) throws IOException

Creates a new TCP output stream.

Creates a socket from the socketFactory argument.

Parameters
socketFactory:SocketFactory

the factory used to create the socket

address:InetAddress

the address to connect to

port:int

the port to connect to

Exceptions
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.
TcpOutputStreamback to summary
protected TcpOutputStream(final SocketFactory socketFactory, final InetAddress address, final int port, final boolean blockOnReconnect) throws IOException

Creates a new TCP output stream.

Creates a socket from the socketFactory argument.

Parameters
socketFactory:SocketFactory

the factory used to create the socket

address:InetAddress

the address to connect to

port:int

the port to connect to

blockOnReconnect:boolean

true to block when attempting to reconnect the socket or false to reconnect asynchronously

Exceptions
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.
TcpOutputStreamback to summary
public TcpOutputStream(final ClientSocketFactory socketFactory, final boolean blockOnReconnect)

Creates a new TCP stream which uses the ClientSocketFactory#createSocket() to create the socket.

Parameters
socketFactory:ClientSocketFactory

the socket factory used to create TCP sockets

blockOnReconnect:boolean

true to block when attempting to reconnect the socket or false to reconnect asynchronously

Method Detail

addErrorback to summary
private void addError(final Exception e)
checkReconnectback to summary
private void checkReconnect()

Attempts to reconnect the socket if required and allowed. Invocations of this method must be locked by the outputLock.

closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.lang.AutoCloseable.close, java.io.Closeable.close.

Doc from java.lang.AutoCloseable.close.

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.OutputStream.close.

if an I/O error occurs.

createThreadback to summary
private Thread createThread()
flushback to summary
public void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getErrorsback to summary
public Collection<Exception> getErrors()

Retrieves the errors occurred, if any, during a write or reconnect.

Returns:Collection<Exception>

a collection of errors or an empty list

isBlockOnReconnectback to summary
public boolean isBlockOnReconnect()

Indicates whether or not the output stream is set to block when attempting to reconnect a TCP connection.

Returns:boolean

true if blocking is enabled, otherwise false

isConnectedback to summary
public boolean isConnected()

Returns the connected state of the TCP stream.

The stream is said to be disconnected when an IOException occurs during a write. Otherwise a stream is considered connected.

Returns:boolean

true if the stream is connected, otherwise false

isReconnectAllowedback to summary
private boolean isReconnectAllowed()

Invocations of this method must be locked by the outputLock.

safeCloseback to summary
private static void safeClose(final Closeable closeable)
setBlockOnReconnectback to summary
public void setBlockOnReconnect(final boolean blockOnReconnect)

Enables or disables blocking when attempting to reconnect the socket.

If set to true the write methods will block when attempting to reconnect. This is only advisable to be set to true if using an asynchronous handler.

Parameters
blockOnReconnect:boolean

true to block when reconnecting or false to reconnect asynchronously discarding any new messages coming in

writeback to summary
public void write(final int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
b:int

the byte.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public void write(final byte[] b) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters
b:byte[]

the data.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
org.jboss.logmanager.handlers back to summary

private Class TcpOutputStream.RetryConnector

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

Field Summary

Modifier and TypeField and Description
private int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attemptsback to summary
private int attempts

Constructor Detail

RetryConnectorback to summary
private RetryConnector()

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override