Spring Integration和TCP服务器套接字-如何向客户端发送消息?

阿隆·洛林茨(Aron Lorincz)

我正在尝试在Spring中创建一个侦听TCP端口并接受连接的服务器。我知道如何将传入请求路由到我的服务,并且它可以响应这些请求。但是,我想在没有收到任何请求的情况下将消息发送给某些客户端。例如,有时我必须通知客户有关它已收到消息的信息。

为此,我认为我需要一种识别客户端的方法,例如,让它们登录。是否有一种方法可以为每个可以存储登录数据的活动连接使用“会话”对象?

我如何向使用用户名X登录的客户端发送消息?

春天有可能吗?

加里·罗素:

从3.0版开始;现在,当连接状态发生更改时,框架将发出连接事件您可以使用ApplicationListener或使用捕获这些事件<event:inbound-channel-adapter/>

TcpConnectionOpenEvent包含一个connectionId; 您可以在知道连接ID的情况下向任意连接发送任意消息,方法是在消息中填充IpHeaders.connectionId标头(ip_connectionId),然后将其发送到<tcp:outbound-channel-adapter/>

如果您需要支持请求/答复以及发送任意消息,则需要使用一对协作的通道适配器进行所有通信,而不是网关。

编辑

这是一个简单的启动应用程序...

package com.example;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

import javax.net.SocketFactory;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.ip.IpHeaders;
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
import org.springframework.integration.ip.tcp.TcpSendingMessageHandler;
import org.springframework.integration.ip.tcp.connection.TcpConnectionOpenEvent;
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpServerConnectionFactory;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

@SpringBootApplication
public class So25102101Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(So25102101Application.class)
                .web(false)
                .run(args);
        int port = context.getBean(TcpServerConnectionFactory.class).getPort();
        Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line = reader.readLine();
        System.out.println(line);
        context.close();
    }

    @Bean
    public TcpReceivingChannelAdapter server(TcpNetServerConnectionFactory cf) {
        TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
        adapter.setConnectionFactory(cf);
        adapter.setOutputChannel(inputChannel());
        return adapter;
    }

    @Bean
    public MessageChannel inputChannel() {
        return new QueueChannel();
    }

    @Bean
    public MessageChannel outputChannel() {
        return new DirectChannel();
    }

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

    @Bean
    public IntegrationFlow outbound() {
        return IntegrationFlows.from(outputChannel())
                .handle(sender())
                .get();
    }

    @Bean
    public MessageHandler sender() {
        TcpSendingMessageHandler tcpSendingMessageHandler = new TcpSendingMessageHandler();
        tcpSendingMessageHandler.setConnectionFactory(cf());
        return tcpSendingMessageHandler;
    }

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

            @Override
            public void onApplicationEvent(TcpConnectionOpenEvent event) {
                outputChannel().send(MessageBuilder.withPayload("foo")
                        .setHeader(IpHeaders.CONNECTION_ID, event.getConnectionId())
                        .build());
            }

        };
    }

}

Pom Deps:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-integration</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-ip</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用STOMP从Spring WebSocket服务器向WebSocket客户端发送消息?

Spring Integration TCP服务器将消息发送到TCP客户端

Spring Integration TCP客户端上的多个套接字连接到一个服务器地址

如何从服务器向客户端发送消息

Vscode语言客户端扩展-如何从服务器向客户端发送消息?

Spring Integration ByteArrayRawSerializer TCP客户端服务器

如何从客户端桌面应用程序向服务器端Spring应用程序发送请求?

如何使用Java中的套接字从服务器向特定客户端发送字符串消息?

从Node JS Socket客户端向MINA套接字服务器发送消息

服务器无法通过C中的套接字向客户端发送消息

通过套接字输出流从 Swift 客户端向 Java 服务器发送消息

如何将 XML 消息从 Python 客户端发送到 Spring 启动微服务

带套接字的TCP客户端/服务器,服务器向客户端发送文件,客户端挂起,Python

从服务器向 websocket 客户端发送消息

服务器向客户端发送消息

TCP套接字服务器使用Spring集成同时处理多个客户端连接

套接字:向多个客户端发送消息

使用Web套接字向客户端发送消息

如何从 Python 服务器向 Java 客户端发送消息

如何从Flask服务器(Python)向HTML客户端发送消息?

在ASP.NET Core SignalR中,如何从服务器向客户端发送消息?

Java如何使服务器向连接的每个客户端发送消息

如何使用Java RMI从服务器向客户端发送消息?

如何使用端口从服务器向客户端发送消息?

如何从Qt中的服务器向连接的客户端发送消息

向所有客户端发送消息(客户端-服务器通信)

如何使用 spring-integration 创建异步单例套接字服务器?

定期通过Spring网络套接字发送消息给客户端

使用套接字通过TCP从服务器端向客户端发送ArrayList <String>?