【问题标题】:How to handle timeout exception using in spring integration using annotation?如何使用注释在spring集成中处理超时异常?
【发布时间】:2016-09-29 20:44:25
【问题描述】:

我使用 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();
    }

}

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

【问题讨论】:

标签: exception spring-integration


【解决方案1】:

向您的消息传递网关添加错误通道;它将收到ErrorMessage;有效载荷是一个MessagingException,具有两个属性causefailedMessage

【讨论】:

  • @Bean public MessageChannel printError() { DirectChannel channel = new DirectChannel();返回通道; } 正确吗?
  • 得到了答案...!
猜你喜欢
  • 2013-10-23
  • 2013-05-27
  • 2021-04-25
  • 2015-08-08
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多