如何在spring集成中忽略序列化器/解串器?

时间:2015-02-17 05:01:42

标签: tcp spring-integration tcp-ip

我想使用spring integration 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 

我想我必须忽略序列化器/解串器以进行双方之间的通信。

1 个答案:

答案 0 :(得分:1)

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

从另一侧看,CustomSerializerDeserializer是[{1}}的扩展名,因为消息ByteArrayCrLfSerializer正好来自那里。

所以,第一个:你应该增加CRLF not found before max message length:的{​​{1}}和第二个 - 你必须在客户端的maxMessageSize末尾提供CustomSerializerDeserializer个字节。或者只在那里使用\r\n