Spring 第二次请求会话中的集成 TCP 客户端连接问题

Spring Integration TCP client connection issue on 2nd session of request

我有一个 Spring 集成 + spring 批处理要求,我必须与远程服务器建立 TCP 客户端连接并发送由 spring 处理的数千条记录组成的请求批。我正在使用 MessageBuilder 作为有效负载和消息通道来发送数据。我们发送所有记录,然后发送响应消息负载请求以开始从服务器接收响应。服务器使用使用转换器处理并添加到队列通道以供稍后读取的响应数据进行响应。

@Configuration
@EnableIntegration
public class GatewayConfig {

    private static final Logger logger = LoggerFactory.getLogger(GatewayConfig.class);
    @Autowired
    private GatewayProperties properties;
    
    /**Abstract class for client connection factories.
     * 
     * @return AbstractClientConnectionFactory
     * @throws Exception 
     */
    
    @Bean
    public AbstractClientConnectionFactory clientCF() {
        AbstractClientConnectionFactory factory = new TcpNetClientConnectionFactory( "138.25.189.110",5030);
        factory.setSoKeepAlive(true);
        factory.setDeserializer(new ByteArraySingleTerminatorSerializer((byte) 13));
        return factory;
    }
    
    
    
    /**
     * Receives messages over TCP.
     * Specify the MessageChannel to which produced Messages should be sent.
     * @param connectionFactory
     * @return TcpReceivingChannelAdapter
     */
    @Bean
    public TcpReceivingChannelAdapter inboundAdapterClient(@Qualifier("clientCF")AbstractClientConnectionFactory connectionFactory) {
        TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    //  adapter.setOutputChannelName(GatewayConstants.AGGREGATOR_PRE_INPUT_CHANNEL);
        adapter.setOutputChannelName(GatewayConstants.PRETRANSFORM_CHANNEL);
        adapter.setErrorChannelName(GatewayConstants.TCP_ERROR);
        adapter.setClientMode(true);
        adapter.setConnectionFactory(connectionFactory);
        return adapter;
    }

    
    @Bean(GatewayConstants.RECEIVE_CHANNEL_NAME)
    public QueueChannel receivingChannel() {
        
        return new QueueChannel();
    }
    
    @Bean
    @Transformer(inputChannel = GatewayConstants.PRETRANSFORM_CHANNEL, outputChannel = GatewayConstants.RECEIVE_CHANNEL_NAME)
    public Pretransform pretransformMet() {
        return new Pretransform();
    }
    
    
    /**
     * Sends messages over TCP
     * @param connectionFactory
     * @return
     */
    @Bean
    @ServiceActivator(inputChannel = GatewayConstants.SEND_TO_VENDOR_CHANNEL_NAME)
    public TcpSendingMessageHandler messageHandlerClient(@Qualifier("clientCF")AbstractClientConnectionFactory connectionFactory) {
        TcpSendingMessageHandler sendingHandler = new TcpSendingMessageHandler();
        sendingHandler.setConnectionFactory(connectionFactory);
        sendingHandler.setClientMode(true);
        
        return sendingHandler;
    }
    
    /**
     * 
     * @return
     */
    @Bean(GatewayConstants.TCP_ERROR)
    public MessageChannel tcpErrrorChannel() {
        return new DirectChannel();
    }
    
    @Bean(GatewayConstants.PRETRANSFORM_CHANNEL)
    public MessageChannel preReceiveChannel() {
        return new DirectChannel();
    }
    
    @Bean(GatewayConstants.BATCH_SEND_CHANNEL_NAME)
    public MessageChannel sendchannel() {
        return new DirectChannel();
    }
    
    /**
     * 
     * @return
     */
    @Bean(GatewayConstants.SEND_TO_VENDOR_CHANNEL_NAME)
    public MessageChannel sendToVendorChannel() {
        return new DirectChannel();
    }

批处理作业每天 运行 午夜。在应用程序 started/deployed 之后,请求被发送并成功收到响应,但第二天服务器没有收到任何数据。发送请求时没有 tcp 连接异常,但服务器端未收到任何数据。

我什至将 singleuse 设置为 true 但得到 运行 客户端无法将 sing use 设置为 true 的时间异常。有没有一种方法可以在发送请求之前和收到响应之后建立连接和 tcp 连接状态时进行记录? 感谢您的帮助并提前致谢。

您可以添加一个 ApplicationListener bean(或 @EventListener 方法)来接收各种 TcpConnectionEvents(打开、关闭等)。

the documentation

编辑

只需将其添加到您的一个 bean 中...

@EventListener
public void connectionEvent(TcpConnectionEvent event) {
    ...
}