spring集成tcp侦听同一套接字上的第二个响应

0ejtzxu1  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(267)

我有一个spring启动项目,我正在使用spring集成来进行tcp通信。我将请求发送到服务器,服务器首先返回06作为ack。几秒钟后,实际响应来自服务器。有时,ack和实际响应会同时出现,但大多数情况下它们不会。在spring集成中,我如何处理这个问题?收到确认后,我该怎么做才能等待实际响应?或者我怎样才能理解实际的React?我该怎么做?
这是我的tcp配置:

<bean id="messageSerializer"
      class="MyCustomByteArraySerializer"/>

<int-ip:tcp-connection-factory id="tcpClientFactory"
                               type="client"
                               host="${host}"
                               port="${port}"
                               so-keep-alive="true"
                               single-use="false"
                               using-nio="true"
                               serializer="messageSerializer"
                               deserializer="messageSerializer"/>

<ip:tcp-outbound-gateway id="outboundGateway"
                         connection-factory="tcpClientFactory"
                         request-channel="toTcpAdapterChannel"
                         reply-channel="fromTcpAdapterChannel"
                         auto-startup="true"
                         request-timeout="90000"
                         remote-timeout="90000"
                         reply-timeout="90000"/>

编辑:我的反序列化程序如下:

@Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        int length = inputStream.available();
        logger.info("Available length of inputStream is: {}", length);
        byte[] incomingArray = new byte[length];
        this.read(inputStream, incomingArray, false);
        if (length == 1) {
            logger.info("Length is 1. Incoming byte: {}", HexUtils.toHexString(incomingArray));
            return null;
        } else {
            System.out.println("Message DUMP-1! " + HexUtils.toHexString(incomingArray));
            incomingArray = Arrays.copyOfRange(incomingArray, 1, length - 1);
        }
        return incomingArray;

    }

编辑2:把代码带到while循环解决了我的问题。

@Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        boolean allMessageRead = false;
        byte[] incomingArray = new byte[0];
        while (!allMessageRead) {
            int length = inputStream.available();
            incomingArray = new byte[length];
            this.read(inputStream, incomingArray, false);
            if (length == 1) {
                logger.info("Available length of inputStream is: {}", length);
                logger.info("Length is 1. Incoming byte: {}", HexUtils.toHexString(incomingArray));
                return null;
            } else if (length > 1) {
                System.out.println("Message DUMP-1! " + HexUtils.toHexString(incomingArray));
                incomingArray = Arrays.copyOfRange(incomingArray, 1, length - 3);
                allMessageRead = true;
            }
        }

        return incomingArray;
    }
mwg9r5ms

mwg9r5ms1#

您需要使用自定义反序列化程序并丢弃您不感兴趣的部分。

相关问题