This application is both the client and the server.
That question was specifically about how to write the server side (accept the connection), using Spring Integration.
The main()
method is simply a test that connects to the server side. It uses standard Java sockets APIs; it could also have been written to use Spring Integration components on the client side.
BTW, you don't have to use XML to write a Spring Integration application, you can configure it with annotations, or use the Java DSL. Read the documentation.
EDIT
Client/Server Example using Java DSL
@SpringBootApplication
public class So54057281Application {
public static void main(String[] args) {
SpringApplication.run(So54057281Application.class, args);
}
@Bean
public IntegrationFlow server() {
return IntegrationFlows.from(Tcp.inboundGateway(
Tcp.netServer(1234)
.serializer(codec()) // default is CRLF
.deserializer(codec()))) // default is CRLF
.transform(Transformers.objectToString()) // byte[] -> String
.<String, String>transform(p -> p.toUpperCase())
.get();
}
@Bean
public IntegrationFlow client() {
return IntegrationFlows.from(MyGateway.class)
.handle(Tcp.outboundGateway(
Tcp.netClient("localhost", 1234)
.serializer(codec()) // default is CRLF
.deserializer(codec()))) // default is CRLF
.transform(Transformers.objectToString()) // byte[] -> String
.get();
}
@Bean
public AbstractByteArraySerializer codec() {
return TcpCodecs.lf();
}
@Bean
@DependsOn("client")
ApplicationRunner runner(MyGateway gateway) {
return args -> {
System.out.println(gateway.exchange("foo"));
System.out.println(gateway.exchange("bar"));
};
}
public interface MyGateway {
String exchange(String out);
}
}
result
FOO
BAR
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…