Spring Integration tcp客户端多个连接

时间:2016-11-09 13:05:10

标签: tcp spring-integration

我使用Spring Integration tcp-outbound-adapter和tcp-inbound-adapter,以便通过TCP与第三方外部系统进行通信。

我使用的连接工厂是“client”类型,并且具有单次使用=“false”,因为与外部系统通信的性质是几十个请求和回复的会话。 外部系统期望我将为每个会话打开一个新的TCP连接。

使用Spring Integration有没有办法做到这一点?

我的代码成功地将SI用于一个这样的会话。但我希望我的系统打开几个这样的连接,这样我就可以处理几个并发会话。 目前,如果我向入站适配器发送新会话的消息,它将使用相同的TCP连接。

请帮忙。

更新:

在使用Gary提供的ThreadAffinity解决方案时,当我们执行4个以上的并发请求时,我们会遇到此异常。知道为什么会这样吗?

11:08:02.083  [pool-1-thread-2] 193.xxx.yyy.zz:443:55729:46c71372-5933-4707-a27b-93cc4bf78c59 Message sent GenericMessage [payload=byte[326], headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@2fb866, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@2fb866, ip_tcp_remotePort=55718, ip_connectionId=127.0.0.1:55718:4444:7f71ce96-eaac-4b21-8b2c-bf736102f818, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, id=2dc3e330-d703-8a61-c46c-012233cadf6f, ip_hostname=127.0.0.1, timestamp=1481706480700}]
11:08:12.093  [pool-1-thread-2] Remote Timeout on 193.xxx.yyy.zz:443:55729:46c71372-5933-4707-a27b-93cc4bf78c59
11:08:12.093  [pool-1-thread-2] Tcp Gateway exception
org.springframework.integration.MessageTimeoutException: Timed out waiting for response
            at org.springframework.integration.ip.tcp.TcpOutboundGateway.handleRequestMessage(TcpOutboundGateway.java:146)
            at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
            at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
            at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
            at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148)
            at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121)
            at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
            at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:423)
            at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:373)
            at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
            at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
            at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
            at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:292)
            at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:212)
            at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:129)
            at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
            at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
            at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
            at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148)
            at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121)
            at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
            at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:423)
            at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
            at org.springframework.messaging.core.GenericMessagingTemplate.doSendAndReceive(GenericMessagingTemplate.java:150)
            at org.springframework.messaging.core.GenericMessagingTemplate.doSendAndReceive(GenericMessagingTemplate.java:45)
            at org.springframework.messaging.core.AbstractMessagingTemplate.sendAndReceive(AbstractMessagingTemplate.java:42)
            at org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:97)
            at org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:441)
            at org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:409)
            at org.springframework.integration.ip.tcp.TcpInboundGateway.doOnMessage(TcpInboundGateway.java:120)
            at org.springframework.integration.ip.tcp.TcpInboundGateway.onMessage(TcpInboundGateway.java:98)
            at org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorSupport.onMessage(TcpConnectionInterceptorSupport.java:159)
            at org.springframework.integration.ip.tcp.connection.TcpNetConnection.run(TcpNetConnection.java:182)
            at org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorSupport.run(TcpConnectionInterceptorSupport.java:111)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:1)

这取决于构成“会话”的内容 - 如果来自客户端会话的所有请求都在单个线程上运行,则可以为连接工厂编写一个简单的包装器,将连接存储在{{1 }}。在最后一个关闭连接的请求之后,您需要一些机制来调用工厂包装器,并将其从ThreadLocal中删除。

如果会话请求可以在多个线程上发生,那么它会更复杂,但您仍然可以使用映射到连接实例的ThreadLocal来执行此操作。

修改

这是一个例子......

ThreadLocal

结果:

@SpringBootApplication
public class So40507731Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So40507731Application.class, args);
        MessageChannel channel = context.getBean("clientFlow.input", MessageChannel.class);
        MessagingTemplate template = new MessagingTemplate(channel);
        ThreadAffinityClientConnectionFactory affinityCF = context.getBean(ThreadAffinityClientConnectionFactory.class);
        ExecutorService exec = Executors.newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(2);
        exec.execute(() -> {
            String result = new String(template.convertSendAndReceive("foo", byte[].class));
            System.out.println(Thread.currentThread().getName() + " " + result);
            result = new String(template.convertSendAndReceive("foo", byte[].class));
            System.out.println(Thread.currentThread().getName() + " " + result);
            affinityCF.release();
            latch.countDown();
        });
        exec.execute(() -> {
            String result = new String(template.convertSendAndReceive("foo", byte[].class));
            System.out.println(Thread.currentThread().getName() + " " + result);
            result = new String(template.convertSendAndReceive("foo", byte[].class));
            System.out.println(Thread.currentThread().getName() + " " + result);
            affinityCF.release();
            latch.countDown();
        });
        latch.await(10, TimeUnit.SECONDS);
        context.close();
        exec.shutdownNow();
    }

    @Bean
    public TcpNetClientConnectionFactory delegateCF() {
        TcpNetClientConnectionFactory clientCF = new TcpNetClientConnectionFactory("localhost", 1234);
        clientCF.setSingleUse(true); // so each thread gets his own connection
        return clientCF;
    }

    @Bean
    public ThreadAffinityClientConnectionFactory affinityCF() {
        return new ThreadAffinityClientConnectionFactory(delegateCF());
    }

    @Bean
    public TcpOutboundGateway outGate() {
        TcpOutboundGateway outGate = new TcpOutboundGateway();
        outGate.setConnectionFactory(affinityCF());
        return outGate;
    }

    @Bean
    public IntegrationFlow clientFlow() {
        return f -> f.handle(outGate());
    }

    @Bean
    public TcpNetServerConnectionFactory serverCF() {
        return new TcpNetServerConnectionFactory(1234);
    }

    @Bean
    public TcpInboundGateway inGate() {
        TcpInboundGateway inGate = new TcpInboundGateway();
        inGate.setConnectionFactory(serverCF());
        return inGate;
    }

    @Bean
    public IntegrationFlow serverFlow() {
        return IntegrationFlows.from(inGate())
                .transform(Transformers.objectToString())
                .transform("headers['ip_connectionId'] + ' ' + payload")
                .get();
    }

    public static class ThreadAffinityClientConnectionFactory extends AbstractClientConnectionFactory
            implements TcpListener {

        private final AbstractClientConnectionFactory delegate;

        private final ThreadLocal<TcpConnectionSupport> connection = new ThreadLocal<>();

        public ThreadAffinityClientConnectionFactory(AbstractClientConnectionFactory delegate) {
            super("", 0);
            delegate.registerListener(this);
            this.delegate = delegate;
        }

        @Override
        protected TcpConnectionSupport obtainConnection() throws Exception {
            TcpConnectionSupport tcpConnection = this.connection.get();
            if (tcpConnection == null || !tcpConnection.isOpen()) {
                tcpConnection = this.delegate.getConnection();
                this.connection.set(tcpConnection);
            }
            return tcpConnection;
        }

        public void release() {
            TcpConnectionSupport connection = this.connection.get();
            if (connection != null) {
                connection.close();
                this.connection.remove();
            }
        }

        @Override
        public void start() {
            this.delegate.start();
            setActive(true);
            super.start();
        }

        @Override
        public void stop() {
            this.delegate.stop();
            setActive(false);
            super.stop();
        }

        @Override
        public boolean onMessage(Message<?> message) {
            return getListener().onMessage(message);
        }

    }

}