支持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代码: 代码
.
.
.