TCP-клиент отправляет сообщение на внешний сервер, ответное сообщение не ожидается - PullRequest
0 голосов
/ 15 февраля 2019

Я настраиваю TCP-клиента с помощью Spring Integration, отправляю сообщение со строкой в ​​качестве полезной нагрузки, возврат не ожидается.Возможно, сериализатор / десериализатор работает неправильно?Извините, я изучаю интеграцию Spring.

Я могу подключиться к внешнему TCP-серверу с помощью oepnssl:

---
# DC API Test System: microstrategy
sessions.list.
.
response
,status_code,1
,status_message,Unrecognised operation
,time,2019-02-15 07:08:08 (+1000)
.

Команда, которую мне нужно отправить, - "sessions.list \ n. \ N".

Теперь я создал tcp-клиент, пытающийся подключиться к серверу:

spring-context.xml

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip-5.1.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-5.1.xsd">

<bean id="integrationConversionService"
          class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.microstrategy.example.ByteArrayToStringConverter"/>
            </list>
        </property>
</bean> 


<bean id="customeSerilizerDeserlizer" class="com.microstrategy.example.CustomSerializerDeserializer" />

<int:gateway service-interface="com.microstrategy.example.SimpleGateway"
    default-request-channel="output"
    default-reply-channel="reply"/>

<int:channel id="output"/>
<int:channel id="reply" datatype="java.lang.String"/>

<int-ip:tcp-connection-factory
    id="clientFactory"
    type="client"
    host="server"
    port="15099"
    serializer="customeSerilizerDeserlizer"
    single-use="true"
    so-timeout="10000"/>

<int-ip:tcp-outbound-gateway 
    request-channel="output"
    reply-channel="reply"
    connection-factory="clientFactory"
    request-timeout="10000"
    reply-timeout="10000"/>

</beans>

Итак, следуя этому репострока должна быть преобразована в byte [].

Я использую точно такой же конвертер, что и репо, поэтому я просто копирую сюда, чтобы сэкономить ваше время:

import java.io.UnsupportedEncodingException;

import org.springframework.core.convert.converter.Converter;

public class ByteArrayToStringConverter implements Converter<byte[], String> {

    private String charSet = "UTF-8";

    public String convert(byte[] bytes) {
        try {
            return new String(bytes, this.charSet);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("caught excepton in converter");
            return new String(bytes);
        }
    }

    /**
     * @return the charSet
     */
    public String getCharSet() {
        return charSet;
    }

    /**
     * @param charSet the charSet to set
     */
    public void setCharSet(String charSet) {
        this.charSet = charSet;
    }

}
public interface SimpleGateway {
    public String send(Message message);
}

Я создал собственный сериализатор:

package com.microstrategy.example;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer;

public class CustomSerializerDeserializer extends AbstractByteArraySerializer {
    @Override
    public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
        outputStream.write(bytes); 
    }

    @Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub
        return null;
    }

}

Моя основная функция:

Message<String> message = MessageBuilder.withPayload("sessions.list").build();
String replyMessage = simpleGateway.send(message);
Message<String> message2 = MessageBuilder.withPayload(".").build();
String replyMessage2 = simpleGateway.send(message2);
System.out.println(replyMessage2);

ReplyMessage:

# DC API Test System: microstrategy

Кажется, я успешно подключился к серверу, отправив сообщение, но сообщение не распознается правильносервер.Будем благодарны за любые полезные предложения, спасибо!

Обновление 1:

Я добавляю вывод в сериализатор:

public class CustomSerializerDeserializer extends AbstractByteArraySerializer {
    @Override
    public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
        System.out.println("inside serialize");
        System.out.println(System.currentTimeMillis());


        String string = new String(bytes);
        System.out.println("byte[] in serialize is " + string);
        outputStream.write(bytes); 
    }

    @Override
    public byte[] deserialize(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub
        System.out.println("inside deserialize");
        System.out.println(System.currentTimeMillis());

        return null;
    }

}
inside serialize
1550182834431
byte[] in serialize is sessions.list
.

# DC API Test System: microstrategy
2019-02-14 17:21:35.185  INFO 91620 --- [       Thread-1] o.s.i.endpoint.EventDrive

Вывод показывает, что байт [] кажется правильным, тогда почему сервер не возвращается как ожидалось?

Обновление 2: я изменил основную функцию (была обновлена), потому что фреймворк будет добавлять «\ n» в конце каждого сообщения.Это правильно?

Вывод

inside serialize
1550184564485
byte[] in serialize is sessions.list
inside serialize
1550184565003
byte[] in serialize is .
2019-02-14 17:49:35.013 ERROR 91740 --- [           main] o.s.i.ip.tcp.TcpOutboundGateway          : Tcp Gateway exception

org.springframework.integration.MessageTimeoutException: Timed out waiting for response

Нет ответа?

Обновление 3: я могу открыть соединение, отправив пустое сообщение.Но почему другое сообщение не работает?

Message<String> message = MessageBuilder.withPayload("").build();
String replyMessage = simpleGateway.send(message);
System.out.println(replyMessage);

Любая помощь спасибо?

Это мое обновление, прежде чем я решил проблему, которая удалена администратором:

Я получил ответ от сервера сейчас, ноон приходит с ошибкой:

Cannot correlate response - no pending reply for server:15099:49469:0fdce5c4-432f-4ce4-b878-2e08d0e96419
inside serialize
1550189909340
byte[] in serialize is sessions.list
.

GenericMessage [payload=byte[35], headers={ip_tcp_remotePort=15099, ip_connectionId=server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839, ip_localInetAddress=/10.21.66.115, ip_address=217.78.6.17, id=3a6ff696-f12f-6328-da1a-5d613d37a4b2, ip_hostname=server, timestamp=1550189909764}]
2019-02-14 19:18:29.850 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.851 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.851 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.851 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.852 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway          : Cannot correlate response - no pending reply for server:15099:49550:a3bc44fa-7d36-483c-a1b8-f91eea62d839
2019-02-14 19:18:29.852 ERROR 92282 --- [pool-1-thread-1] o.s.i.ip.tcp.TcpOutboundGateway  

Основная функция

message = new GenericMessage<String>("sessions.list\n.\n");
replyMessage = simpleGateway.send(message);
System.out.println(replyMessage);

Я пытался удалить последний "\ n"

message = new GenericMessage<String>("sessions.list\n.");

Не работаетполучил исключение тайм-аута.Как я могу удалить эти ошибки «Невозможно сопоставить ответ»?

Обновление 1:

Я думаю, что сервер отвечает несколькими строками сообщения:

sessions.list
.
response
,status_code,0
,status_message,OK
,time,2019-02-16 00:10:49 (+1000)
sessions
.

Мне нужноперехватывать все ответы до ".".

Ответы [ 2 ]

0 голосов
/ 17 февраля 2019

Наконец это решено.Я опубликую свое решение для справки других.

Мой TCP-клиент отправляет многострочные команды, оканчивающиеся на «.», И ожидает многострочный ответ, который также оканчивается на «.»с внешнего сервера.Поэтому мне нужно написать собственный сериализатор и десериализатор.Сериализатор / десериализатор CRLF по умолчанию не соответствует моему случаю.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import org.springframework.core.serializer.Deserializer;
import org.springframework.core.serializer.Serializer;

public class CustomSerializerDeserializer implements Serializer<String>, Deserializer<String> {

    @Override
    public String deserialize(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub
        StringBuilder builder = new StringBuilder();
        int c;
        while (true) {
            c = inputStream.read();
            builder.append((char)c);
            if ((char)c == '.') {
                break;
            }
        }

        return builder.toString();
    }

    @Override
    public void serialize(String object, OutputStream outputStream) throws IOException {
        // TODO Auto-generated method stub
        outputStream.write(object.getBytes());
        outputStream.flush();   
    }
}

Конфигурация xml:

<int-ip:tcp-connection-factory
    id="clientFactory"
    type="client"
    host="server"
    port="15099"
    ssl-context-support="sslContext"
    serializer="customeSerilizerDeserlizer"
    deserializer="customeSerilizerDeserlizer"
    single-use="true"
    so-timeout="10000"/>
0 голосов
/ 15 февраля 2019

Если вы не ожидаете ответа, вы должны использовать адаптер шлюза исходящего канала вместо шлюза.

...