Spring集成TCP服务器打印传入消息

我现在正在尝试一段时间来开始使用 Spring Integration,但不幸的是无法让它工作。

我想让一个服务器监听 TCP 端口并打印出从客户端发送给它的数据。我的客户端是另一个命令行工具,但因为我无法让它工作,我正在使用这个虚拟客户端发送消息。

在这里我的虚拟客户发送消息。


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);


当我使用命令行工具的实际客户端时,连接也会建立,但任何后续消息都会发送 throw SocketTimeoutException。


我感谢任何帮助,以及对使用注释的 TCP 的 Spring 集成教程的任何建议!谢谢!


江户川乱折腾
浏览 126回答 1
1回答

有只小跳蛙

要从客户端打印数据,只需WireTap在fromTcportoEcho通道上放置一个,然后将其接入其他通道进行打印即可。通常LoggingHandler,拥有该窃听频道的订阅者就足够了。您可以在参考手册中查看更多信息:https ://docs.spring.io/spring-integration/docs/current/reference/html/#channel-interceptors
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java