当前位置: 首页 > 知识库问答 >
问题:

用Spring集成框架实现Tcp连接

袁和通
2023-03-14

我试图创建一个接受入站连接的Tcp服务器,并异步地向连接的客户端发送消息。有一个Tcp服务器的示例,但它使用的是网关,是请求/响应,不支持异步。

    null
@EnableIntegration
@IntegrationComponentScan
@Configuration
public class IntegrationConfig implements
        ApplicationListener<TcpConnectionEvent> {
    @Value("${listen.port:8000}")
    private int port;

    @Bean  //for accepting text message from TCP, putty
    public MessageChannel fromTcp() {
        return new DirectChannel();
    }

    @Bean  //for sending text message to TCP client, outbound
    public MessageChannel toTcp() {
        return new DirectChannel();
    }

    // receive from MVC controller
    @Bean
    public MessageChannel invokeChannel() {
        return new DirectChannel();
    }   

    @Bean  //inbound, it is working, I could read the inbound message while debugging
    public TcpReceivingChannelAdapter in(
            AbstractServerConnectionFactory connectionFactory) {
        TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
        adapter.setOutputChannel(fromTcp());
        adapter.setConnectionFactory(connectionFactory);
        return adapter;
    }

    //transform TCP bytes to string message, working
    @Transformer(inputChannel = "fromTcp", outputChannel = "toCollaborate")
    public String convert(byte[] bytes) {

        return new String(bytes);
    }

    MessageHeaders staticheader;  //save ip_connectinId, use this to collaborate outbound message later, for testing purpose only
    @ServiceActivator(inputChannel = "toCollaborate", outputChannel = "toTcp")
    public Message<String> handleTcpMessage(Message<String> stringMsg) {
        staticheader = stringMsg.getHeaders();
        return stringMsg;
        // save the header, collaborate to output channel
    }

    //collaborate message from REST API invokeChannel to a outbound tcp client, this fail
    @Transformer(inputChannel = "invokeChannel", outputChannel = "toTcp")
    public Message<String> headerBeforeSend(String test) {
        GenericMessage<String> msg = new GenericMessage<String>(
                "from rest api");
        if (staticheader != null) {         
            MessageBuilder
                    .fromMessage(msg)
                    .setHeader("ip_connectionId",
                            staticheader.get("ip_connectionId")).build();
        }
        return msg;
    }

    @ServiceActivator(inputChannel = "toTcp")
    @Bean
    public TcpSendingMessageHandler out(
            AbstractServerConnectionFactory connectionFactory) {
        TcpSendingMessageHandler tcpOutboundAdp = new TcpSendingMessageHandler();
        tcpOutboundAdp.setConnectionFactory(connectionFactory);


        return tcpOutboundAdp;
    }   

    // should need only 1 factory? and keep connectin alive
    // server for in coming connection
    @Bean
    public AbstractServerConnectionFactory serverCF() {
        return new TcpNetServerConnectionFactory(this.port);
    }

    @Override
    public void onApplicationEvent(TcpConnectionEvent tcpEvent) {
        // TODO Auto-generated method stub
        TcpConnection source = (TcpConnection) tcpEvent.getSource();

    }

}
//The MVC controller
@Autowired
    MessageChannel invokeChannel;
    @RequestMapping(value="/invoke")
    public String sayHello()
    {
        //trigger gateway to send a message
        String msg = "hello";
        MessagingTemplate template = new MessagingTemplate();
        template.send(invokeChannel, new GenericMessage<String>(msg));      
        return msg;
    }

异常org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.messaging.messagehandlingException:无法找到出站套接字org.springframework.web.servlet.frameworkservlet.processRequest(frameworkservlet.java:981)org.springframework.web.servlet.frameworkservlet.doget((wsfilter.java:52)

共有1个答案

鄂曦之
2023-03-14

是;默认情况下,连接保持打开;是的,您可以使用@serviceactivator来处理请求;是的,您只需设置连接id标头。

要在Java配置中配置出站适配器,请将@serviceactivator添加到处理程序bean...

@Bean
public TcpReceivingChannelAdapter() in() {

    ...
    adapter.setOutputChannel(newRequests());

}


...

@ServiceActivator(inputChannel="toClientChannel")
@Bean
public TcpSendingMessageHandler out() {

    ...

}
 类似资料:
  • 我试图使用Spring Integration创建一个iso8385 TCP服务器。典型的情况如下: 客户端连接到服务器并保存ISO8385消息 服务器处理消息 服务器制定响应并关闭连接 我希望跟踪每个新的TCP连接,并为其创建一个标识符,以便将每个处理与连接的客户端相关联。但我不知道怎么做。这个想法是: 将ISO8385转换为java类的tcp入站适配器 将处理消息的服务激活器 将java类转换

  • 基于此,有人对如何使用spring Integration实现此功能有什么建议吗?

  • 本文向大家介绍Spring集成MyBatis框架,包括了Spring集成MyBatis框架的使用技巧和注意事项,需要的朋友参考一下 Java在写数据库查询时,我接触过四种方式: 1、纯Java代码,引用对应的数据库驱动包,自己写连接与释放逻辑(可以用连接池) 这种模式实际上性能是非常不错的,但是使用起来并不是非常方便:一是要手工为Connection做获取与释放,大量的冗余代码也容易出错;另一个是

  • 问题内容: 我已经开发了一个Spring / JPA应用程序: 服务,存储库和域层即将完成 。 该 所缺的只是层是网络层 。我正在考虑将Playframework 2.0用于Web层,但不确定是否可以 在Playframework 2.0类中注入/使用spring bean 。 这可能吗?如果可以,怎么办? 问题答案: 您可以。已针对Play 2.5.x更新: https://github.com

  • 1. 前言 通过几个章节的学习,大家对于 Spring 已经有了初步的认知,我们通过案例练习,或者源码追踪,可以粗略的看到 Spring 框架初始化 bean 对象的过程,那么这个章节,我们模拟 Spring 框架的思路,来写一个类似 Spring 加载对象的案例,加深大家的印象。 2. 案例实现思路 2.1 步骤介绍 思路分析: 我们通过写过的案例可以知道: Spring 框架的容器 是一个接口

  • 如果你正在建设一个纯静态的应用程序(与后端api分离部署),那么你可能甚至不需要编辑config/index.js。但是,如果你想要这个模板与现有的后端框架集成,例如Rails/Django/Laravel,拥有自己的项目结构,您可以编辑config/index.js,直接生成前端资源注入到你的后台项目。 让我们看一下默认的config/index.js: var path = require('