Package org.jboss.logmanager.handlers
Class TcpOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.jboss.logmanager.handlers.TcpOutputStream
-
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
,FlushableCloseable
- Direct Known Subclasses:
SslTcpOutputStream
public class TcpOutputStream extends OutputStream implements FlushableCloseable
An output stream that writes data to asocket
. If anIOException
occurs during awrite(byte[], int, int)
and asocket factory
was defined the stream will attempt to reconnect indefinitely. By default additional writes are discarded when reconnecting. If you set theblock on reconnect
totrue
, 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
-
-
Field Summary
Fields Modifier and Type Field Description protected Object
outputLock
-
Constructor Summary
Constructors Modifier Constructor Description TcpOutputStream(InetAddress address, int port)
Creates a TCP output stream.TcpOutputStream(InetAddress address, int port, boolean blockOnReconnect)
Creates a TCP output stream.protected
TcpOutputStream(Socket socket)
Deprecated.protected
TcpOutputStream(SocketFactory socketFactory, InetAddress address, int port)
Creates a new TCP output stream.protected
TcpOutputStream(SocketFactory socketFactory, InetAddress address, int port, boolean blockOnReconnect)
Creates a new TCP output stream.TcpOutputStream(ClientSocketFactory socketFactory, boolean blockOnReconnect)
Creates a new TCP stream which uses theClientSocketFactory.createSocket()
to create the socket.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
flush()
Collection<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.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)
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
outputLock
protected final Object outputLock
-
-
Constructor Detail
-
TcpOutputStream
public TcpOutputStream(InetAddress address, int port) throws IOException
Creates a TCP output stream. Uses thedefault socket factory
to 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
public TcpOutputStream(InetAddress address, int port, boolean blockOnReconnect) throws IOException
Creates a TCP output stream.Uses the
default socket factory
to create the socket.- Parameters:
address
- the address to connect toport
- the port to connect toblockOnReconnect
-true
to block when attempting to reconnect the socket orfalse
to 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
@Deprecated protected TcpOutputStream(Socket socket)
Deprecated.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 asocket
from thesocketFactory
argument.- 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
socket
from thesocketFactory
argument.- Parameters:
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 orfalse
to 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
public TcpOutputStream(ClientSocketFactory socketFactory, boolean blockOnReconnect)
Creates a new TCP stream which uses theClientSocketFactory.createSocket()
to create the socket.- Parameters:
socketFactory
- the socket factory used to create TCP socketsblockOnReconnect
-true
to block when attempting to reconnect the socket orfalse
to reconnect asynchronously
-
-
Method Detail
-
write
public void write(int b) throws IOException
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
flush
public void flush() throws IOException
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in 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:
true
if blocking is enabled, otherwisefalse
-
setBlockOnReconnect
public void setBlockOnReconnect(boolean blockOnReconnect)
Enables or disables blocking when attempting to reconnect the socket. If set totrue
thewrite
methods will block when attempting to reconnect. This is only advisable to be set totrue
if using an asynchronous handler.- Parameters:
blockOnReconnect
-true
to block when reconnecting orfalse
to 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 anIOException
occurs during a write. Otherwise a stream is considered connected.- Returns:
true
if the stream is connected, otherwisefalse
-
getErrors
public Collection<Exception> getErrors()
Retrieves the errors occurred, if any, during a write or reconnect.- Returns:
- a collection of errors or an empty list
-
-