[英]Spring Boot TCP Client
我正在尋找通過不帶xml(spring-integration)的sping引導連接TCP的示例。
我從如何在Spring Boot中創建Tcp連接以接受連接獲得以下代碼段? 網址。
在此示例中,僅main方法足以連接tcp。 為什么在這里聲明其他bean和變壓器?
這是錯的嗎? 我不想使用簡單的Java套接字客戶端來接受響應,而是要與Spring集成。 但是沒有使用Java DSL的合適示例。
能否請你幫忙?
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);
}
}
該應用程序既是客戶端又是服務器。
這個問題專門涉及如何使用Spring Integration編寫服務器端(接受連接)。
main()
方法只是連接到服務器端的測試。 它使用標准的Java套接字API; 它也可能被編寫為在客戶端使用Spring Integration組件。
順便說一句,您不必使用XML來編寫Spring Integration應用程序,您可以使用注釋對其進行配置,也可以使用Java DSL。 閱讀文檔。
編輯
使用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);
}
}
結果
FOO
BAR
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.