首页 > 解决方案 > Spring集成TCP服务器打印传入消息

问题描述

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

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

到目前为止,我查看了两个示例,但实际上迷失了哪个示例:

  1. 关于 TCP 自动售货机连接的博文
  2. 基于官方注解的示例 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 连接!但是,我在哪里可以看到消息?我最初以为我可以打印任何通过@Transformeror@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

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

标签: javatcpspring-integration

解决方案


要从客户端打印数据,只需WireTapfromTcportoEcho通道上有一个,然后将其接入其他通道进行打印即可。通常LoggingHandler,拥有该窃听频道的订阅者就足够了。

您可以在参考手册中查看更多信息:https ://docs.spring.io/spring-integration/docs/current/reference/html/#channel-interceptors


推荐阅读