網(wǎng)站首頁 編程語言 正文
一、說明
SmartInitializingSingleton 接口的 afterSingletonsInstantiated()方法類似bean實例化執(zhí)行后的回調(diào)函數(shù)。
afterSingletonsInstantiated 會在spring 容器基本啟動完成后執(zhí)行。此時所有的單列bean都已初始化完成。實現(xiàn)了SmartInitializingSingleton 接口的類
可以在afterSingletonsInstantiated 中做一些回調(diào)操作。
二、XXL-JOB 中SmartInitializingSingleton 的使用
xxl-job 是一個輕量級的分布式任務(wù)調(diào)度框架,通過一個中心式的調(diào)度平臺,調(diào)度多個執(zhí)行器執(zhí)行任務(wù),而且監(jiān)控界面就集成在調(diào)度中心,界面又簡潔。
xxl-job中, XxlJobSpringExecutor,XxlJobSimpleExecutor 繼承自 XxlJobExecutor 同時也實現(xiàn)了 SmartInitializingSingleton 接口。
XxlJobSpringExecutor 在 afterSingletonsInstantiated() 方法中,完成了提取 @XxlJob 注解標(biāo)注的方法,用于后續(xù)任務(wù)的執(zhí)行。調(diào)用父類方法中的start() 方法,初始化netty服務(wù)器。
public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationContextAware, SmartInitializingSingleton, DisposableBean {
private static final Logger logger = LoggerFactory.getLogger(XxlJobSpringExecutor.class);
// start
@Override
public void afterSingletonsInstantiated() {
// init JobHandler Repository
/*initJobHandlerRepository(applicationContext);*/
// init JobHandler Repository (for method)
initJobHandlerMethodRepository(applicationContext);
// refresh GlueFactory
GlueFactory.refreshInstance(1);
// super start
try {
super.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/// 其他代碼 省略
}
父類中的start() 方法:initEmbedServer 用于初始化netty服務(wù)。
// ---------------------- start + stop ----------------------
public void start() throws Exception {
// init logpath
XxlJobFileAppender.initLogPath(logPath);
// init invoker, admin-client
initAdminBizList(adminAddresses, accessToken);
// init JobLogFileCleanThread
JobLogFileCleanThread.getInstance().start(logRetentionDays);
// init TriggerCallbackThread
TriggerCallbackThread.getInstance().start();
// init executor-server 初始化netty 服務(wù)
initEmbedServer(address, ip, port, appname, accessToken);
}
獲取容器中被@XxlJob 注解標(biāo)注的方法的實現(xiàn)方法:
private void initJobHandlerMethodRepository(ApplicationContext applicationContext) {
if (applicationContext == null) {
return;
}
// init job handler from method
String[] beanDefinitionNames = applicationContext.getBeanNamesForType(Object.class, false, true);
for (String beanDefinitionName : beanDefinitionNames) {
Object bean = applicationContext.getBean(beanDefinitionName);
Map<Method, XxlJob> annotatedMethods = null; // referred to :org.springframework.context.event.EventListenerMethodProcessor.processBean
try {
annotatedMethods = MethodIntrospector.selectMethods(bean.getClass(),
new MethodIntrospector.MetadataLookup<XxlJob>() {
@Override
public XxlJob inspect(Method method) {
return AnnotatedElementUtils.findMergedAnnotation(method, XxlJob.class);
}
});
} catch (Throwable ex) {
logger.error("xxl-job method-jobhandler resolve error for bean[" + beanDefinitionName + "].", ex);
}
if (annotatedMethods==null || annotatedMethods.isEmpty()) {
continue;
}
for (Map.Entry<Method, XxlJob> methodXxlJobEntry : annotatedMethods.entrySet()) {
Method executeMethod = methodXxlJobEntry.getKey();
XxlJob xxlJob = methodXxlJobEntry.getValue();
// regist
registJobHandler(xxlJob, bean, executeMethod);
}
}
}
使用時和其他框架差不多,在@Configuraton 修飾的配置類中返回一個示例化的bean即可。
@Configuration
public class XxlJobConfig {
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAddress(address);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
三、spring 容器中 SmartInitializingSingleton 的使用
EventListenerMethodProcessor也實現(xiàn)了 SmartInitializingSingleton 接口,主要用于完成@EventListener注解方式的事件監(jiān)聽。
在afterSingletonsInstantiated() 方法中處理邏輯與xxl-job 中的類似,都是用于篩選實例中被指定注解修飾的方法。
public class EventListenerMethodProcessor
implements SmartInitializingSingleton, ApplicationContextAware, BeanFactoryPostProcessor {
@Override
public void afterSingletonsInstantiated() {
ConfigurableListableBeanFactory beanFactory = this.beanFactory;
Assert.state(this.beanFactory != null, "No ConfigurableListableBeanFactory set");
String[] beanNames = beanFactory.getBeanNamesForType(Object.class);
for (String beanName : beanNames) {
if (!ScopedProxyUtils.isScopedTarget(beanName)) {
Class<?> type = null;
try {
type = AutoProxyUtils.determineTargetClass(beanFactory, beanName);
}
catch (Throwable ex) {
// An unresolvable bean type, probably from a lazy bean - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
}
}
if (type != null) {
if (ScopedObject.class.isAssignableFrom(type)) {
try {
Class<?> targetClass = AutoProxyUtils.determineTargetClass(
beanFactory, ScopedProxyUtils.getTargetBeanName(beanName));
if (targetClass != null) {
type = targetClass;
}
}
catch (Throwable ex) {
// An invalid scoped proxy arrangement - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve target bean for scoped proxy '" + beanName + "'", ex);
}
}
}
try {
processBean(beanName, type);
}
catch (Throwable ex) {
throw new BeanInitializationException("Failed to process @EventListener " +
"annotation on bean with name '" + beanName + "'", ex);
}
}
}
}
}
}
寫在最后
以上兩個使用的方式都是在spring容器 單列被加載完成后,處理一些自定義的邏輯。EventListenerMethodProcessor主要用于篩選出被@EventListener
注解的方法,在后續(xù)的使用中 通過 發(fā)布-訂閱模式,實現(xiàn)事件的監(jiān)聽。
XxlJobSpringExecutor 主要用于篩選出被@XxlJob 注解標(biāo)注的方法,用于后續(xù)任務(wù)的啟動和停止。
原文鏈接:https://blog.csdn.net/daxues_/article/details/125750267
相關(guān)推薦
- 2022-10-09 python將寫好的程序打包成exe可執(zhí)行文件_python
- 2022-01-15 跨域系列之proxy代理,解決跨域的方法之一
- 2022-07-15 Python?print函數(shù):如何將對象打印輸出_python
- 2022-04-17 Specified key was too long; max key length is 767
- 2023-05-20 pytorch中forwod函數(shù)在父類中的調(diào)用方式解讀_python
- 2022-10-18 AJAX跨域問題解決方案詳解_AJAX相關(guān)
- 2022-10-13 Broadcast廣播機制在Pytorch?Tensor?Numpy中的使用詳解_python
- 2022-02-26 SpringSecurity 自定義JwtAuthorFilter基于JWT的Token驗證
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支