如何使用注释在spring集成中使用超时异常?

时间:2016-09-28 14:41:59

标签: exception spring-integration

我使用AbstractClientConnectionFactory进行客户端服务器连接,使用TcpReceivingChannelAdapter,TcpSendingMessageHandler分别发送和接收上下文的CorrelationStrategy。在这种情况下,我如何处理timeoutException?

         public class ClientCall {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext ctx = new AnnotationConfigApplicationContext(GatewayConfig.class);
        GatewayService gatewayService = ctx.getBean(GatewayService.class);
        //int i=0;
        Message message = new Message();        
        /*while(i<4)
        {*/
            message.setPayload("It's working");
            gatewayService.sendMessage(message);
        /*  i++;            
        }*/

    }
}

public class Message {

    private String payload;
  // getter setter
}

@EnableIntegration
@IntegrationComponentScan
@Configuration
@ComponentScan(basePackages = "com.gateway.service")
public class GatewayConfig {

    // @Value("${listen.port:6788}")
    private int port = 6785;

    @Autowired
    private GatewayService<Message> gatewayService;

    @MessagingGateway(defaultRequestChannel = "sendMessageChannel")
    public interface Gateway {
        void viaTcp(String payload);
    }

    @Bean
    public AbstractClientConnectionFactory clientCF() {
        TcpNetClientConnectionFactory clientConnectionFactory = new TcpNetClientConnectionFactory("localhost",
                this.port);
        clientConnectionFactory.setSingleUse(false);
        return clientConnectionFactory;
    }

    @Bean
    @ServiceActivator(inputChannel = "sendMessageChannel")
    public MessageHandler tcpOutGateway(AbstractClientConnectionFactory connectionFactory) {
        TcpOutboundGateway outGateway = new TcpOutboundGateway();
        outGateway.setConnectionFactory(connectionFactory);
        // outGateway.setAsync(true);
        outGateway.setOutputChannel(receiveMessageChannel());
        outGateway.setRequiresReply(true);
        outGateway.setReplyChannel(receiveMessageChannel());
        return outGateway;
    }

    @Bean
    public MessageChannel sendMessageChannel() {
        DirectChannel channel = new DirectChannel();
        return channel;
    }


    @Bean
    public MessageChannel receiveMessageChannel() {
        DirectChannel channel = new DirectChannel();
        return channel;
    }

    @Transformer(inputChannel = "receiveMessageChannel", outputChannel = "processMessageChannel")
    public String convert(byte[] bytes) {
        return new String(bytes);
    }

    @ServiceActivator(inputChannel = "processMessageChannel")
    public void upCase(String response) {
        gatewayService.receiveMessage(response);
    }

    @Transformer(inputChannel = "errorChannel", outputChannel = "processMessageChannel")
    public void convertError(byte[] bytes) {
        String str = new String(bytes);
        System.out.println("Error: " + str);
    }

}


public interface GatewayService<T> {

    public void sendMessage(final T payload);

    public void receiveMessage(String response);

}


@Service
public class GatewayServiceImpl implements GatewayService<Message> {

    @Autowired
    private Gateway gateway;

    @Autowired
    private GatewayContextManger<String, Object> gatewayContextManger;

    @Override
    public void sendMessage(final Message message) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                gateway.viaTcp(message.getPayload());
            }
        }).start();
    }

    @Override
    public void receiveMessage(final String response) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                Message message = new Message();
                message.setPayload(response);
                Object obj = gatewayContextManger.get(message.getPayload());
                synchronized (obj) {
                    obj.notify();
                }
            }
        }).start();
    }

}

这是我的客户端代码,如果我向服务器发送了一个请求并且响应没有及时响应,那么如果服务器不可用,我应该如何捕获超时异常或套接字异常?

1 个答案:

答案 0 :(得分:0)

向您的消息传递网关添加错误通道;它会收到ErrorMessage;有效负载是MessagingException,其中包含两个属性causefailedMessage