Announcement Announcement Module
Collapse
No announcement yet.
Integration with TCP server (read/write string to socket) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Integration with TCP server (read/write string to socket)

    Hi,

    I want to integrate via Spring integration.
    My standalone program connects to a ip address and port.

    I write String commands to the socket. For example: "S*1,2,3,4;" always ended with the ';' character
    And in a thread I read the incoming commands from server. All incoming commands are als Strings that ends with the ';' character.

    In standalone code like this:

    Sending data to socket:
    Code:
    String command = "!@@@!!@!@!@!@;"
    
    this.socket = new Socket(this.host, this.port);
        this.in = this.socket.getInputStream();
        this.outputStreamWriter = new OutputStreamWriter(this.socket.getOutputStream());
        this.reader = new BufferedReader(
            new InputStreamReader(this.in, "UTF-8"));
    
    this.outputStreamWriter.write(command, 0, command.length());
    Reading data from socket in thread:

    Code:
    String buffer = "";
    
    int character = this.reader.read();
             String c = String.valueOf((char) character);
             c.trim();
            if (!c.equals(";")) {
              buffer = buffer + c;
    
    } else {
              buffer = buffer + c;
              String command = buffer;
    
             //do something
             buffer = "";
    }
    Now I want to integrate this functionallity in my spring appliction.
    I can use this:

    http://xebee.xebia.in/2010/11/01/dat...g-integration/

    But I don't found an example how to write my custom class to write and read the socket.
    Can someone help me with that?

    custom-socket-reader-class-name="CustomClass"
    custom-socket-writer-class-name="CustomClass"

  • #2
    That blog is a little out of date; see the documentation http://static.springsource.org/sprin...e/html/ip.html and the tcp sample https://github.com/SpringSource/spri...-client-server

    All you need is a custom serializer/deserializer. For this application, it might be as simple as subclassing ByteArraySingleTerminatorSerializer, in the same way that https://github.com/garyrussell/sprin...erializer.java does, but using ";" instead of 0x0a.

    Note: this is available in the upcoming 2.2 release (currently at release candidate 2 - 2.2.0.RC2).

    For earlier versions, you'll need to clone the code.

    Comment


    • #3
      Ok thanks!
      So I add this configuration to my application-context.xml

      Code:
      	<int-ip:tcp-connection-factory id="client"
      		type="client" host="localhost" port="5555" single-use="true"
      		so-timeout="10000" />
      
      	<int-ip:tcp-outbound-channel-adapter
      		id="outboundClient" channel="input" connection-factory="client" />
      
      
      	<int:channel id="input" />
      This means that when a message received on channel "input", it will open a socket to localhost port 5555 and write the message to that socket. yes?

      I use MessagingTemplate like this:

      Code:
      MessagingTemplate messagingTemplate = new MessagingTemplate();
          messagingTemplate.send("input", new GenericMessage<String>(command));
      I got this exception:

      Code:
      Caused by: java.lang.IllegalStateException: No 'channelResolver' specified for MessagingTemplate. Unable to invoke methods with a channel name argument.
      	at org.springframework.util.Assert.state(Assert.java:384)
      	at org.springframework.integration.core.MessagingTemplate.getRequiredChannelResolver(MessagingTemplate.java:337)
      	at org.springframework.integration.core.MessagingTemplate.resolveChannelName(MessagingTemplate.java:353)
      	at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:153)
      	at nl.eleqtron.qvisual.spring.service.GiaBusServiceImpl.sendCommand(GiaBusServiceImpl.java:13)
      	at nl.eleqtron.qvisual.vaadin.ui.MainWindow$1.buttonClick(MainWindow.java:48)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      Any idea?

      Comment


      • #4
        When you create a MessagingTemplate manually (rather than by declaring it as a bean) you can't send by channel name by default. You either have to send to a channel reference (e.g. @Autowire private MessageChannel input; ) or provide it with a channel resolver, or declare it as a <bean/> and @Autowire private MessagingTemplate template; (and Spring will wire up the template for you to enable sending by channel name).

        However, it's much better to isolate your sending code from Spring Integration by using a Messaging Gateway...

        http://static.springsource.org/sprin...r.html#gateway

        Comment

        Working...
        X