gpt4 book ai didi

java - 我如何找出 spring 集成中发生了什么异常?

转载 作者:可可西里 更新时间:2023-11-01 02:42:43 28 4
gpt4 key购买 nike

当我通过 TCP 客户端向服务器发送消息时,如何找出 spring 集成中发生的异常。所以,有一个 <int-ip:tcp-connection-factory> (客户端!),它有一个错误 channel 。当在客户端和服务器之间创建连接并在几秒钟后服务器关闭连接时出现异常。没关系,我能够捕获它,异常是:“读取超时”。但是当连接没有创建时,因为服务器没有监听指定的端口,我没有通过错误 channel 缓存任何异常。因此,我试图以这种方式缓存异常:

try{
success = gateway.send(adaptor);
} catch ( MessagingException exc){
System.out.println("Exception occurred : Timed out waiting for response");
}

但是 - 在这一点上 - 我缓存

Timed out waiting for response

任何原因的异常(exception)。 (例如,当服务器关闭连接时,或者连接没有发生时) A 不能区分异常。因此,我同时监听了这个捕获机制和错误 channel 。

我不优雅的解决方案是:

Main service class :
success = 0;
try{
success = gateway.send(adaptor); // this will be return 1, if there aren't any exception
return "Success";
} catch ( MessagingException exc){
try {
Thread.sleep(1000); // I have to wait for the error channel's message, this portion not elegant !
} catch (InterruptedException e) {
return "ERROR";
}
if ( success == 2){
return "The server closed the connection";
} else {
return "There is no connection with server";
}

错误处理程序片段是:

public void onException(TcpConnectionExceptionEvent event){
mainService.setSuccess(2);
}

有没有更简单、更优雅的解决方案?

更正:我使用 Spring 集成 4.04,这是我的客户端管道的 bean:

<!-- Client side -->

<int:gateway id="gw"
service-interface="hu.gmxdev.climaxreplica.service.SimpleGateway"
default-request-channel="outputchannel" />

<int-ip:tcp-connection-factory id="client"
type="client" host="localhost" port="10001" single-use="true"
so-timeout="5000"
/>

<int:channel id="outputchannel" />

<int-ip:tcp-outbound-gateway id="outGateway"
request-channel="outputchannel" reply-channel="replychannel"
connection-factory="client" request-timeout="5000" reply-timeout="5000" />

<int:service-activator input-channel="replychannel"
method="reply" ref="echoService" id="serviceactivator">
</int:service-activator>
<int:channel id="replychannel"></int:channel>

<int-ip:tcp-connection-event-inbound-channel-adapter id="event"
event-types="org.springframework.integration.ip.tcp.connection.TcpConnectionExceptionEvent"
channel="errorChannel" />
<int:channel id="errorChannel"></int:channel>

<int:service-activator ref="tcpErrorHandler" method="onException"
input-channel="errorChannel">
</int:service-activator>

我添加了轨迹轨迹。这是当服务器关闭已建立的连接,或服务器未在预期时间内响应客户端时。两种情况下的轨迹都相同。 :

org.springframework.integration.MessageTimeoutException: Timed out waiting for response
at org.springframework.integration.ip.tcp.TcpOutboundGateway.handleRequestMessage(TcpOutboundGateway.java:132)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:170)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:255)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:223)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:109)
at org.springframework.messaging.core.GenericMessagingTemplate.doSendAndReceive(GenericMessagingTemplate.java:144)
at org.springframework.messaging.core.GenericMessagingTemplate.doSendAndReceive(GenericMessagingTemplate.java:44)
at org.springframework.messaging.core.AbstractMessagingTemplate.sendAndReceive(AbstractMessagingTemplate.java:75)
at org.springframework.messaging.core.AbstractMessagingTemplate.convertSendAndReceive(AbstractMessagingTemplate.java:125)
at org.springframework.messaging.core.AbstractMessagingTemplate.convertSendAndReceive(AbstractMessagingTemplate.java:103)
at org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:241)
at org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceive(MessagingGatewaySupport.java:220)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:341)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:304)
at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:295)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy115.send(Unknown Source)
at hu.gmxdev.climaxreplica.service.MainService.senddata(MainService.java:88)
at hu.gmxdev.climaxreplica.controller.MainController.senddata(MainController.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:724)

最佳答案

在这两种情况下都会抛出 MessagingException...

11:58:08.068 ERROR [main][org.springframework.integration.ip.tcp.TcpOutboundGateway] Tcp Gateway exception
org.springframework.integration.MessageTimeoutException: Timed out waiting for response

12:00:34.859 WARN  [main][org.springframework.integration.gateway.GatewayProxyFactoryBean$MethodInvocationGateway] failure occurred in gateway sendAndReceive
org.springframework.messaging.MessagingException: org.springframework.messaging.MessagingException: Failed to send or receive

在第二种情况下您不会得到 TcpConnectionExceptionEvent - 该事件是针对特定已建立 连接的异常。由于我们实际上并没有建立连接,因此没有连接可以报告异常。

为了确定实际的异常,你应该遍历原因树;像这样的东西:

if (e.getCause() != null) {
if (e.getCause().getCause() instanceof ConnectException) {
...
}
}

您还可以更改网关以抛出 ConnectException...

int send(Foo adapter) throws ConnectException;

...框架会自动为您解包...

try {
success = gateway.send(adaptor);
}
catch (MessagingException exc) {
System.out.println("Exception occurred : " + exc.getMessage());
}
catch (ConnectException e) {
...
}

超时(无响应)的消息传递异常是 MessageTimeoutException(MessagingException 的子类)。对于在等待回复时发生的错误,您应该得到...

throw new MessagingException("Exception while awaiting reply", (Throwable) reply.getPayload());

关于java - 我如何找出 spring 集成中发生了什么异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26689505/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com