0

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

@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();
    }

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

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

4

1 回答 1

0

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

请参阅文档

编辑

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

@EventListener
public void connectionEvent(TcpConnectionEvent event) {
    ...
}
于 2020-09-14T13:56:47.507 回答