EventBus-----spring Guava

文章介绍了如何在SpringBoot2.7.10项目中配置和使用AsyncEventBus进行异步消息处理。通过配置事件注入,创建单例的AsyncEventBus,并利用ThreadPoolTaskExecutor设置线程池。服务层通过@Subscribe注解监听事件,控制器层通过eventBus.post发送消息。示例展示了从发送到接收的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

支持spring4.x以上及springboot)使AsyncEventBus交给spring容器管理

spring 配置 异步消息总线

1.configable:单例初始化eventBus-->AsyncEventBus 异步消息总线
2.controller: eventBus.post 发送异步消息
3.service:  Subscribe 作为listener 监听,register(this) 构造方法注册监听

在这里插入图片描述
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.10</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>event_bus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>event_bus</name>
    <description>event_bus</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>

        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>20.1.0</version>
            <scope>compile</scope>
        </dependency>

        <!-- SpringBoot会默认使用logback作为日志框架,在生成springboot项目的时候可以直接勾选logback:   slf4j+logback-->

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

事件注入配置

package com.example.eventbus.google.spring.sample.configable;

import com.google.common.eventbus.AsyncEventBus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @className: AsyncEventBusConfig
 * @author: czh
 * @date: 2023/4/22
 * @description: 事件注入配置
 * @version: 1.0.0
 **/
@Configuration
public class AsyncEventBusConfig {
    @Bean
    @Scope("singleton")
    public AsyncEventBus asyncEventBus() {
        final ThreadPoolTaskExecutor executor = executor();
        return new AsyncEventBus(executor);
    }

    @Bean
    public ThreadPoolTaskExecutor executor(){
        /*
        org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
        private int corePoolSize = 1;
        private int maxPoolSize = 2147483647;
        private int queueCapacity = 2147483647;
        private int keepAliveSeconds = 60;
        private boolean allowCoreThreadTimeOut = false;
        * */
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(1000);
        // executor.setKeepAliveSeconds(600);
        // executor.setAllowCoreThreadTimeOut(true);
        return executor;
    }
}

需要异步执行的类中注册该类,并给异步执行的方法上加@Subscribe注解

package com.example.eventbus.google.spring.sample.service;

/**
 * @className: BusListenerServiceI
 * @author: czh
 * @date: 2023/4/22
 * @Description: 需要异步执行的类中注册该类,并给异步执行的方法上加@Subscribe注解
 * @version: 1.0.0
 **/
public interface BusListenerServiceI {
}

需要异步执行的类中注册该类,并给异步执行的方法上加@Subscribe注解

package com.example.eventbus.google.spring.sample.service.impl;

import com.example.eventbus.google.spring.sample.service.BusListenerServiceI;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @className: BusListenerServiceImpl
 * @author: czh
 * @date: 2023/4/22
 * @Description: 需要异步执行的类中注册该类,并给异步执行的方法上加@Subscribe注解
 * @version: 1.0.0
 **/
@Service("busListenerService")
@Slf4j
public class BusListenerServiceImpl implements BusListenerServiceI {

    @Autowired
    private AsyncEventBus asyncEventBus;

    @PostConstruct // 注册该类
    public void register(){
        log.info("singleton async event bus register listener instance on spring service PostConstruct.................");
        asyncEventBus.register(this);
    }

    @AllowConcurrentEvents//线程安全
    @Subscribe // 异步执行的方法标识:需要传入String类型参数 : 消费 event信息
    public void onMessage(String message){
        log.info("listener Subscribe received message: {}",message);
    }
}

调用方法的类

package com.example.eventbus.google.spring.sample.controller;

import com.google.common.eventbus.AsyncEventBus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @className: GuavaEventBusController
 * @author: czh
 * @date: 2023/4/22
 * @description: 调用方法的类
 * @version: 1.0.0
 **/
@RestController
@RequestMapping
@Slf4j
public class GuavaEventBusController {
    @Autowired
    private AsyncEventBus eventBus;

    @GetMapping("/eventbus")
    public String eventbus(){
        log.info("event bus controller enter........");
        log.info("event bus send event message");
        eventBus.post("异步消息发送: event message,controller send"); // 调用执行方法的参数
        log.info("event bus controller send success........");
        return "send success......................................";
    }

}

application.properties 配置

server.port= 7007
spring.application.name=g_event_bus
#file属性可有可无
#logging.file=applog/sys.log
logging.level.com.example=debug

发起请求:
在这里插入图片描述

输出:

18:39:34 [http-nio-7007-exec-1] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
18:39:34 [http-nio-7007-exec-1] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 2 ms
18:39:34 [http-nio-7007-exec-1] INFO  c.e.e.g.s.s.c.GuavaEventBusController - event bus controller enter........
18:39:34 [http-nio-7007-exec-1] INFO  c.e.e.g.s.s.c.GuavaEventBusController - event bus send event message
18:39:34 [http-nio-7007-exec-1] INFO  c.e.e.g.s.s.c.GuavaEventBusController - event bus controller send success........
18:39:34 [executor-1] INFO  c.e.e.g.s.s.s.i.BusListenerServiceImpl - listener Subscribe received message: 异步消息发送: event message,controller send

.
.
.
github代码: 代码
.
.
.

内容概要:本文档详细介绍了基于MATLAB实现的无人机三维路径规划项目,核心算法采用蒙特卡罗树搜索(MCTS)。项目旨在解决无人机在复杂三维环境中自主路径规划的问题,通过MCTS的随机模拟与渐进式搜索机制,实现高效、智能化的路径规划。项目不仅考虑静态环境建模,还集成了障碍物检测与避障机制,确保无人机飞行的安全性和效率。文档涵盖了从环境准备、数据处理、算法设计与实现、模型训练与预测、性能评估到GUI界面设计的完整流程,并提供了详细的代码示例。此外,项目采用模块化设计,支持多无人机协同路径规划、动态环境实时路径重规划等未来改进方向。 适合人群:具备一定编程基础,特别是熟悉MATLAB和无人机技术的研发人员;从事无人机路径规划、智能导航系统开发的工程师;对MCTS算法感兴趣的算法研究人员。 使用场景及目标:①理解MCTS算法在三维路径规划中的应用;②掌握基于MATLAB的无人机路径规划项目开发全流程;③习如何通过MCTS算法优化无人机在复杂环境中的飞行路径,提高飞行安全性和效率;④为后续多无人机协同规划、动态环境实时调整等高级应用打下基础。 其他说明:项目不仅提供了详细的理论解释和技术实现,还特别关注了实际应用中的挑战和解决方案。例如,通过多阶段优化与迭代增强机制提升路径质量,结合环境建模与障碍物感知保障路径安全,利用GPU加速推理提升计算效率等。此外,项目还强调了代码模块化与调试便利性,便于后续功能扩展和性能优化。项目未来改进方向包括引入深度强化习辅助路径规划、扩展至多无人机协同路径规划、增强动态环境实时路径重规划能力等,展示了广阔的应用前景和发展潜力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值