6

I wonder if it is possible to plug a bidirectional connection between a spring mvc application and a legacy system working with TCP-IP connections.

as mentionned the legacy system works with TCP/ip and not http , so no need to talk about how HTTP is better , thanks !

romu31
  • 821
  • 9
  • 17
  • I don't know the answer (I'm not familiar with `spring`), but `the legacy systren works with TCP/ip not http , so no need to talk about how HTTP is better` doesn't really make any sense; they're not competing protocols. – admdrew Dec 13 '13 at 19:09
  • yes but on other answers people says http is better and so on – romu31 Dec 13 '13 at 19:13

1 Answers1

8

See Spring Integration. You can simply wire an SI flow into your MVC controller using a messaging gateway

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

The gateway is injected into the controller using its service-interface.

The tcp-client-server sample shows how.

EDIT:

If it's not clear from the sample, you would need to define your SI flow...

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

and inject the Gateway into your @Controller...

public interface SimpleGateway {

    public String send(String text);

}

@Controller 
public class MyController {

        @Autowired
        SimpleGateway gw;

     ...
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • thanks , so if i get it right the gateway will be a member of the controller ? – romu31 Dec 13 '13 at 19:45
  • I think my question could be what is the best solution to boostrap a TCP/IP client server into a spring MVC application – romu31 Dec 13 '13 at 20:05
  • I thought that's what I answered; in case it's not clear from the sample, I updated the answer. The complete documentation for the TCP stuff is here: http://docs.spring.io/spring-integration/docs/2.2.6.RELEASE/reference/html/ip.html If you are saying you want your controller to act as server (rather than the client), then you can use the server side of the sample. – Gary Russell Dec 13 '13 at 20:32
  • thanks I checked the basic sample from github earlier this afternoon. If it is not too much troubles , I was also interested about best practice , is it better to connect in the controller or an the service layer ? The webapp is based on Spring MVC websockets (with Stomp) and 50% of the incoming / outcomming request will be by TCP . Thanks again for your answer – romu31 Dec 13 '13 at 22:15
  • It's still not clear to me whether you are the TCP server or client; sounds like you want to provide a server that can accept Websocket and TCP connections (I had assumed you wanted to interface with some other legacy server FROM the controller. If you can clarify, it will be easier for me to answer. – Gary Russell Dec 13 '13 at 22:23
  • 1) I plugged the code into a light spring mvc I got and it worked nicely .2) With the spring mvc websockets app, I had an exception but I think I can manage 3) this code is good for Spring webapp to legacy system . For legacy system to Spring Mvc I am still wondering about how to plug it/bootstrap , something like a TCP listener into Spring MVC . – romu31 Dec 13 '13 at 23:03
  • Take a look at the server side of the sample; you use a TCP inbound gatway to receive/reply; notice the service activator - the ref can be your controller - invoke one of your existing methods, or a new method. Depending on your needs, you might need to add transformers to transform from/to the byte[]s. Think of the service-activator as being the reverse of the gateway, where the SI flow invokes some POJO and returns the result. In the sample, the POJO is a simple bean but it could easily be a controller. – Gary Russell Dec 13 '13 at 23:21
  • Would these Gateway, tcp-connection factory, etc...definitions go in the same file my bean definitions are located in? I'm just now attempting to accomplish the same thing romu31 was trying to do. I have a WiFi module that needs to send messages to my Spring MVC server, and then I need to send messages to that WiFi module when a certain route is hit (for example). – kibowki Nov 13 '14 at 19:40
  • Yes; you can put them in the `DispatcherServlet`'s config file, or, most common, put them in the root application context (loaded by the `ContextLoaderListener`), along with other 'back-end' service beans. Beans in the servlet's context (controllers etc) can "see" root context beans, but not the other way around; typically, the gateway is in the root context, and wired into the controller(s). – Gary Russell Nov 13 '14 at 20:00
  • @GaryRussell Got it, thank you. I will try this out and see how it goes. – kibowki Nov 14 '14 at 02:26
  • @GaryRussell thanks for the response, but I am not familiar with Spring boot. Where exactly is the XML stuff like "" supposed to go ? Even the documentation at http://docs.spring.io/spring-integration/reference/html/ip.html makes no mention of where to put this XML – Eresse May 15 '17 at 14:34
  • You should ask a new question - this one is very old; to use XML configuration in a boot application, you use `@ImportResource("some.xml")`; as I said this is a very old question - there are more modern ways to configure a boot app using Java. Ask a new question and someone will help. – Gary Russell May 15 '17 at 15:11
  • http://stackoverflow.com/questions/43983296/how-do-i-setup-tcpconnectionfactory-or-sslserversocketfactory-from-java-in-sprin/43984753#43984753 – Gary Russell May 15 '17 at 16:43