Java Spring Integration DSL Tcp:如何防止过度记录客户端拒绝的连接
我使客户机和服务器都能够等待彼此连接,所以它们可以以任何顺序独立启动。 解决方案就是这样做的,开箱即用,但客户端一直在将大量堆栈跟踪打印到我们的日志中,带有Java Spring Integration DSL Tcp:如何防止过度记录客户端拒绝的连接,java,spring-integration-dsl,spring-integration-ip,Java,Spring Integration Dsl,Spring Integration Ip,我使客户机和服务器都能够等待彼此连接,所以它们可以以任何顺序独立启动。 解决方案就是这样做的,开箱即用,但客户端一直在将大量堆栈跟踪打印到我们的日志中,带有java.net.ConnectException:Connection-densed:connect,然后是46行长的堆栈跟踪,直到服务器启动并连接为止。 这并不理想 我的问题是:如何应用我的自定义逻辑来微调要记录的内容和时间 到目前为止,我发现日志是由org.springframework.integration.handler.Logg
java.net.ConnectException:Connection-densed:connect
,然后是46行长的堆栈跟踪,直到服务器启动并连接为止。
这并不理想
我的问题是:如何应用我的自定义逻辑来微调要记录的内容和时间
到目前为止,我发现日志是由org.springframework.integration.handler.LoggingHandler
打印的。这就像一种错误通道,错误总是被分派到那里。我找不到这个集合的位置,所以我可以,例如,用我自己的实现替换它。
我设法配置了自己的默认错误通道,但该通道是与预配置的LoggingHandler通道一起添加的,而不是替换它
另一种方法是在发送第一条消息时设置更长的超时时间。我也在努力解决这个问题。我试图将其设置为outboundGateway
,作为句柄(Tcp.outboundGateway(clientConnectionFactory).remoteTimeout(1\u 000\u 000L))
,但没有任何效果。好,已解决
问题实际上并不在LoggingHandler
或任何错误通道中,而是org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory.createSocket()
在服务器未立即就绪时抛出异常,然后TcpOutboundGateway
以老式方式记录此异常;只有这样,错误才会被发送到errorChannel
,在那里可以对其作出反应;默认的SI反应是再次打印:)这是我最初没有注意到的,异常记录了两次。可以使用自定义错误消息处理程序阻止第二个日志,但不能使用第一个日志
TcpNetClientConnectionFactory.createSocket()调用默认Java的createSocket(),没有设置超时的选项。如果收件人没有准备好,方法调用几乎立即失败。请参阅JDK的增强请求
可能的解决方案是重写TcpNetClientConnectionFactory.createSocket()
,重复对服务器的连接尝试,直到成功
WaitingCPNetClientConnectionFactory
public class WaitingTcpNetClientConnectionFactory extends TcpNetClientConnectionFactory {
private final SocketConnectionListener socketConnectionListener;
private final int waitBetweenAttemptsInMs;
private final Logger log = LogManager.getLogger();
public WaitingTcpNetClientConnectionFactory(
String host, int port,
int waitBetweenAttemptsInMs,
SocketConnectionListener socketConnectionListener) {
super(host, port);
this.waitBetweenAttemptsInMs = waitBetweenAttemptsInMs;
this.socketConnectionListener = socketConnectionListener;
}
@Override
protected Socket createSocket(String host, int port) throws IOException {
Socket socket = null;
while (socket == null) {
try {
socket = super.createSocket(host, port);
socketConnectionListener.onConnectionOpen();
} catch (ConnectException ce) {
socketConnectionListener.onConnectionFailure();
log.warn("server " + host + ":" + port + " is not ready yet ..waiting");
try {
Thread.sleep(waitBetweenAttemptsInMs);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new IOException("interrupted while wating between connection attempts", ie);
}
}
}
return socket;
}
}
作为额外的奖励,我还将成功或失败设置为提供的SocketConnectionListener
,这是我自己的自定义界面,因此应用程序的其他部分可以与之同步;例如,等待流式处理,直到服务器/对等节点就绪
使用WaitingCpNetClientConnectionFactory
的方法与使用TcpNetClientConnectionFactory
的方法相同
HeartbeatClientConfig(仅相关位):
现在它只打印:
INFO [ main] o.b.e.d.s.h.client.HeartbeatClientRun : Started HeartbeatClientRun in 1.042 seconds (JVM running for 1.44)
WARN [ask-scheduler-1] h.c.WaitingTcpNetClientConnectionFactory : server localhost:7777 is not ready yet ..waiting
WARN [ask-scheduler-1] h.c.WaitingTcpNetClientConnectionFactory : server localhost:7777 is not ready yet ..waiting
WARN [ask-scheduler-1] h.c.WaitingTcpNetClientConnectionFactory : server localhost:7777 is not ready yet ..waiting
WARN [ask-scheduler-1] h.c.WaitingTcpNetClientConnectionFactory : server localhost:7777 is not ready yet ..waiting
像往常一样,我的git上有完整的项目源代码。好的,解决了
问题实际上并不在LoggingHandler
或任何错误通道中,而是org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory.createSocket()
在服务器未立即就绪时抛出异常,然后TcpOutboundGateway
以老式方式记录此异常;只有这样,错误才会被发送到errorChannel
,在那里可以对其作出反应;默认的SI反应是再次打印:)这是我最初没有注意到的,异常记录了两次。可以使用自定义错误消息处理程序阻止第二个日志,但不能使用第一个日志
TcpNetClientConnectionFactory.createSocket()调用默认Java的createSocket(),没有设置超时的选项。如果收件人没有准备好,方法调用几乎立即失败。请参阅JDK的增强请求
可能的解决方案是重写TcpNetClientConnectionFactory.createSocket()
,重复对服务器的连接尝试,直到成功
WaitingCPNetClientConnectionFactory
public class WaitingTcpNetClientConnectionFactory extends TcpNetClientConnectionFactory {
private final SocketConnectionListener socketConnectionListener;
private final int waitBetweenAttemptsInMs;
private final Logger log = LogManager.getLogger();
public WaitingTcpNetClientConnectionFactory(
String host, int port,
int waitBetweenAttemptsInMs,
SocketConnectionListener socketConnectionListener) {
super(host, port);
this.waitBetweenAttemptsInMs = waitBetweenAttemptsInMs;
this.socketConnectionListener = socketConnectionListener;
}
@Override
protected Socket createSocket(String host, int port) throws IOException {
Socket socket = null;
while (socket == null) {
try {
socket = super.createSocket(host, port);
socketConnectionListener.onConnectionOpen();
} catch (ConnectException ce) {
socketConnectionListener.onConnectionFailure();
log.warn("server " + host + ":" + port + " is not ready yet ..waiting");
try {
Thread.sleep(waitBetweenAttemptsInMs);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new IOException("interrupted while wating between connection attempts", ie);
}
}
}
return socket;
}
}
作为额外的奖励,我还将成功或失败设置为提供的SocketConnectionListener
,这是我自己的自定义界面,因此应用程序的其他部分可以与之同步;例如,等待流式处理,直到服务器/对等节点就绪
使用WaitingCpNetClientConnectionFactory
的方法与使用TcpNetClientConnectionFactory
的方法相同
HeartbeatClientConfig(仅相关位):
现在它只打印:
INFO [ main] o.b.e.d.s.h.client.HeartbeatClientRun : Started HeartbeatClientRun in 1.042 seconds (JVM running for 1.44)
WARN [ask-scheduler-1] h.c.WaitingTcpNetClientConnectionFactory : server localhost:7777 is not ready yet ..waiting
WARN [ask-scheduler-1] h.c.WaitingTcpNetClientConnectionFactory : server localhost:7777 is not ready yet ..waiting
WARN [ask-scheduler-1] h.c.WaitingTcpNetClientConnectionFactory : server localhost:7777 is not ready yet ..waiting
WARN [ask-scheduler-1] h.c.WaitingTcpNetClientConnectionFactory : server localhost:7777 is not ready yet ..waiting
像往常一样,我的git上有完整的项目源代码