我正在寻找一个示例,通过不使用xml(spring-integration)的快速引导连接TCP。
我从How to create a Tcp Connection in spring boot to accept connections? URL中得到了以下片段。
在本例中,仅使用主方法就足以连接tcp。为什么其他豆类和变压器在这里声明?
这样做不对吗?我不使用简单的Java客户端来接受响应,而是与Spring集成。但是没有使用Java的适当示例。
你能帮忙吗?
package com.example;
import java.net.Socket;
import javax.net.SocketFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
import org.springframework.integration.transformer.ObjectToStringTransformer;
import org.springframework.messaging.MessageChannel;
@SpringBootApplication
public class So39290834Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(So39290834Application.class, args);
Socket socket = SocketFactory.getDefault().createSocket("localhost", 9999);
socket.getOutputStream().write("foo\r\n".getBytes());
socket.close();
Thread.sleep(1000);
context.close();
}
@Bean
public TcpNetServerConnectionFactory cf() {
return new TcpNetServerConnectionFactory(9999);
}
@Bean
public TcpReceivingChannelAdapter inbound(AbstractServerConnectionFactory cf) {
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(cf);
adapter.setOutputChannel(tcpIn());
return adapter;
}
@Bean
public MessageChannel tcpIn() {
return new DirectChannel();
}
@Transformer(inputChannel = "tcpIn", outputChannel = "serviceChannel")
@Bean
public ObjectToStringTransformer transformer() {
return new ObjectToStringTransformer();
}
@ServiceActivator(inputChannel = "serviceChannel")
public void service(String in) {
System.out.println(in);
}
}发布于 2019-01-06 01:00:58
这个应用程序既是客户端,也是服务器。
这个问题是关于如何使用Spring编写服务器端(接受连接)。
main()方法只是一个连接到服务器端的测试。它使用标准的Java套接字API;它也可以编写成在客户端使用组件。
顺便说一句,您不必使用XML来编写应用程序,您可以用注释来配置它,或者使用Java。读读文件。
编辑
使用Java 的客户机/服务器示例
@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);
}
}结果
FOO
BARhttps://stackoverflow.com/questions/54057281
复制相似问题