我使用套接字连接作为客户端,spring-integration-ip:

@Bean 
public AbstractClientConnectionFactory clientFactory() throws Exception { 
    TcpConnectionFactoryFactoryBean f = new TcpConnectionFactoryFactoryBean(); 
    f.setType("client"); 
    f.setHost(host); 
    f.setPort(port); 
    ... 
} 
 
@Bean 
@ServiceActivator(inputChannel = "clientChannel") 
public TcpOutboundGateway outboundGatewasy(AbstractClientConnectionFactory factory) throws Exception { 
    TcpOutboundGateway g = new TcpOutboundGateway(); 
    g.setConnectionFactory(factory); 
    g.setRequiresReply(true); 
    return gate; 
} 

现在我可以注入(inject)@Autowired TcpOutboundGateway gateway。但没有像 .beforeShutdown()getActiveConnections() 这样的方法。

那么,当应用程序关闭时,如何检索 Activity 的套接字连接?

请您参考如下方法:

连接由连接工厂而不是网关管理。

改为 Autowiring 连接工厂,并使用 getOpenConnectionIds()

使用closeConnection(String connectionId)关闭连接。

编辑

@SpringBootApplication 
public class So44760185Application { 
 
    public static void main(String[] args) { 
        ConfigurableApplicationContext context = SpringApplication.run(So44760185Application.class, args); 
        MessageChannel channel = context.getBean("clientChannel", MessageChannel.class); 
        try { 
            channel.send(new GenericMessage<>("foo")); 
            System.err.println("Expected ConnectException"); 
        } 
        catch (MessagingException e) { 
            if (!(e.getCause().getCause() instanceof ConnectException)) { 
                throw e; 
            } 
            System.out.println("good1"); 
        } 
 
        context.getBean(ShuttingDownAdvice.class).shuttingDown = true; 
 
        try { 
            channel.send(new GenericMessage<>("foo")); 
            System.err.println("Expected shutting down exception"); 
        } 
        catch (MessagingException e) { 
            if (!(e.getCause().getMessage().equals("No new connections allowed"))) { 
                throw e; 
            } 
            System.out.println("good2"); 
        } 
        context.close(); 
    } 
 
    @Bean 
    public static TcpConnectionFactoryFactoryBean connectionFactoryBean() { 
        TcpConnectionFactoryFactoryBean f = new TcpConnectionFactoryFactoryBean(); 
        f.setType("client"); 
        f.setHost("localhost"); 
        f.setPort(1234); 
        return f; 
    } 
 
    @Bean 
    @ServiceActivator(inputChannel = "clientChannel") 
    public TcpOutboundGateway outboundGateway(AbstractClientConnectionFactory factory) throws Exception { 
        TcpOutboundGateway g = new TcpOutboundGateway(); 
        g.setConnectionFactory(factory); 
        g.setRequiresReply(true); 
        g.setAdviceChain(Collections.singletonList(advice())); 
        return g; 
    } 
 
    @Bean 
    public ShuttingDownAdvice advice() { 
        return new ShuttingDownAdvice(); 
    } 
 
    public static class ShuttingDownAdvice extends AbstractRequestHandlerAdvice { 
 
        private volatile boolean shuttingDown; 
 
        @Override 
        protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception { 
            if (this.shuttingDown) { 
                throw new RuntimeException("No new connections allowed"); 
            } 
            return callback.execute(); 
        } 
 
    } 
 
} 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!