spring boot udp或者tcp接收数据的实例详解

 更新时间:2021年12月01日 09:49:04   作者:JakeWin  
这篇文章主要介绍了spring boot udp或者tcp接收数据,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

下面用的是 springboot内置integration依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-ip</artifactId>
        </dependency>

下面是一个类 用来接收udp协议和tcp协议的数据

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.Filter;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
import org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer;
import org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter;
import org.springframework.messaging.Message;

@Configuration
public class DataReceiveConfigration {

    
    @Bean
    public UnicastReceivingChannelAdapter getUnicastReceivingChannelAdapter() {
        UnicastReceivingChannelAdapter adapter = new  UnicastReceivingChannelAdapter(4567);//实例化一个udp 4567端口
        adapter.setOutputChannelName("udp");
        return adapter;
    }
    
    @Transformer(inputChannel="udp",outputChannel="udpString")
    public String transformer(Message<?> message) {
        return new String((byte[])message.getPayload());//把接收的数据转化为字符串
    }
    
    @Filter(inputChannel="udpString",outputChannel="udpFilter")
    public boolean filter(String message) {
        return message.startsWith("abc");//如果接收数据开头不是abc直接过滤掉
    }

   @Router(inputChannel="udpFilter")
    public String routing(String message) {
        if(message.contains("1")) {//当接收数据包含数字1时
            return "udpRoute1";
        }
        else {
            return "udpRoute2";
        }    
    }

    
   @ServiceActivator(inputChannel="udpRoute1")
   public void udpMessageHandle(String message) {
       System.out.println("udp1:" +message);
   }

    @ServiceActivator(inputChannel="udpRoute2")
    public void udpMessageHandle2(String message) {
        System.out.println("udp2:" +message);
    }
    
    @Bean
    public TcpNetServerConnectionFactory getServerConnectionFactory() {
        TcpNetServerConnectionFactory serverConnectionFactory = new TcpNetServerConnectionFactory(1234);
        serverConnectionFactory.setSerializer(new ByteArrayRawSerializer());
        serverConnectionFactory.setDeserializer(new ByteArrayRawSerializer());
        serverConnectionFactory.setLookupHost(false);
        return serverConnectionFactory;
    }

    @Bean
    public TcpReceivingChannelAdapter getReceivingChannelAdapter() {
        TcpReceivingChannelAdapter receivingChannelAdapter = new TcpReceivingChannelAdapter();
        receivingChannelAdapter.setConnectionFactory(getServerConnectionFactory());
        receivingChannelAdapter.setOutputChannelName("tcp");
        return receivingChannelAdapter;
    }

    @ServiceActivator(inputChannel="tcp")
    public void messageHandle(Message<?> message) {
        System.out.println(new String((byte[])message.getPayload()));
    }
}

到此这篇关于spring boot udp或者tcp接收数据的实例详解的文章就介绍到这了,更多相关spring boot接收数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java try catch语句异常处理详解

    Java try catch语句异常处理详解

    这篇文章主要给大家介绍了关于Java try catch语句异常处理的相关资料,Java中的try-catch用于捕获和处理异常,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • Java8中关于Function.identity()的使用

    Java8中关于Function.identity()的使用

    这篇文章主要介绍了Java8中关于Function.identity()的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • java8中:: 用法示例(JDK8双冒号用法)

    java8中:: 用法示例(JDK8双冒号用法)

    这篇文章主要给大家介绍了关于java8 中的:: 用法(JDK8双冒号用法)的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用java8具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • SpringBoot实现登录校验(JWT令牌)

    SpringBoot实现登录校验(JWT令牌)

    JWT全称为JSON Web Token,是一种用于身份验证的开放标准,本文主要介绍了SpringBoot实现登录校验(JWT令牌),具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • Runtime.getRuntime().exec 路径包含空格的解决

    Runtime.getRuntime().exec 路径包含空格的解决

    这篇文章主要介绍了Runtime.getRuntime().exec 路径包含空格的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • SpringSecurity构建基于JWT的登录认证实现

    SpringSecurity构建基于JWT的登录认证实现

    这篇文章主要介绍了SpringSecurity构建基于JWT的登录认证实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • 基于Java SSM框架开发图书借阅系统源代码

    基于Java SSM框架开发图书借阅系统源代码

    本文给大家介绍了基于Java SSM框架开发图书借阅系统,开发环境基于idea2020+mysql数据库,前端框架使用bootstrap4框架,完美了实现图书借阅系统,喜欢的朋友快来体验吧
    2021-05-05
  • Spring中的@Cacheable缓存注解详解

    Spring中的@Cacheable缓存注解详解

    这篇文章主要介绍了Spring中的@Cacheable缓存注解详解,数据库查找的流程是先要从磁盘拿到数据,再刷新到内存,再返回数据。磁盘相比于内存来说,速度是很慢的,为了提升性能,就出现了基于内存的缓存,需要的朋友可以参考下
    2023-05-05
  • SpringBoot Jpa 自定义查询实现代码详解

    SpringBoot Jpa 自定义查询实现代码详解

    这篇文章主要介绍了SpringBoot Jpa 自定义查询实现代码详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Java使用ant.jar执行SQL脚本文件的示例代码

    Java使用ant.jar执行SQL脚本文件的示例代码

    这篇文章主要介绍了Java使用ant.jar执行SQL脚本文件,文中通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-02-02

最新评论