EventBus-----spring Guava

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

spring 配置 异步消息总线

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

EventBus-----spring Guava_第1张图片
pom.xml




    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.10
         
    

    com.example
    event_bus
    0.0.1-SNAPSHOT
    event_bus
    event_bus

    
        1.8
    

    

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            com.google.guava
            guava
            31.1-jre
        

        
            org.jetbrains
            annotations
            20.1.0
            compile
        

        

        
            org.slf4j
            slf4j-api
            1.7.25
        

        
        
            org.projectlombok
            lombok
            1.18.26
            provided
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



事件注入配置

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

发起请求:
EventBus-----spring Guava_第2张图片

输出:

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

你可能感兴趣的:(java,中间件,java)