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接收数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot2.x漏洞将logback1.2.x 升级至1.3.x

    SpringBoot2.x漏洞将logback1.2.x 升级至1.3.x

    安全部门在代码漏洞扫描中发现logback 1.2.x版本存在CVE漏洞,建议升级至1.3.x版本,本文就来介绍了logback1.2.x 升级至1.3.x,具有一定的参考价值,感兴趣的可以了解一下
    2024-09-09
  • 阿里云部署SpringBoot项目启动后被杀进程的问题解析

    阿里云部署SpringBoot项目启动后被杀进程的问题解析

    这篇文章主要介绍了阿里云部署SpringBoot项目启动后被杀进程的问题,本文给大家分享问题原因所在及解决步骤,需要的朋友可以参考下
    2023-09-09
  • Spring Validation方法实现原理分析

    Spring Validation方法实现原理分析

    这篇文章主要介绍了Spring Validation实现原理分析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • python 与HFSS联合仿真的教程讲解

    python 与HFSS联合仿真的教程讲解

    这篇文章主要介绍了python 与HFSS联合仿真的教程讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Spring ApplicationListener监听器用法详解

    Spring ApplicationListener监听器用法详解

    这篇文章主要介绍了Spring ApplicationListener监听器用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • java实现双层圣诞树加修饰代码示例

    java实现双层圣诞树加修饰代码示例

    大家好,本篇文章主要讲的是java实现双层圣诞树加修饰代码示例,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • Intellij IDEA 中调试 maven 插件的步骤

    Intellij IDEA 中调试 maven 插件的步骤

    这篇文章主要介绍了Intellij IDEA 中调试 maven 插件,本文分步骤给大家讲解的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • Java基于Rest Assured自动化测试接口详解

    Java基于Rest Assured自动化测试接口详解

    Rest Assured 是一个基于 Java 的流行的用于测试 RESTful API 的库。这篇文章主要介绍了Java如何基于Rest Assured实现自动化测试接口,需要的可以参考一下
    2023-03-03
  • SpringBoot中自定义注解实现控制器访问次数限制实例

    SpringBoot中自定义注解实现控制器访问次数限制实例

    本篇文章主要介绍了SpringBoot中自定义注解实现控制器访问次数限制实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04
  • 使用concurrentHashMap如何实现缓存

    使用concurrentHashMap如何实现缓存

    文章介绍了使用ConcurrentHashMap实现缓存的线程安全性和初始化方法,以及如何处理高并发场景下的缓存清理和数据一致性问题,包括分桶、使用ConcurrentLinkedQueue以及使用CountDownLatch来确保缓存数据的不丢失
    2025-02-02

最新评论