栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Event事件发布&消费Demo

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Event事件发布&消费Demo

Spring Event事件发布&消费Demo

demo基于springboot maven项目

spring event默认是同步执行的,因此后面示范了如何异步执行事件的监听函数,以及配置自己的异步线程池

pom.xml
		
        
            org.projectlombok
            lombok
            true
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
实现自己的事件
package com.example.demo.event;

import lombok.Getter;
import lombok.ToString;
import org.springframework.context.ApplicationEvent;


@Getter
@ToString
public class NoticeEvent extends ApplicationEvent {
    
    private String title;
    
    private String content;



    public NoticeEvent(String title,String content) {
        super(title);
        this.title = title;
        this.content = content;
    }


}

定义事件监听器
package com.example.demo.listener;

import com.example.demo.event.NoticeEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;


@Component
public class NoticeEventListener implements ApplicationListener {

    @Async
    @Override
    public void onApplicationEvent(NoticeEvent noticeEvent) {
        System.out.println(Thread.currentThread().getName() + "线程处理事件");
        System.out.println(noticeEvent);
    }
}
定义事件发布接口
package com.example.demo.controller;

import com.example.demo.event.NoticeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class EventPublisherController {

    
    @Autowired
    private ApplicationEventPublisher eventPublisher;


    @RequestMapping("/publish")
    public String publish(String title,String content){
        NoticeEvent event = new NoticeEvent(title,content);
        System.out.println("接口收到请求,内容如下:");
        System.out.println(event);
        eventPublisher.publishEvent(event);
        String ret = "事件发布成功";
        System.out.println(ret);
        return ret;
    }
}

走异步线程池执行监听函数
  • 给启动类加@EnableAsync注解
  • 给消费函数加@Async注解
定义自己的spring异步线程池
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;


@Configuration
public class AsyncConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(16);
        executor.setQueueCapacity(Integer.MAX_VALUE);
        executor.setThreadNamePrefix("async-thread-");
        executor.initialize();
        return executor;
    }
}

发送请求

http://localhost/publish?title=放假&content=明天放假

执行结果

接口收到请求,内容如下:
NoticeEvent(title=放假, content=明天放假)
2021-10-25 15:30:04.414  INFO 10088 --- [p-nio-80-exec-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService
事件发布成功
async-thread-1线程处理事件
NoticeEvent(title=放假, content=明天放假)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/349042.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号