Class TcpSocketManager
- java.lang.Object
-
- org.apache.logging.log4j.core.appender.AbstractManager
-
- org.apache.logging.log4j.core.appender.OutputStreamManager
-
- org.apache.logging.log4j.core.net.AbstractSocketManager
-
- org.apache.logging.log4j.core.net.TcpSocketManager
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,ByteBufferDestination
- Direct Known Subclasses:
SslSocketManager
public class TcpSocketManager extends AbstractSocketManager
Manager of TCP Socket connections.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
TcpSocketManager.FactoryData
Data for the factory.static class
TcpSocketManager.HostResolver
private class
TcpSocketManager.Reconnector
Handles reconnecting to a Socket on a Thread.protected static class
TcpSocketManager.TcpSocketManagerFactory<M extends TcpSocketManager,T extends TcpSocketManager.FactoryData>
Factory to create a TcpSocketManager.
-
Field Summary
Fields Modifier and Type Field Description private int
connectTimeoutMillis
private static int
DEFAULT_PORT
The default port number of remote logging server (4560).static int
DEFAULT_RECONNECTION_DELAY_MILLIS
The default reconnection delay (30000 milliseconds or 30 seconds).private static TcpSocketManager.TcpSocketManagerFactory<TcpSocketManager,TcpSocketManager.FactoryData>
FACTORY
private boolean
immediateFail
private int
reconnectionDelayMillis
private TcpSocketManager.Reconnector
reconnector
private boolean
retry
private java.net.Socket
socket
private SocketOptions
socketOptions
-
Fields inherited from class org.apache.logging.log4j.core.net.AbstractSocketManager
host, inetAddress, port
-
Fields inherited from class org.apache.logging.log4j.core.appender.OutputStreamManager
byteBuffer, layout
-
Fields inherited from class org.apache.logging.log4j.core.appender.AbstractManager
count, LOGGER
-
-
Constructor Summary
Constructors Constructor Description TcpSocketManager(java.lang.String name, java.io.OutputStream os, java.net.Socket socket, java.net.InetAddress inetAddress, java.lang.String host, int port, int connectTimeoutMillis, int reconnectionDelayMillis, boolean immediateFail, Layout<? extends java.io.Serializable> layout, int bufferSize)
TcpSocketManager(java.lang.String name, java.io.OutputStream os, java.net.Socket socket, java.net.InetAddress inetAddress, java.lang.String host, int port, int connectTimeoutMillis, int reconnectionDelayMillis, boolean immediateFail, Layout<? extends java.io.Serializable> layout, int bufferSize, SocketOptions socketOptions)
Constructs.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected boolean
closeOutputStream()
private TcpSocketManager.Reconnector
createReconnector()
protected java.net.Socket
createSocket(java.net.InetSocketAddress socketAddress)
protected static java.net.Socket
createSocket(java.net.InetSocketAddress socketAddress, SocketOptions socketOptions, int connectTimeoutMillis)
int
getConnectTimeoutMillis()
java.util.Map<java.lang.String,java.lang.String>
getContentFormat()
Gets this TcpSocketManager's content format.int
getReconnectionDelayMillis()
java.net.Socket
getSocket()
USE AT YOUR OWN RISK, method is public for testing purpose only for now.static TcpSocketManager
getSocketManager(java.lang.String host, int port, int connectTimeoutMillis, int reconnectDelayMillis, boolean immediateFail, Layout<? extends java.io.Serializable> layout, int bufferSize)
static TcpSocketManager
getSocketManager(java.lang.String host, int port, int connectTimeoutMillis, int reconnectDelayMillis, boolean immediateFail, Layout<? extends java.io.Serializable> layout, int bufferSize, SocketOptions socketOptions)
Obtains a TcpSocketManager.SocketOptions
getSocketOptions()
USE AT YOUR OWN RISK, method is public for testing purpose only for now.static void
setHostResolver(TcpSocketManager.HostResolver resolver)
This method is only for unit testing.java.lang.String
toString()
protected void
write(byte[] bytes, int offset, int length, boolean immediateFlush)
Some output streams synchronize writes while others do not.private void
writeAndFlush(byte[] bytes, int offset, int length, boolean immediateFlush)
-
Methods inherited from class org.apache.logging.log4j.core.appender.OutputStreamManager
createOutputStream, drain, flush, flushBuffer, flushDestination, getByteBuffer, getManager, getOutputStream, hasOutputStream, isOpen, releaseSub, setOutputStream, skipFooter, write, write, write, writeBytes, writeBytes, writeFooter, writeHeader, writeToDestination
-
Methods inherited from class org.apache.logging.log4j.core.appender.AbstractManager
close, getCount, getLoggerContext, getManager, getName, hasManager, log, logDebug, logError, logger, logWarn, narrow, release, stop, updateData
-
-
-
-
Field Detail
-
DEFAULT_RECONNECTION_DELAY_MILLIS
public static final int DEFAULT_RECONNECTION_DELAY_MILLIS
The default reconnection delay (30000 milliseconds or 30 seconds).- See Also:
- Constant Field Values
-
DEFAULT_PORT
private static final int DEFAULT_PORT
The default port number of remote logging server (4560).- See Also:
- Constant Field Values
-
FACTORY
private static final TcpSocketManager.TcpSocketManagerFactory<TcpSocketManager,TcpSocketManager.FactoryData> FACTORY
-
reconnectionDelayMillis
private final int reconnectionDelayMillis
-
reconnector
private TcpSocketManager.Reconnector reconnector
-
socket
private java.net.Socket socket
-
socketOptions
private final SocketOptions socketOptions
-
retry
private final boolean retry
-
immediateFail
private final boolean immediateFail
-
connectTimeoutMillis
private final int connectTimeoutMillis
-
-
Constructor Detail
-
TcpSocketManager
@Deprecated public TcpSocketManager(java.lang.String name, java.io.OutputStream os, java.net.Socket socket, java.net.InetAddress inetAddress, java.lang.String host, int port, int connectTimeoutMillis, int reconnectionDelayMillis, boolean immediateFail, Layout<? extends java.io.Serializable> layout, int bufferSize)
Deprecated.Constructs.- Parameters:
name
- The unique name of this connection.os
- The OutputStream.socket
- The Socket.inetAddress
- The Internet address of the host.host
- The name of the host.port
- The port number on the host.connectTimeoutMillis
- the connect timeout in milliseconds.reconnectionDelayMillis
- Reconnection interval.immediateFail
- True if the write should fail if no socket is immediately available.layout
- The Layout.bufferSize
- The buffer size.
-
TcpSocketManager
public TcpSocketManager(java.lang.String name, java.io.OutputStream os, java.net.Socket socket, java.net.InetAddress inetAddress, java.lang.String host, int port, int connectTimeoutMillis, int reconnectionDelayMillis, boolean immediateFail, Layout<? extends java.io.Serializable> layout, int bufferSize, SocketOptions socketOptions)
Constructs.- Parameters:
name
- The unique name of this connection.os
- The OutputStream.socket
- The Socket.inetAddress
- The Internet address of the host.host
- The name of the host.port
- The port number on the host.connectTimeoutMillis
- the connect timeout in milliseconds.reconnectionDelayMillis
- Reconnection interval.immediateFail
- True if the write should fail if no socket is immediately available.layout
- The Layout.bufferSize
- The buffer size.
-
-
Method Detail
-
getSocketManager
@Deprecated public static TcpSocketManager getSocketManager(java.lang.String host, int port, int connectTimeoutMillis, int reconnectDelayMillis, boolean immediateFail, Layout<? extends java.io.Serializable> layout, int bufferSize)
Obtains a TcpSocketManager.- Parameters:
host
- The host to connect to.port
- The port on the host.connectTimeoutMillis
- the connect timeout in millisecondsreconnectDelayMillis
- The interval to pause between retries.bufferSize
- The buffer size.- Returns:
- A TcpSocketManager.
-
getSocketManager
public static TcpSocketManager getSocketManager(java.lang.String host, int port, int connectTimeoutMillis, int reconnectDelayMillis, boolean immediateFail, Layout<? extends java.io.Serializable> layout, int bufferSize, SocketOptions socketOptions)
Obtains a TcpSocketManager.- Parameters:
host
- The host to connect to.port
- The port on the host.connectTimeoutMillis
- the connect timeout in millisecondsreconnectDelayMillis
- The interval to pause between retries.bufferSize
- The buffer size.- Returns:
- A TcpSocketManager.
-
write
protected void write(byte[] bytes, int offset, int length, boolean immediateFlush)
Description copied from class:OutputStreamManager
Some output streams synchronize writes while others do not. Synchronizing here insures that log events won't be intertwined.- Overrides:
write
in classOutputStreamManager
- Parameters:
bytes
- The serialized Log event.offset
- The offset into the byte array.length
- The number of bytes to write.immediateFlush
- flushes immediately after writing.
-
writeAndFlush
private void writeAndFlush(byte[] bytes, int offset, int length, boolean immediateFlush) throws java.io.IOException
- Throws:
java.io.IOException
-
closeOutputStream
protected boolean closeOutputStream()
- Overrides:
closeOutputStream
in classOutputStreamManager
-
getConnectTimeoutMillis
public int getConnectTimeoutMillis()
-
getContentFormat
public java.util.Map<java.lang.String,java.lang.String> getContentFormat()
Gets this TcpSocketManager's content format. Specified by:- Key: "protocol" Value: "tcp"
- Key: "direction" Value: "out"
- Overrides:
getContentFormat
in classAbstractSocketManager
- Returns:
- Map of content format keys supporting TcpSocketManager
-
createReconnector
private TcpSocketManager.Reconnector createReconnector()
-
createSocket
protected java.net.Socket createSocket(java.net.InetSocketAddress socketAddress) throws java.io.IOException
- Throws:
java.io.IOException
-
createSocket
protected static java.net.Socket createSocket(java.net.InetSocketAddress socketAddress, SocketOptions socketOptions, int connectTimeoutMillis) throws java.io.IOException
- Throws:
java.io.IOException
-
setHostResolver
public static void setHostResolver(TcpSocketManager.HostResolver resolver)
This method is only for unit testing. It is not Thread-safe.- Parameters:
resolver
- the HostResolver.
-
getSocketOptions
public SocketOptions getSocketOptions()
USE AT YOUR OWN RISK, method is public for testing purpose only for now.
-
getSocket
public java.net.Socket getSocket()
USE AT YOUR OWN RISK, method is public for testing purpose only for now.
-
getReconnectionDelayMillis
public int getReconnectionDelayMillis()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-