Сейчас я некоторое время пытаюсь начать работу с Spring Integration, но, к сожалению, не могу заставить его работать.
Я хотел бы, чтобы сервер прослушивал порт TCP и распечатывал данные, отправленные ему с клиента.Мой клиент - это еще один инструмент командной строки, но из-за того, что я не смог заставить его работать, я использую этот фиктивный клиент для отправки сообщения.
Пока что я рассмотрел два примера и потерял, какой из них на самом деле следовать:
- Сообщение в блоге о подключении к торговому автомату TCP
- Официальный пример на основе аннотаций TcpClientServerAnnotationDemoTest.java , который используется здесь.
@EnableIntegration
@IntegrationComponentScan
@Configuration
public class Config {
@MessagingGateway(defaultRequestChannel = "toTcp")
public interface Gateway {
String viaTcp(String in);
}
@Bean
public TcpInboundGateway tcpInGate(AbstractServerConnectionFactory connectionFactory) {
TcpInboundGateway inGate = new TcpInboundGateway();
inGate.setConnectionFactory(connectionFactory);
inGate.setRequestChannel(fromTcp());
return inGate;
}
@Bean
public MessageChannel fromTcp() {
return new DirectChannel();
}
@MessageEndpoint
public static class Echo {
@Transformer(inputChannel = "fromTcp", outputChannel = "toEcho")
public String convert(byte[] bytes) {
return new String(bytes);
}
@ServiceActivator(inputChannel = "toEcho")
public String upCase(String in) {
return in.toUpperCase();
}
@Transformer(inputChannel = "resultToString")
public String convertResult(byte[] bytes) {
return new String(bytes);
}
}
@Bean
public AbstractServerConnectionFactory serverCF() {
return new TcpNetServerConnectionFactory(8000);
}
}
и вот мой фиктивный клиент отправляет сообщение.
String host = "localhost";
int port = 8000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String myMessage = "THIS IS MY MESSAGE!";
String sendMessage = myMessage + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+ sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " + message);
Он успешно создает TCP-соединение!Но где я могу увидеть сообщение?Сначала я думал, что могу напечатать все, что проходит через @Transformer
или @ServiceActivator
, но это не сработало.
2019-03-06 15:46:12.023 DEBUG 22941 --- [pool-2-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : Accepted connection from 127.0.0.1:41178
2019-03-06 15:46:12.025 DEBUG 22941 --- [pool-2-thread-1] o.s.i.i.tcp.connection.TcpNetConnection : New connection localhost:41178:8000:fdaf998f-7045-43e6-aa4a-2e4ecf6b1d9d
2019-03-06 15:46:12.025 DEBUG 22941 --- [pool-2-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : serverCF: Added new connection: localhost:41178:8000:fdaf998f-7045-43e6-aa4a-2e4ecf6b1d9d
2019-03-06 15:46:12.025 TRACE 22941 --- [pool-2-thread-1] .s.i.i.t.c.TcpNetServerConnectionFactory : serverCF: Connection is open: localhost:41178:8000:fdaf998f-7045-43e6-aa4a-2e4ecf6b1d9d
2019-03-06 15:46:12.025 DEBUG 22941 --- [pool-2-thread-2] o.s.i.i.tcp.connection.TcpNetConnection : localhost:41178:8000:fdaf998f-7045-43e6-aa4a-2e4ecf6b1d9d Reading...
2019-03-06 15:46:12.025 DEBUG 22941 --- [pool-2-thread-2] o.s.i.i.t.s.ByteArrayCrLfSerializer : Available to read: 20
2019-03-06 15:46:12.026 TRACE 22941 --- [pool-2-thread-1] o.s.i.i.tcp.connection.TcpNetConnection : Published: TcpConnectionOpenEvent [source=TcpNetConnection:localhost:41178:8000:fdaf998f-7045-43e6-aa4a-2e4ecf6b1d9d], [factory=serverCF, connectionId=localhost:41178:8000:fdaf998f-7045-43e6-aa4a-2e4ecf6b1d9d] **OPENED**
Когда я использую инструмент командной строки для реального клиента, соединение также устанавливается, но любые последующие сообщения отправляются throw SocketTimeoutException
.
Я ценю любую помощь,а также любые предложения для учебника по Spring Integration для TCP с использованием аннотаций!Спасибо!