我想处理TCP连接工厂异常。
使用抽象连接工厂:
@Bean
public AbstractClientConnectionFactory clientFactory() {
TcpNetClientConnectionFactory factory = new TcpNetClientConnectionFactory(host, Integer.parseInt(port));
factory.setSoKeepAlive(Boolean.parseBoolean(keepAlive));
factory.setSoTimeout(timeout);
factory.setSoReceiveBufferSize(Integer.parseInt(bufferSize));
factory.setSoSendBufferSize(Integer.parseInt(bufferSize));
return factory;
}
此连接在TcpSendingMessageHandler和TcpReceivingChannelAdapter分离的bean中注入。
@Bean
public TcpReceivingChannelAdapter tcpIn() {
...
receiver.setConnectionFactory(clientFactory());
...
}
@Bean
@ServiceActivator(...)
public TcpSendingMessageHandler tcpOut() {
...
sender.setConnectionFactory(clientFactory());
...
}
我有一些ApplicatioListeners,例如:TcpConnectionExceptionEvent,TcpConnectionCloseEvent和TcpConnectionOpenEvent。
@EventListener
public void handleTcpConnectionCloseEvent(TcpConnectionExceptionEvent event){
...
}
但是,我检测到打开的连接何时关闭,启动了TcpConnectionExceptionEvent,但是当连接甚至没有打开时也没有。如何处理“connection refused”
问题或任何其他TCP错误?
是否可以使用控制总线启动/停止连接?我发送:
Message operation = MessageBuilder.withPayload("@clientFactory.isRunning()").build();
boolean sent = operationChannel.send(operation);
这似乎无法正常工作,因为没有收到任何响应,只会创建“连续”调用,寻找淹没应用程序的参考。存在clientFactory
个bean(使用context.getBeanDefinitionNames()
检查)
此外,我可以设置尝试重新连接的最大重试次数吗?
编辑(重试建议):我将tcpRetryAdvice添加到我的tcp出站通道但我仍然感到困惑,因为clientFactory()
(当“connection refused”
时)不遵循定义的retryAdvice中的策略。如何控制当前的尝试以及最后是否已传递消息?
@Bean
@ServiceActivator(inputChannel = "tcpSender", adviceChain = "tcpRetryAdvice")
public TcpSendingMessageHandler tcpOut(AbstractClientConnectionFactory connectionFactory) { ... }
@Bean
public RequestHandlerRetryAdvice tcpRetryAdvice() {
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(2);
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
backOffPolicy.setInitialInterval(3000);
backOffPolicy.setMaxInterval(10000);
backOffPolicy.setMultiplier(2);
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(retryPolicy);
retryTemplate.setBackOffPolicy(backOffPolicy);
RequestHandlerRetryAdvice tcpRetryAdvice = new RequestHandlerRetryAdvice();
tcpRetryAdvice.setRetryTemplate(retryTemplate);
// This allows fail-controlling
tcpRetryAdvice.setRecoveryCallback(new ErrorMessageSendingRecoverer(failMessageChannel()));
return tcpRetryAdvice;
}
编辑(ControlBus):
我只是想停止向TCP发送消息,因为我必须知道TCP(connectionFactory)何时连接才能不丢弃JMS消息。
编辑(Bean的记录错误)
首先,我有:
tcpRetryAdvice.setRecoveryCallback(new ErrorMessageSendingRecoverer(failMessageChannel()));
这可以跟踪异常,但我怎么能收到消息?
然后我将错误发送到errorChannel
但是当clientFactory()
bean引发"connecton refused"
异常时,我仍然看到整个堆栈跟踪。我想避免这个:
[ERROR][TcpSendingMessageHandler] - [TcpSendingMessageHandler.java:80] - 26/08/2016
20:40:57.424 - Error creating connection
java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
....
我只想:
In my error channel: Message not delivered <Failed to obtain a connection; nested exception is java.net.ConnectException: Connection refused: connect>
我正在将回调发送给failChannel()
,我不想看到堆栈跟踪。理想情况下,我想管理回调。获取未发送的消息以保存它以等待套接字重新连接并使用errorChannel记录错误。
答案 0 :(得分:1)
新TcpConnectionFailedEvent
为recently added。
4.3.2应在下周左右发布;您可以先使用4.3.2.BUILD-SNAPSHOT版本进行试用。
您可以向出站适配器添加retry advice。
我不确定您对控制总线的看法。