【问题标题】:How can I ignore serializer/deserializer in spring integration?如何在 Spring 集成中忽略序列化器/反序列化器?
【发布时间】:2015-02-17 05:01:42
【问题描述】:

我想使用 spring 集成 tcp 模块发送文件。所以我使用 tcp-connection-factory 制作了服务器端。

<int-ip:tcp-connection-factory id="crLfServer" 
        type="server" 
        host="localhost"  
        port="7777"  
        single-use="true"
        so-timeout="10000"
        serializer="serializer"
        deserializer="serializer"
        />

<bean id="serializer" class="com.sds.redca.core.normalizer.CustomSerializerDeserializer" />

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

但是,我用java中的pojo类制作了客户端,如下所示。

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;

public class SendClient {
    public static void main(String[] args) {
        String serverIp = "localhost";
        Socket socket = null;

        try {
            // 서버 연결
            socket = new Socket(serverIp, 7777);
            System.out.println("서버에 연결되었습니다.");

            FileSender fs = new FileSender(socket);
            fs.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class FileSender extends Thread {
    Socket socket;
    DataOutputStream dos;
    FileInputStream fis;
    BufferedInputStream bis;

    public FileSender(Socket socket) {
        this.socket = socket;
        try {
            dos = new DataOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            String fName = "d:/test2/test.jpg";
            dos.writeUTF(fName);

            File f = new File(fName);
            fis = new FileInputStream(f);
            bis = new BufferedInputStream(fis);

            int len;
            int size = 4096;
            byte[] data = new byte[size];
            while ((len = bis.read(data)) != -1) {
                dos.write(data, 0, len);
            }

            dos.flush();
            dos.close();
            bis.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

最后,出现如下错误。

CRLF not found before max message length: 2048 

我认为我必须忽略序列化器/反序列化器以进行两侧之间的通信。

【问题讨论】:

    标签: tcp spring-integration tcp-ip


    【解决方案1】:

    由于您不在客户端使用 Spring Integration IP,因此您应该手动处理 the end of message

    从另一面看,您的CustomSerializerDeserializerByteArrayCrLfSerializer 的扩展,因为消息CRLF not found before max message length: 正是来自那里。

    所以,第一个:您应该为CustomSerializerDeserializer 增加maxMessageSize,第二个-您必须在客户端上的write 末尾提供\r\n 字节。或者只是在那里使用ByteArrayCrLfSerializer.serialize

    【讨论】:

    • 感谢您的回答。这对我来说很棒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 2017-06-09
    • 2017-03-07
    相关资源
    最近更新 更多