如何在spring MVC应用程序中插入TCP-IP客户端服务器

rom*_*u31 5 spring-mvc spring-integration

我想知道是否可以在spring mvc应用程序和使用TCP-IP连接的遗留系统之间插入双向连接.

提到遗留系统可以使用TCP/ip而不是http,所以不需要谈论HTTP如何更好,谢谢!

Gar*_*ell 8

请参阅Spring Integration.您只需使用消息传递网关将SI流连接到MVC控制器即可

Controller-> gateway - > {optional filtering/transforming} - > tcp outbound gateway

网关使用其服务接口注入控制器.

TCP客户端-服务器示例演示如何.

编辑:

如果从样本中不清楚,您需要定义SI流量...

<!-- Client side -->

<int:gateway id="gw"
    service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
    default-request-channel="input"/>

<int-ip:tcp-connection-factory id="client"
    type="client"
    host="localhost"
    port="1234"
    single-use="true"
    so-timeout="10000"/>

<int:channel id="input" />

<int-ip:tcp-outbound-gateway id="outGateway"
    request-channel="input"
    reply-channel="clientBytes2StringChannel"
    connection-factory="client"
    request-timeout="10000"
    remote-timeout="10000"/>

<int:transformer id="clientBytes2String"
    input-channel="clientBytes2StringChannel"
    expression="new String(payload)"/>
Run Code Online (Sandbox Code Playgroud)

并将Gateway注入您的@Controller...

public interface SimpleGateway {

    public String send(String text);

}

@Controller 
public class MyController {

        @Autowired
        SimpleGateway gw;

     ...
}
Run Code Online (Sandbox Code Playgroud)