spring任意消息传递tcp套接字

2024-03-18

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

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

我也知道也许使用TcpSendingMessageHandler and TcpReceivingChannelAdapter是解决方案,但我找不到任何有关如何使用它的示例代码。

这是我的代码:

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
    }
}

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

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

        };
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

spring任意消息传递tcp套接字 的相关文章

随机推荐

  • 在“.describe()”中显示 pandas-dataframe 的所有列

    我被困在这里 但我这是一个由两部分组成的问题 查看 describe include all 的输出 并非所有列都显示 如何显示所有列 这是我在使用 Spyder 时经常遇到的一个常见问题 即如何在控制台中显示所有列 任何帮助表示赞赏 im
  • 元素内部的#shadow-root 是什么?

    我今天看到了一些奇怪的事情 查看与该帖子相关的图片 如下 我已经做了一个输入 type text 它是屏幕图片上的 1 它的 css 看起来像这样 table tbody input width 40px border none heigh
  • 蒙版和剪辑 GLSurfaceView

    我使用的 SDK 通过回调提供矩形 glsurfaceview 我希望能够以圆形布局渲染此视图 即 我想在圆形视图上显示视图 我尝试过使用屏蔽布局 例如使用可屏蔽布局https github com christophesmet andro
  • 检查Flutter应用是否有可用的互联网连接

    我有一个要执行的网络调用 但在此之前 我需要检查设备是否具有互联网连接 这就是我到目前为止所做的 var connectivityResult new Connectivity checkConnectivity User defined
  • Angular 6.1.0 - 恢复滚动位置未按预期工作

    RouterModule forRoot routes scrollPositionRestoration enabled 6 1 0 中的这项新功能无法按预期工作 看来 ViewportScroller 服务尝试在填充 DOM 元素之前恢
  • 使用 ggplot2 将带有文本的图像作为刻度标签

    如何使用以下命令将本地图像文件作为刻度标签和国旗下的国家 地区名称ggplot2 我想实现这样的目标 数据如下 countries c Norway Spain Germany Canada China values c 10 20 30
  • 如何在javascript中计算LaTeX公式?

    我有 LaTeX 格式的 JavaScript 字符串 例如 frac y 2 2 x frac 2 sqrt y 2 x y 2 我希望能够用定义的变量来评估它 有谁知道用于此目的的框架或库 我尝试在谷歌和堆栈中找到它 但没有成功 如果您
  • AttributeError:“模块”对象没有属性“newperson”

    我目前正在学习 python 编程 并且是初学者 目前我陷入了文件练习 所以这些是我需要做的设定事情 而不是做我想做的任何事情 不幸的是 这也意味着我可能无法做任何复杂的 对我来说 快捷方式 目前使用Python 3 2 2 我需要两个不同
  • Javascript ES6 中注册表符号 (Symbol.for) 的用途是什么?

    有这样一件事registryJS ES6 中的符号 发现在这篇 Mozilla 文章中 https hacks mozilla org 2015 06 es6 in depth symbols 它不同于Symbol Stack Overfl
  • Xcode 4 中的 openssl 库

    正如标题所示 我正在努力将 openssl 库包含在我的 xcode iOs 项目中 例如 include
  • 如何在 CMake 中将多个库目标分组为一个

    我正在尝试将多个目标分组为一个目标 以便下游用户只需要链接到该单个目标 下游用户不需要查找所有目标 并且上游库中的所有功能都可以通过链接到该目标来使用 请参阅下面我失败的尝试的 CMakeLists cmake minimum requir
  • Hovertemplate 在绘图中与 add_trace() 一起使用时显示数据两次

    我使用以下方法创建了分组条形图plotly 问题是在我的hovertemplate为了追踪我得到一个白色的盒子LA ZOO这似乎是不必要的 我想摆脱它 Animals lt c giraffes orangutans monkeys SF
  • MSDeploy 是否足够“友好”,或者可以将其封装在 MSI 文件中

    您认为 MSDeploy 包是让最终用户在其系统上安装 Web 应用程序的一个不错的选择吗 与使用 MSI 文件安装 Web 应用程序的体验相比如何 有人尝试过将 MSDeploy 包封装在 MSI 包中吗 会起作用吗 据我描述 MSDep
  • JavaFX 中加载器实例化抛出空指针

    我已经声明了两个 fxml 文件 并为每个文件声明了一个控制器 根布局控制器是一个控制器根布局 fxml and 概览控制器是一个控制器概述 fxml rootlayout 有带有文件打开项的菜单栏 overviewcontroller 有
  • Visual C++ 2012 cout 在运行时崩溃

    我今天决定尝试一下 Visual Studio 2012 Express 首先要做的就是写 Hello world 但是 我无法使其工作 我创建了一个 Windows 控制台应用程序项目 编写了标准代码 但导致了运行时错误 这是我的代码 i
  • Azure 上的 React + Express:无效的主机标头

    错误 当部署到具有多容器支持的 Azure Web Apps 时 我收到 无效的主机标头 消息来自https mysite azurewebsites com https mysite azurewebsites com 本地设置 这运行良
  • 无法在 Jenkins 上找到 TFS 插件 [重复]

    这个问题在这里已经有答案了 无法找到适用于 Jenkins 的 TFVC 或 Azure DevOps 和 Team Foundation Server 插件 詹金斯版本 2 263 1 由于安全漏洞 TFS 插件的官方分发已暂停 在 Je
  • 空值 - 布尔表达式

    所以我有一个关于考试作业的问题 在这个作业中我们有一堆布尔表达式 例如 FALSE OR NULL NULL 然后我们需要写出布尔表达式的值 为此 我使用了三值逻辑 但是当您获得如下布尔表达式时 它如何应用 NULLL AND TRUE O
  • 如何使用 sql 查询将数据库的子集提取到 dbunit 文件中?

    Why 我有一个很大的 Oracle 表 我想测试一些 DAO 方法 为此 我使用 dbunit Problem 我想使用 sql 查询将现有数据库的子集提取为 dbunit 平面 xml 文件 查询示例 Select t1 field1
  • spring任意消息传递tcp套接字

    我正在使用 spring integration 开发定制的双向 TCP 套接字服务器 服务器将处理请求 响应任务 但我无法向特定的连接 ID 发送任意消息 我也知道也许使用TcpSendingMessageHandler and TcpR