定义一个事件

就是一个普通的类,继承 ApplicationEvent 即可。

  1. public class LightEvent extends ApplicationEvent {
  2. private static final long serialVersionUID = 1L;
  3. private LightColorEnum lightColor;
  4. public LightEvent(Object source) {
  5. super(source);
  6. }
  7. public LightEvent(Object source, LightColorEnum lightColor) {
  8. super(source);
  9. this.setLightColor(lightColor);
  10. }
  11. public LightColorEnum getLightColor() {
  12. return lightColor;
  13. }
  14. public void setLightColor(LightColorEnum lightColor) {
  15. this.lightColor = lightColor;
  16. }
  17. public enum LightColorEnum {
  18. RED("stop"), YELLOW("wait"), GREEN("go");
  19. private String message;
  20. private LightColorEnum(String message){
  21. this.message = message;
  22. }
  23. public String getMessage() {
  24. return message;
  25. }
  26. public void setMessage(String message) {
  27. this.message = message;
  28. }
  29. }
  30. }

监听一个事件

  1. @Component
  2. public class LightEventAnnotationListener {
  3. private static final Logger log = LoggerFactory.getLogger(LightEventAnnotationListener.class);
  4. @EventListener
  5. @Async //异步方法
  6. public void handler(LightEvent event){
  7. try {
  8. Thread.sleep(2000); //模拟一个比较耗时的操作
  9. } catch (Exception e) {
  10. e.printStackTrace();
  11. }
  12. log.info("traffic light:" + event.getLightColor().getMessage());
  13. log.info(Thread.currentThread().getName());
  14. }
  15. }

发布一个事件

  1. @Autowired
  2. private ApplicationEventPublisher applicationEventPublisher;
  3. @RequestMapping("/publish")
  4. public Object publish(){
  5. LightEvent lightEvent = new LightEvent(this, LightEvent.LightColorEnum.YELLOW);
  6. applicationEventPublisher.publishEvent(lightEvent);
  7. log.info("things after publish");
  8. return "ok";
  9. }

异步执行

默认是同步,发布事件后,主线程会等待所有监听方法执行完毕,才会继续下一步,例如
image.png

开启异步

1、启动类

image.png
2、监听方法
image.png

监听器执行顺序


通过 SmartApplicationListener 设置优先级等,可以进行更详细的控制。

  1. import org.springframework.context.ApplicationEvent;
  2. import org.springframework.context.event.SmartApplicationListener;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * 监听器
  6. * SmartApplicationListener可以设置顺序等
  7. */
  8. @Component
  9. public class WechatListener implements SmartApplicationListener {
  10. //设置监听优先级,越小越靠前
  11. @Override
  12. public int getOrder() {
  13. return 1;
  14. }
  15. //监听器智能所在之一,能够根据事件类型动态监听
  16. @Override
  17. public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
  18. return aClass == OrderCreateEvent.class;
  19. }
  20. //监听器智能所在之二,能够根据事件发布者类型动态监听
  21. @Override
  22. public boolean supportsSourceType(Class<?> aClass) {
  23. return aClass == OrderServiceImpl.class;
  24. }
  25. @Override
  26. @Async
  27. public void onApplicationEvent(ApplicationEvent applicationEvent) {
  28. OrderCreateEvent event = (OrderCreateEvent) applicationEvent;
  29. //发送微信
  30. System.out.println(event.getContentList().get(0) + ",您的订单:" + event.getContentList().get(1) + "创建成功! ----by wechat");
  31. }
  32. }