当前位置: 首页 > news >正文

ApplicationListener , @EventListener 和 CommandLineRunner 启动顺序验证

一. 背景

排查线上问题, 发现一个重要功能的全局锁线程启动延迟很高. 服务启动40分钟之后, 才能拿到锁. 排查之后发现原因是因为代码引入了高优先级的ApplicationListener代码, 导致全局锁线程启动延迟.

二. 结论

启动顺序从高到底依次为: ApplicationListener , @EventListener, CommandLineRunner , 分别可以使用order调整排序.
但是order只能控制同类型间的启动顺序.

三. 伪代码

3.1. 实现ApplicationListener接口

优先级最高,  可以有多个, 同ApplicationListener类型之间, 可以通过@Order(1) 注解控制相同ApplicationListener实现的启动顺序. 

import java.util.concurrent.TimeUnit;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@Component
@Order(1)
public class ApplicationListener02Test implements ApplicationListener<WebServerInitializedEvent> {@Overridepublic void onApplicationEvent(WebServerInitializedEvent event) {System.out.println("Listener : ApplicationListener02Test");try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}

3.2. @EventListener 注解方式

@EventListener 注解方式 优先级是低于 实现ApplicationListener的方式.  必须等实现ApplicationListener类都执行完才启动, 可以通过@Order(1) 注解控制相同@EventListener实现的启动顺序.

import java.util.concurrent.TimeUnit;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@Component
public class EventListener02Test {@EventListener@Order(0)public void listener(ContextRefreshedEvent event) {System.out.println("Listener : EventListener02Test");try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}}}

3.3. 实现CommandLineRunner 方式

优先级最低. 实现CommandLineRunner接口方式 优先级是低于 @EventListener的方式.  必须等实现@EventListener类都执行完才启动, 可以通过@Order(1) 注解控制相同CommandLineRunner实现的启动顺序.
package com.netease.easyflink.monitor.listener;import java.util.concurrent.TimeUnit;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;@Component
@Order(1)
public class CommandLineRunner01Test implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {System.out.println("Listener : CommandLineRunner01Test");try {TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
http://www.lryc.cn/news/132817.html

相关文章:

  • 网络编程基础(1)
  • Linux驱动开发(Day4)
  • LVS负载均衡群集部署(LVS-NAT模型实例)
  • 【仿写tomcat】五、响应静态资源(访问html页面)、路由支持以及多线程改进
  • stm32单片机/51单片机蜂鸣器不响(proteus模拟)
  • BERT、ERNIE、Grover、XLNet、GPT、MASS、UniLM、ELECTRA、RoBERTa、T5、C4
  • 主机防护的重要性和方式
  • 聚观早报 | 抢先体验阿维塔11座舱;本田和讴歌采用NACS充电标准
  • 思科计算机网络答案(包含第1~11章节)
  • 所见即所得,「Paraverse平行云」助力万间打造智能建造新图景
  • AI图片处理功能演示
  • CentOS系统环境搭建(六)——使用docker-compose安装redis
  • 个人论坛项目测试报告
  • 一起来学shiny把(4)—调控控件进行输出
  • VBIC卡管理系统设计与实现
  • 八种架构演进
  • 商城-学习整理-高级-分布式事务(十九)
  • Java学习笔记(三):面向对象
  • 电商项目part02 电商后台多数据源
  • 【C# 基础精讲】LINQ 基础
  • ChatGPT成为工作工具,具体都应用在哪些地方?
  • Shader学习(三)(片元着色器)
  • 谷歌推出首款量子弹性 FIDO2 安全密钥
  • 前端常用的三种加密方式(MD5、base64、sha.js)
  • alpine镜像时区设置
  • Java导入Excel,保留日期格式为文本格式
  • uploadifive php上传进度条插件 解决动态传参数问题
  • Lombok生成的Getter和Setter的名称对于“eMail”或“xAxis”等属性存在大小写转换异常
  • Redis基础概念和数据类型详解
  • C语言之extern “C“详解与使用方法