Spring Boot TCP客户端

编程入门 行业动态 更新时间:2024-10-22 11:02:38
本文介绍了Spring Boot TCP客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一个通过不带xml(spring-integration)的sping boot连接TCP的示例.

I'm looking for an example to connect TCP through sping boot without xml(spring-integration).

我从如何在Spring Boot中创建Tcp连接以接受连接? URL.

在此示例中,仅main方法足以连接tcp.为什么在这里声明其他bean和转换器?

in this example, just main method alone enough to connect tcp. why other beans and the transformer are declared here?

错了吗?我不想使用简单的Java套接字客户端来接受响应,而是要与Spring集成.但是没有使用Java DSL的合适示例.

Is it wrong? Instead of using simple Java socket client to accept the response, I would like to integrate with Spring. But no suitable examples available using Java DSL.

能请你帮忙吗?

package com.example; import java.Socket; import javax.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); } }

推荐答案

此应用程序既是客户端又是服务器.

This application is both the client and the server.

该问题专门涉及如何使用Spring Integration编写服务器端(接受连接).

That question was specifically about how to write the server side (accept the connection), using Spring Integration.

main()方法只是连接到服务器端的测试.它使用标准的Java套接字API;它也可以编写为在客户端使用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.

顺便说一句,您不必使用XML来编写Spring Integration应用程序,您可以使用注释对其进行配置,也可以使用Java DSL.阅读文档.

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.

编辑

使用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( TcpServer(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( TcpClient("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

更多推荐

Spring Boot TCP客户端

本文发布于:2023-11-25 09:08:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   Spring   Boot   TCP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!