Spring TCP Socket客户端未从大型机接收数据 [英] Spring TCP Socket client not receiving data from Mainframe

查看:31
本文介绍了Spring TCP Socket客户端未从大型机接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 此示例 向使用HEX 数据进行通信的大型机发送和接收数据.所以我的配置中有以下内容:

I am trying to use this example to send and receive the data to and from a Mainframe machine that uses HEX data to communicate. So I have the following in my configuration:

    <?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>

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

And in my Serializer/Deserializer, I have the following content:

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();
}

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

The input in itself is a hex string(assume so). So the message is being sent and the response also is being received properly but the output is not being passed back to the Echo Service. Why is that not happening? Is there anything wrong in my configuration?

推荐答案

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

This is not a DEBUG log, only INFO; your log4j configuration must be wrong.

内部反序列化

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

This is simply the receiving thread waiting for data; it is stuck in your for loop waiting for the mainframe to close the connection - I suggest you add some debug inside the loop.

编辑

您的 POM 中缺少公共日志记录...

Commons-logging is missing from your POM...

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

或切换到 log4j2

or switch to log4j2

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

并创建一个 log4j2 配置文件.

and create a log4j2 config file.

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

Spring Framework 5 now uses its own implementation of commons-logging that does not support log4j 1.x.

这篇关于Spring TCP Socket客户端未从大型机接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆