SpringBoot 事件监听ApplicationEventPublisher
GitHub: link. 欢迎star
注意:本篇博客风格(不多比比就是撸代码!!!)
一、实现步骤
-
1.创建自定义事件,继承ApplicationEvent
-
2.创建自定义监听,实现ApplicationListener
-
3.通过ApplicationEventPublisher发布自定义事件
-
4.监听执行可以异步@Async并在启动类启动异步@EnableAsync
二、代码
1.CustomEvent.java(自定义事件)
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
/**
* @author Andon
* 2022/3/1
* <p>
* 自定义事件
*/
@Getter
public class CustomEvent extends ApplicationEvent {
private final String id;
private final String type;
public CustomEvent(Object source, String id, String type) {
super(source);
this.id = id;
this.type = type;
}
}
2.CustomListener.java(自定义监听)
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* @author Andon
* 2022/3/1
* <p>
* 自定义监听
*/
@Slf4j
@Component
public class CustomListener implements ApplicationListener<CustomEvent> {
@SneakyThrows
@Async("globalExecutorService") //异步,指定线程池
@Override
public void onApplicationEvent(CustomEvent event) {
log.info("CustomListener onApplicationEvent start!! [{}] event.source:{} event.id:{} event.type:{}", Thread.currentThread().getId(), event.getSource(), event.getId(), event.getType());
Thread.sleep(3000);
log.info("CustomListener onApplicationEvent end!!");
}
}
3.ApplicationEventPublisher.publishEvent(发布事件)
import com.andon.springbootutil.event.CustomEvent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author Andon
* 2022/3/1
*/
@Api(tags = "事件监听")
@Slf4j
@RestController
@RequestMapping(value = "/event")
public class TestEventController {
@Resource
private ApplicationEventPublisher applicationEventPublisher;
@ApiOperation(value = "事件监听测试")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "id"),
@ApiImplicitParam(name = "name", value = "name"),
@ApiImplicitParam(name = "type", value = "type"),
})
@GetMapping(value = "/test")
public void test(String id, String name, String type) {
log.info("TestEventController test start!! [{}] id:{} name:{} type:{}", Thread.currentThread().getId(), id, name, type);
applicationEventPublisher.publishEvent(new CustomEvent(name, id, type));
log.info("TestEventController test end!!");
}
}
4.SpringBootUtilApplication.java(启动类)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync //启用异步
@SpringBootApplication
public class SpringBootUtilApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootUtilApplication.class, args);
}
}
5.线程池
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author Andon
* 2021/12/8
*/
@Slf4j
@SpringBootConfiguration
public class ExecutorConfig {
@Bean
public ThreadPoolExecutor globalExecutorService() {
String osName = System.getProperty("os.name");
log.info("osName:{}", osName);
//获取当前机器的核数
int cpuNum = Runtime.getRuntime().availableProcessors();
log.info("cpuNum:{}", cpuNum);
ThreadPoolExecutor executor = new ThreadPoolExecutor(
cpuNum, cpuNum * 2, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
log.info("executor:{}", JSONObject.toJSONString(executor));
return executor;
}
}
三、测试
GitHub: link. 欢迎star