gpt4 book ai didi

java - 在Spring集成中,如何捕获不同的异常?

转载 作者:行者123 更新时间:2023-12-01 12:21:22 25 4
gpt4 key购买 nike

在 Spring 集成中,我有一个简单的 tcp 客户端管道:一个网关、一个 tcp 出站网关、一个服务激活器和一个错误 channel 。在 tcp-connection-factory 中有一个简单的拦截器。错误 channel 非常简单,我用这个过滤器实现了 tcp-connection-event-inbound-channel-adapter:

  • org.springframework.integration.ip.tcp.connection.TcpConnectionExceptionEvent。

所以我的错误处理程序非常简单,如下所示:

public class TcpErrorHandler {
public void onException(){
System.out.println("Exception!!! ");
}
}

它有效,因为当我有一个套接字关闭异常(服务器端我关闭连接)时,应用程序会写入“异常!!!”到控制台,但另一方面,当我出现连接超时异常时,它不起作用。我的问题是:如何获得所有与我最相关的异常(exception)情况:

  • 运行时套接字关闭异常
  • 连接超时异常
  • 其他异常(exception)情况

有捕捉机制吗?

这是我的 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="2000" deserializer="climaxDeserializer"
interceptor-factory-chain="customInterceptorFactoryChain"/>

<int:channel id="outputchannel" />

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

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

<int:channel id="replychannel"></int:channel>

<bean id="customInterceptorFactoryChain"
class="org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain">
<property name="interceptors">
<array>
<bean class="hu.gmxdev.climaxreplica.service.CustomInterceptorFactory"/>
</array>
</property>
</bean>

<!-- Error channel -->

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

<int:channel id="errorChannel"></int:channel>

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

这是我的错误处理程序:

public class TcpErrorHandler {
@Autowired
private ApplicationContext appContext;

public void onException(TcpConnectionExceptionEvent event){
MainService mainService = appContext.getBean(MainService.class);
mainService.setSuccess(3);
System.out.println("Exception!!! ");
System.out.println(event.getCause().getMessage());
}
}

拦截器在这里:

public class CustomInterceptor extends TcpConnectionInterceptorSupport{

public CustomInterceptor () {
System.out.println("catched_constructor1");
}

public CustomInterceptor (ApplicationEventPublisher applicationEventPublisher) {
super(applicationEventPublisher);
System.out.println("catched_constructor");
}

@Override
public boolean onMessage(Message<?> message) {
System.out.println("catched_message");
return super.onMessage(message);
}


@Override
public void send(Message<?> message){
System.out.println("catched_send");
MessageHeaders mh = message.getHeaders();
try {
super.send(message);
}
catch (Exception e) {
System.out.println("catched_send_exception");
}
}

@Override
public void close() {
String id = getConnectionId();
System.out.println("catched_closed" + id);
super.close();
}

}

还有我的“来电者”:

success = gateway.send("fooooooo");

最佳答案

您可以定义提供给入站适配器的错误 channel 。这是一个例子:

    <int:channel id="error-channel"></int:channel>
<int-ws:inbound-gateway id="gateway" error-channel="error-channel"
request-channel="in" marshaller="marshaller" unmarshaller="marshaller"
reply-channel="out" />

现在,下游抛出的所有异常都将被此错误 channel 捕获。然后,您可以使用此错误 channel 作为输入来定义服务激活器:

 <int:service-activator  input-channel="error-channel"
ref="exceptionHandler" method="handleError" output-channel="outError"></int:service-activator>

这个激活器引用了一个定义错误处理逻辑的 bean。

关于java - 在Spring集成中,如何捕获不同的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26646963/

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