Как я могу отправить сигнал EOF на сервер, когда я использую Spring интеграции в качестве клиента TCP? - PullRequest
0 голосов
/ 26 декабря 2018

Я хочу использовать пружинную интеграцию для замены клиента сокета. Мой код клиента сокета выглядит так:

public static void main(String[] args) {

    try {
        Socket socket = new Socket("localhost", 7779);
        OutputStream os = socket.getOutputStream();
        PrintWriter pw = new PrintWriter(os);
        String str = "hello server!";
        pw.write(str);
        pw.flush();
        socket.shutdownOutput();
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String info = br.readLine();
        while (info != null) {
            System.out.println("i am client. server says that " + info);
            info = br.readLine();
            pw.close();
        }
        br.close();
        is.close();
        pw.close();
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

клиент сокета получит ответ сервера msg.и я использую весеннюю интеграцию, чтобы сделать ту же работу.XML-код весенней интеграции выглядит следующим образом:

    <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="localhost"
                               port="7779"
                               single-use="true"
                               so-timeout="10000"/>

<int:channel id="input"/>

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

<int:object-to-string-transformer id="clientBytes2String"
                                  input-channel="clientBytes2StringChannel"/>

это часть примера tcp-клиент-сервер весенней интеграции https://github.com/spring-projects/spring-integration-samples/tree/master/basic/tcp-client-server

Java-код выглядит так:

    final Scanner scanner = new Scanner(System.in);
    final GenericXmlApplicationContext context = Main.setupContext();
    final SimpleGateway gateway = context.getBean(SimpleGateway.class);
    final AbstractServerConnectionFactory crLfServer = context.getBean(AbstractServerConnectionFactory.class);

    TestingUtilities.waitListening(crLfServer, 10000L);

    while (true) {

        final String input = scanner.nextLine();

        if ("q".equals(input.trim())) {
            break;
        }
        else {
            final String result = gateway.send(input);
            System.out.println(result);
        }
    }

    System.out.println("Exiting application...bye.");
    System.exit(0);

}

public static GenericXmlApplicationContext setupContext() {
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    if (System.getProperty(AVAILABLE_SERVER_SOCKET) == null) {
        System.out.print("Detect open server socket...");
        int availableServerSocket = SocketUtils.findAvailableTcpPort(5678);

        final Map<String, Object> sockets = new HashMap<>();
        sockets.put(AVAILABLE_SERVER_SOCKET, availableServerSocket);

        final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

        context.getEnvironment().getPropertySources().addLast(propertySource);
    }

    System.out.println("using port " + context.getEnvironment().getProperty(AVAILABLE_SERVER_SOCKET));

    context.load("classpath:META-INF/spring/integration/tcpClientServerDemo-context.xml");
    context.registerShutdownHook();
    context.refresh();

    return context;
}

и я не могу получить ответ, потому что сервер не получает сигнал eof, и он все еще заблокирован в readLine ();я получаю исключение: org.springframework.integration.MessageTimeoutException: время ожидания ответа истекло.

Вот код сервера:

try {
        ServerSocket serverSocket = new ServerSocket(7779);
        Socket socket = serverSocket.accept();
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String info = br.readLine();
        System.out.println("from client : "+info);
        while (info != null) {
            System.out.println("i am server. message from client is " + info);
            info = br.readLine(); //server blocked here
        }
        socket.shutdownInput();
        OutputStream os = socket.getOutputStream();
        String replyMsg="welcom from server 7779 ACK\r\n";
        os.write(replyMsg.getBytes());
        os.close();
        br.close();
        isr.close();
        is.close();
        socket.close();
        serverSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

так как я могу отправить eof singal на сервер такмое приложение получит ответ от сервера?

1 Ответ

0 голосов
/ 24 апреля 2019

Spring Integration в настоящее время не поддерживает эти shutDown*() методы.

Пожалуйста, откройте GitHub Issue , и мы посмотрим.

...