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

时间:2019-03-06 15:03:44

标签: java tcp spring-integration

我现在尝试了一段时间以开始使用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 Integration教程提供任何建议!谢谢!

1 个答案:

答案 0 :(得分:1)

要打印来自客户端的数据,只需要在SubjectSubject通道上有一个@Test public void test() { Subject<String> subject = MyDesiredSubject.create(); subject.onNext("1"); subject.onNext("2"); TestObserver<String> testObserver = subject.test(); testObserver.assertValues("1", "2"); testObserver.dispose(); subject.onNext("3"); subject.onNext("4"); testObserver = subject.test(); testObserver.assertValues("3", "4"); } 并将其点击到其他通道进行打印即可。通常,WireTap足以作为该窃听频道的订户。

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