spring 任意消息 tcp 套接字 [英] spring arbitrary messaging tcp socket

查看:31
本文介绍了spring 任意消息 tcp 套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring-integration 开发定制的双向 tcp 套接字服务器.

i am using spring-integration to develop a customized two-way tcp socket server.

服务器将处理请求/响应任务,但我无法向特定的连接 ID 发送任意消息

the server will process request / response tasks but i am not able to send an arbitrary message to specific connection-Id

我也知道可能使用 TcpSendingMessageHandlerTcpReceivingChannelAdapter 是解决方案,但我找不到任何关于如何使用它的示例代码.

also i know maybe using TcpSendingMessageHandler and TcpReceivingChannelAdapter is the solution , but i was not able to find any sample code about how to use that.

这是我的代码:

public class SocketServer {

    private Logger logger = LoggerFactory.getLogger(SocketServer.class);

    @Bean
    public AbstractServerConnectionFactory connectionFactory() {
        return new TcpNetServerConnectionFactory(2025);
    }

    @Bean
    public TcpInboundGateway TcpInboundGateway(AbstractServerConnectionFactory connectionFactory) {
        TcpInboundGateway inGate = new TcpInboundGateway();
        inGate.setConnectionFactory(connectionFactory);
        inGate.setRequestChannelName("directChannel");
        return inGate;
    }

    @Bean
    public DirectChannel directChannel() {
        return new DirectChannel();
    }

    @MessageEndpoint
    public class Echo {

        @Transformer(inputChannel = "directChannel")
        public String process(byte[] input) throws Exception {
            return new String(input).toUpperCase();
        }

    }

    public boolean sendMessage(String connectionId){
           //TODO send Message
    }
}

推荐答案

给你 - 应该是不言自明的...

Here you go - should be self-explanatory...

@SpringBootApplication
public class So41760289Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So41760289Application.class, args);
        Socket socket = SocketFactory.getDefault().createSocket("localhost", 12345);

        // request/reply
        socket.getOutputStream().write("foo\r\n".getBytes());
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(reader.readLine());

        // arbitrary send
        @SuppressWarnings("unchecked")
        Set<String> connections = context.getBean(Set.class);
        for (String connection : connections) {
            context.getBean("bar", MessageChannel.class).send(
                    MessageBuilder.withPayload("foo")
                        .setHeader(IpHeaders.CONNECTION_ID, connection)
                        .build());
        }
        System.out.println(reader.readLine());
        reader.close();
        context.close();
    }

    @Bean
    public TcpNetServerConnectionFactory cf() {
        return new TcpNetServerConnectionFactory(12345);
    }

    @Bean
    public TcpReceivingChannelAdapter receiver() {
        TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
        adapter.setConnectionFactory(cf());
        adapter.setOutputChannelName("foo");
        return adapter;
    }

    @Transformer(inputChannel = "foo", outputChannel = "bar")
    public String process(byte[] in) {
        return new String(in).toUpperCase();
    }

    @Bean
    @ServiceActivator(inputChannel = "bar")
    public TcpSendingMessageHandler sender() {
        TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
        handler.setConnectionFactory(cf());
        return handler;
    }

    @Bean
    public Set<String> connections() {
        return Collections.synchronizedSet(new HashSet<>());
    }

    @Bean
    public ApplicationListener<TcpConnectionEvent> listener() {
        return new ApplicationListener<TcpConnectionEvent>() {

            @Override
            public void onApplicationEvent(TcpConnectionEvent event) {
                if (event instanceof TcpConnectionOpenEvent) {
                    connections().add(event.getConnectionId());
                }
                else if (event instanceof TcpConnectionCloseEvent) {
                    connections().remove(event.getConnectionId());
                }
            }

        };
    }

}

这篇关于spring 任意消息 tcp 套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆