В весенней интеграции, как ловить разные исключения?
В весенней интеграции у меня есть простой канал клиента tcp: шлюз, исходящий шлюз tcp, активатор службы плюс канал ошибок. В tcp-connection-factory есть простой перехватчик. Канал ошибок очень прост, я реализовал tcp-connection-event-inbound-channel-adapter с этим фильтром:
org.springframework.integration.ip.tcp.connection.TcpConnectionExceptionEvent.Так что мой обработчик ошибок очень прост, выглядит так:
public class TcpErrorHandler {
public void onException(){
System.out.println("Exception!!! ");
}
}
Это работает, потому что, когда у меня есть Исключение закрытия сокета (на стороне сервера я закрываю соединение), приложение пишет «Исключение !!!» на консоль, но с другой стороны, это не работает, когда у меня есть исключение тайм-аут соединения. Мои вопросы: Как мне получить все наиболее важные исключения для меня:
Runtime Socket закрыть ИсключениеИсключение времени ожидания соединенияДругие исключенияЕсть ли механизм ловли?
Вот фрагмент из моего конфига 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");