Spring TCP Client without Transformer

≡放荡痞女 提交于 2019-12-20 05:57:37

问题


I followed this example for setting up a TCP Client in Spring. Below is my tcpClientServerDemo-context.xml file where the transformer lies. Can someone help me remove the transformer and send the data as it is without any modifications? If I try to remove the line reply-channel='clientBytes2StringChannel' or even make it null, I get exceptions when building the project.

<?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="hello.SimpleGateway"
                 default-request-channel="input"/>

    <int-ip:tcp-connection-factory id="client"
                                   type="client"
                                   host="192.86.33.61"
                                   serializer="CustomSerializerDeserializer"
                                   deserializer="CustomSerializerDeserializer"
                                   port="${availableServerSocket}"
                                   single-use="true"
                                   so-timeout="10000"/>

    <bean id="CustomSerializerDeserializer" class="hello.CustomSerializerDeserializer" />

    <int:channel id="input" />

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

    <!-- Server side -->
    <!-- When creating the socket factory on the server side, we specify both the serializer and deserializer
    which deals with both accepting a stream formatted with the Stx-Etx bytes as well as sending a stream
    formatted with the Stx-Etx bytes. -->
    <int-ip:tcp-connection-factory id="serverConnectionFactory"
                                   type="server"
                                   port="${availableServerSocket}"
                                   single-use="true"
                                   so-linger="10000"
                                   serializer="Custom1SerializerDeserializer"
                                   deserializer="Custom1SerializerDeserializer"/>


    <bean id="Custom1SerializerDeserializer" class="hello.CustomSerializerDeserializer1" />

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

    <!-- We leave a message listener off of this channel on purpose because we hook
    one up before the test actually runs (see the unit test associated with this
    context file) -->
    <int:channel id="incomingServerChannel" />

</beans>

EDIT:

Now I am able to send the messages using a custom serializer/deserializer. But unfortunately, I am unable to receive the responses. Here is my serializer/deserializer:

public class CustomSerializerDeserializer implements Serializer<String>, Deserializer<String> {
protected final Log logger = LogFactory.getLog(this.getClass());

public void serialize(String input, OutputStream outputStream) throws IOException {
    logger.info("inside serialize");
    outputStream.write(buildSampleMsg(input));
    outputStream.flush();
}

public String deserialize(InputStream inputStream) throws IOException {
    logger.info("inside deserialize");
    final int bufferSize = 1024;
    final char[] buffer = new char[bufferSize];
    final StringBuilder out = new StringBuilder();
    Reader in = new InputStreamReader(inputStream, "UTF-8");
    for (;;) {
        int rsz = in.read(buffer, 0, buffer.length);
        if (rsz < 0) {
            break;
        }
        out.append(buffer, 0, rsz);
    }
    logger.info(out.toString());
    return out.toString();
}

public byte[] buildSampleMsg(String body){
    logger.info("inside buildsamplemsg");
    ......
    return hexStringToByteArray(data);
}

I have some logging done on the first line of the serializer/deserializer but the log is never printed. Which in turn means, we don't get any response. Any help will be appreciated.


回答1:


Removing the reply-channel is correct. You don't give any indication of the error, but the gateway interface method return type must be changed to byte[].

EDIT

Clearly you are doing something wrong if the mainframe is getting junk. You should do the EBCDIC conversion after you convert your "ABCD" to a byte[] (with getBytes()); if you have EBCDIC chars in a String, that won't work. Also, bear in mind the default serializer will add CRLF (ascii) to the output. If that mainframe can determine the end of message from the data itself, you can use a ByteArrayRawSerializer in the serializer attribute. However, you will need a custom deserializer because the framework won't know how to construct a message from the stream; unless the mainframe closes the socket after sending the reply, in which case a ByteArrayRawSerializer will work as the deserializer attribute.

A common technique used to communicate with mainframes (and others) is to use a 1, 2, or 4 byte length header (network byte order). The ByteArrayLengthHeaderSerializer does just that.

If the mainframe is expecting EBCDIC delimiters, you'll need a custom serializer/deserializer - it might make more sense to do the EBCDIC conversion there, separating it from your application logic.

You can read about serializers/deserializers here.

TCP is a streaming protocol; this means that some structure has to be provided to data transported over TCP, so the receiver can demarcate the data into discrete messages. Connection factories are configured to use (de)serializers to convert between the message payload and the bits that are sent over TCP. This is accomplished by providing a deserializer and serializer for inbound and outbound messages respectively. A number of standard (de)serializers are provided.

The ByteArrayCrlfSerializer, converts a byte array to a stream of bytes followed by carriage return and linefeed characters (\r\n). This is the default (de)serializer and can be used with telnet as a client, for example.



来源:https://stackoverflow.com/questions/45367943/spring-tcp-client-without-transformer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!