我正在尝试使用this example向使用HEX数据进行通信的大型机计算机收发数据。所以我的配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">

    <context:property-placeholder />

    <!-- 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="<ip>"
                                   serializer="CustomSerializerDeserializer"
                                   deserializer="CustomSerializerDeserializer"
                                   port="${availableServerSocket}"
                                   single-use="false"
                                   so-timeout="1800000"
                                   using-nio="false" />

    <bean id="CustomSerializerDeserializer" class="org.springframework.integration.samples.tcpclientserver.CustomSerializerDeserializer" />


    <int:channel id="input" />

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

    <!-- Server side -->

    <int-ip:tcp-connection-factory id="crLfServer"
                                   type="server"
                                   port="${availableServerSocket}"/>

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

    <int:channel id="toSA" />

    <int:service-activator input-channel="toSA"
                           ref="echoService"
                           method="test"/>

    <bean id="echoService"
          class="org.springframework.integration.samples.tcpclientserver.EchoService" />

    <int:object-to-string-transformer id="serverBytes2String"
                                      input-channel="serverBytes2StringChannel"
                                      output-channel="toSA"/>

</beans>


在我的序列化器/反序列化器中,我具有以下内容:

public void serialize(String input, OutputStream outputStream) throws IOException {
    outputStream.write(buildmsg(input));
    outputStream.flush();
}

public String deserialize(InputStream inputStream) throws IOException {
    String data = "";
    logger.info("inside deserialize");
    DataInputStream dis = new DataInputStream(inputStream);
    int len = dis.readInt();
    if (len > 0) {
        logger.info("len: " + decimaltohex(len));
        logger.info("data: " + data);
        byte[] b = new byte[dis.available()];
        dis.readFully(b);
        data += decimaltohex(len);
        data += byteArrayToHex(b);
    }
    logger.info("full data:" + data);
    return data;
}

public byte[] buildmsg(String body) throws UnsupportedEncodingException {
    String header = "00000000";
    String totMsg = header + body;
    header = massageheader(decimaltohex(totMsg.length() / 2));
    totMsg = header + body;
    logger.info("sending data : " + totMsg);
    return hexStringToByteArray(totMsg);
}

public String decimaltohex(int data) {
    return Integer.toHexString(data);
}

public static String massageheader(String data) {
    String str = String.format("%8s", data).replace(' ', '0').toUpperCase();
    return str;
}

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                + Character.digit(s.charAt(i + 1), 16));
    }
    return data;
}

public static String byteArrayToHex(byte[] a) {
    StringBuilder sb = new StringBuilder(a.length * 2);
    for (byte b : a) {
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}


输入本身是一个十六进制字符串(假设是)。因此,正在发送消息,并且也正确接收了响应,但是没有将输出传递回Echo服务。为什么没有发生?我的配置有问题吗?

最佳答案

这不是DEBUG日志,只有INFO;您的log4j配置一定是错误的。


  内部反序列化


这仅仅是等待数据的接收线程;它卡在for循环中,等待大型机关闭连接-我建议您在循环内添加一些调试。

编辑

您的POM中缺少Commons-logging ...

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>


或切换到log4j2

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.7</version>
</dependency>


并创建一个log4j2配置文件。



    
        
            
        
    
    
        
            
        
    


Spring Framework 5现在使用其自己的不支持log4j 1.x的commons-logging实现。

09-06 01:51
查看更多