【问题标题】:Spring integration TCP server print incoming messagesSpring集成TCP服务器打印传入消息
【发布时间】:2019-03-06 15:03:44
【问题描述】:

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

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

到目前为止,我查看了两个示例,但实际上不知道应该遵循哪个示例:

  1. Blog post on TCP vending machine connection
  2. The official annotation based example 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

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

【问题讨论】:

    标签: java tcp spring-integration


    【解决方案1】:

    要从客户端打印数据,只需在fromTcptoEcho 通道上设置一个WireTap,然后将其接入其他通道进行打印即可。通常LoggingHandler 足以成为该窃听频道的订阅者。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-03
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      相关资源
      最近更新 更多