Reputation: 160
I am trying now for a while to get started with Spring Integration, but unfortunately can't get it to work.
I would like to have a server listening on a TCP port and printing out the data sent to it from the client. My client is another command line tool, but because I couldn't get it to work I am using this dummy client to send a message.
So far I looked into two examples and am lost which one to follow actually:
@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);
}
}
and here my dummy client sending a message.
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);
It does successfully create a TCP connection! But, where can I see the message? I was initially thinking that I could just print whatever passes through the @Transformer
or @ServiceActivator
but that didn't work.
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**
When I am using the actual client the command line tool, the connection is established as well, but any follow up messages send throw SocketTimeoutException
.
I appreciate any help, as well as any suggestion for a tutorial on Spring Integration for TCP using annotations! Thank you!
Upvotes: 1
Views: 1154
Reputation: 121177
To print the data from the client there is just enough to have a WireTap
on the fromTcp
or toEcho
channel and tap it into some other channel for printing. Typically the LoggingHandler
is enough to have as a subscriber to that wire-tapping channel.
You can see more info in the Reference Manual: https://docs.spring.io/spring-integration/docs/current/reference/html/#channel-interceptors
Upvotes: 2