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


问题内容

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

如前所述,旧版系统仅适用于TCP / ip,而不适用于HTTP,因此无需讨论HTTP的更好之处,谢谢!


问题答案:

参见Spring Integration。您可以简单地使用消息传递网关将SI流连接到MVC控制器中

控制器->网关-> {可选过滤/转换}-> TCP出站网关

网关使用其服务接口注入到控制器中。

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)"/>

并将网关注入到您的@Controller

public interface SimpleGateway {

    public String send(String text);

}

@Controller 
public class MyController {

        @Autowired
        SimpleGateway gw;

     ...
}