使用spring-integration-ip的Java服务器

时间:2013-10-31 02:09:54

标签: java spring-integration

服务器使用java,并通过TCP与C ++客户端通信。当用户离开计算机超过5分钟时,客户端将向服务器发送一个字节数组.java服务器由spring-integration-ip编写。如果我应该设置使用-nio =“true”,我会感到困惑吗?有什么问题吗?关于并发性?我如何使用自定义协议转换字节数组?我是TCP和spring-integration的新手,感谢您的帮助!

当我按照示例编写应用程序时遇到问题。服务器反序列化字节流然后回显服务,但客户端在回显服务返回后无法接收序列化字节流。

弹簧tcp.xml:

<int-ip:tcp-connection-factory 
     id="serverConnectionFactory"
     type="server" port="5678" 
     using-nio="true" 
     serializer="connectionSerializeDeserialize"
     deserializer="connectionSerializeDeserialize" />

<bean id="connectionSerializeDeserialize" 
      class="com.snail.tcp.CustomSerializerDeserializer" />

<int-ip:tcp-inbound-gateway id="gatewayCrLf"
    connection-factory="serverConnectionFactory" request-channel="toSA"
    error-channel="errorChannel" />

<int:channel id="toSA" />

<int:service-activator input-channel="toSA"
    ref="echoService" method="test" />
<bean id="echoService" class="com.snail.tcp.EchoService" />

(de)序列化方法:

private static final int LENGTH = 2;
private static final int FLAG = 2;


@Override
public String deserialize(InputStream is) throws IOException {
    logger.debug("begin deserialize");
    int length = Integer.valueOf(parseString(is, LENGTH));
    int flag = Integer.valueOf(parseString(is, FLAG));
    String content = parseString(is, length);
    return content;
}

@Override
public void serialize(String s, OutputStream os) throws IOException {
    logger.debug("begin serialize:" + s);
    byte[] content = s.getBytes();
    os.write(content);
    os.flush();
}

回声服务:

 public String test(String input) {
    logger.debug("echo service:" + input);
    //some service...
    return "echo:" + input;
}

主要方法:

 public static void main(String[] args) {
    Socket socket = null;
    Writer out = null;
    BufferedReader in=null;
    try {
        socket = new Socket("127.0.0.1", 5678);
        socket.setSoTimeout(10000);
        out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        out.write("1001HELLOWORLD");
        out.flush();

        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        StringBuffer str = new StringBuffer();
        int c;
        while ((c = in.read()) != -1) {
            str.append((char) c);
        }
        System.out.println(str.toString());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            out.close();
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

控制台日志:

[DEBUG] com.snail.tcp.CustomSerializerDeserializer-deserialize():begin deserialize

[DEBUG] com.snail.tcp.EchoService-test():echo服务:HELLOWORLD

[DEBUG] com.snail.tcp.CustomSerializerDeserializer-serialize():begin serialize:echo:HELLOWORLD

但客户端无法接收反序列化消息'HELLOWORLD',而是收到SocketTimeoutException:读取超时... 我想知道我做错了什么,很多

1 个答案:

答案 0 :(得分:3)

框架负责并发;如果你支持大量客户,你通常只需要NIO;对于少数客户,通常不需要NIO。

您可以使用自定义(反)串行器或将转换器放在入站端点的下游。

我建议您阅读Documentation,查看samples,如果您还有任何问题,请回答更具体的问题。