我怎么知道,spring集成中发生了什么异常?

时间:2014-11-01 12:55:23

标签: java spring tcp exception-handling spring-integration

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

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

但是 - 此时 - 我缓存

  

等待回复超时

任何原因的例外情况。 (例如,当服务器关闭连接,或者连接没有发生时)A不能区分异常。因此,我立刻听这个捕获机制和错误通道。

我不太优雅的解决方案是:

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)

1 个答案:

答案 0 :(得分:1)

在这两种情况下都会抛出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 - 该事件适用于特定已建立连接的例外情况。由于我们实际上没有建立连接,因此没有连接报告异常。

为了确定实际的异常,你应该遍历cause树;类似的东西:

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) {
    ...
}

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

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