欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

@Scheduled定時器原理及@RefreshScope相互影響

 更新時間:2023年07月13日 14:35:58   作者:王偵  
這篇文章主要為大家介紹了@Scheduled定時器原理及@RefreshScope相互影響詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

1.ScheduledAnnotationBeanPostProcessor

@EnableScheduling

  • @Import(SchedulingConfiguration.class)
  • 注冊了ScheduledAnnotationBeanPostProcessor

@RestController
@RefreshScope  //動態(tài)感知修改后的值
public class TestController implements ApplicationListener<RefreshScopeRefreshedEvent>{
    @Value("${common.age}")
     String age;
    @Value("${common.name}")
     String name;
    @GetMapping("/common")
    public String hello() {
        return name+","+age;
    }
    //觸發(fā)@RefreshScope執(zhí)行邏輯會導(dǎo)致@Scheduled定時任務(wù)失效
    @Scheduled(cron = "*/3 * * * * ?")  //定時任務(wù)每隔3s執(zhí)行一次
    public void execute() {
        System.out.println("定時任務(wù)正常執(zhí)行。。。。。。");
    }
    @Override
    public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
        this.execute();
    }
}

1.1 SmartInitializingSingleton#afterSingletonsInstantiated

ScheduledAnnotationBeanPostProcessor#afterSingletonsInstantiated

  • DefaultListableBeanFactory#preInstantiateSingletons
  • SmartInitializingSingleton#afterSingletonsInstantiated
  • 沒干啥重要事情

1.2 RefreshScope處理ContextRefreshedEvent創(chuàng)建refresh中的bean

并調(diào)用ScheduledAnnotationBeanPostProcessor#postProcessAfterInitialization找出TestController中加了@Scheduled注解的方法。

ScheduledAnnotationBeanPostProcessor#postProcessAfterInitialization

  • 發(fā)布ContextRefreshedEvent
  • RefreshScope#onApplicationEvent
  • Object bean = this.context.getBean(name); 獲取scope為refresh的bean:scopedTarget.testController
  • 會調(diào)用至postProcessAfterInitialization
  • 找到有@Scheduled注解的方法execute()
  • tasks.add(this.registrar.scheduleCronTask(new CronTask(runnable, new CronTrigger(cron, timeZone))));

1.3 ScheduledAnnotationBeanPostProcessor本身也能處理ContextRefreshedEvent

這里真正開始調(diào)度1.2中找到的任務(wù)。

ScheduledAnnotationBeanPostProcessor#onApplicationEvent

  • ScheduledAnnotationBeanPostProcessor也會處理ContextRefreshedEvent
  • ScheduledAnnotationBeanPostProcessor#finishRegistration
  • this.taskScheduler設(shè)置為ThreadPoolTaskScheduler(哪里配置的?)
  • ScheduledTaskRegistrar#afterPropertiesSet
  • ScheduledTaskRegistrar#scheduleTasks
  • 開始執(zhí)行任務(wù),這cronTasks不為空,則執(zhí)行該任務(wù)
    addScheduledTask(scheduleCronTask(task));
  • ScheduledTaskRegistrar#scheduleCronTask
  • scheduledTask.future = this.taskScheduler.schedule(task.getRunnable(), task.getTrigger());
  • ThreadPoolTaskScheduler#schedule
  • ReschedulingRunnable#schedule以及ReschedulingRunnable#run實現(xiàn)定時調(diào)度,線程池為ScheduledThreadPoolExecutor

2.@RefreshScope的影響

當Nacos Config配置中心發(fā)布配置時,會調(diào)用RefreshScope#refreshAll。
此時會ScheduledAnnotationBeanPostProcessor#postProcessBeforeDestruction會將加了@RefreshScope的TestController里面的任務(wù)全部cancel掉。

    public void refreshAll() {
        super.destroy();
        this.context.publishEvent(new RefreshScopeRefreshedEvent());
    }

RefreshScope#refreshAll

  • GenericScope.BeanLifecycleWrapper#destroy
  • DisposableBeanAdapter#run
  • ScheduledAnnotationBeanPostProcessor#postProcessBeforeDestruction會將TestController里面的任務(wù)全部cancel掉。

ScheduledAnnotationBeanPostProcessor#postProcessBeforeDestruction

    @Override
    public void postProcessBeforeDestruction(Object bean, String beanName) {
        Set<ScheduledTask> tasks;
        synchronized (this.scheduledTasks) {
            tasks = this.scheduledTasks.remove(bean);
        }
        if (tasks != null) {
            for (ScheduledTask task : tasks) {
                task.cancel();
            }
        }
    }

取消核心流程GenericScope#destroy()

    public void destroy() {
        List<Throwable> errors = new ArrayList<Throwable>();
        Collection<BeanLifecycleWrapper> wrappers = this.cache.clear();
        for (BeanLifecycleWrapper wrapper : wrappers) {
            try {
                Lock lock = this.locks.get(wrapper.getName()).writeLock();
                lock.lock();
                try {
                    wrapper.destroy();
                }
                finally {
                    lock.unlock();
                }
            }
            catch (RuntimeException e) {
                errors.add(e);
            }
        }
        if (!errors.isEmpty()) {
            throw wrapIfNecessary(errors.get(0));
        }
        this.errors.clear();
    }

2.1 ThreadPoolTaskScheduler在哪里配置的?

ThreadPoolTaskScheduler

  • 構(gòu)造bean:nacosWatch:NacosWatch時會創(chuàng)建一個
  • 構(gòu)造bean:taskScheduler:ThreadPoolTaskScheduler

public class TaskSchedulingAutoConfiguration {
    @Bean
    @ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
    @ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class, ScheduledExecutorService.class })
    public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
        return builder.build();
    }
    @Bean
    @ConditionalOnMissingBean
    public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties,
            ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
        TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
        builder = builder.poolSize(properties.getPool().getSize());
        Shutdown shutdown = properties.getShutdown();
        builder = builder.awaitTermination(shutdown.isAwaitTermination());
        builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
        builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
        builder = builder.customizers(taskSchedulerCustomizers);
        return builder;
    }

2.2 TestController改造成ApplicationListener<RefreshScopeRefreshedEvent>

這樣做為什么能夠保證定時任務(wù)正常執(zhí)行?

  • RefreshScope#refreshAll
  • 發(fā)布RefreshScopeRefreshedEvent事件
  • 調(diào)用到TestController代理對象#onApplicationEvent
  • CglibAopProxy.DynamicAdvisedInterceptor#intercept
  • SimpleBeanTargetSource#getTarget
  • AbstractBeanFactory#getBean獲取scopedTarget.testController
  • GenericScope#get
  • 會創(chuàng)建真正的TestController實例,然后初始化后調(diào)用ScheduledAnnotationBeanPostProcessor#postProcessAfterInitialization找出TestController中加了@Scheduled注解的方法
  • TestController#onApplicationEvent
  • this.execute();
  • 真正調(diào)用的是ReschedulingRunnable#run

以上就是@Scheduled定時器原理及@RefreshScope相互影響的詳細內(nèi)容,更多關(guān)于Scheduled定時器RefreshScope的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot依賴和代碼分開打包的實現(xiàn)步驟

    SpringBoot依賴和代碼分開打包的實現(xiàn)步驟

    本文主要介紹了SpringBoot依賴和代碼分開打包的實現(xiàn)步驟,,這種方法將依賴和代碼分開打包,一般更新只有代碼修改,Pom文件是不會經(jīng)常改動的,感興趣的可以了解一下
    2023-10-10
  • 23種設(shè)計模式(10)java組合模式

    23種設(shè)計模式(10)java組合模式

    這篇文章主要為大家詳細介紹了23種設(shè)計模式之java組合模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • SpringBoot整合Thymeleaf小項目及詳細流程

    SpringBoot整合Thymeleaf小項目及詳細流程

    這篇文章主要介紹了SpringBoot整合Thymeleaf小項目,本項目使用SpringBoot開發(fā),jdbc5.1.48,主要涉及到Mybatis的使用,Thymeleaf的使用,用戶密碼加密,驗證碼的設(shè)計,圖片的文件上傳(本文件上傳到本地,沒有傳到數(shù)據(jù)庫)登錄過濾,需要的朋友可以參考下
    2022-03-03
  • 在IntelliJ IDEA中多線程并發(fā)代碼的調(diào)試方法詳解

    在IntelliJ IDEA中多線程并發(fā)代碼的調(diào)試方法詳解

    這篇文章主要介紹了在IntelliJ IDEA中多線程并發(fā)代碼的調(diào)試方法,本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 解析springcloud中的Hystrix

    解析springcloud中的Hystrix

    Hystrix是一個用于處理分布式系統(tǒng)的延遲和容錯的開源庫,在分布式系統(tǒng)里,許多依賴不可避免的會調(diào)用失敗,比如超時、異常等。這篇文章主要介紹了springcloud中的Hystrix,需要的朋友可以參考下
    2020-10-10
  • Springboot接入MyBatisPlus的實現(xiàn)

    Springboot接入MyBatisPlus的實現(xiàn)

    最近web端比較熱門的框架就是SpringBoot和Mybatis-Plus,這里簡單總結(jié)集成用法,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • 分析SpringBoot的啟動原理

    分析SpringBoot的啟動原理

    這篇文章主要分析了SpringBoot的啟動原理,幫助大家更好的理解和使用spring boot框架,感興趣的朋友可以了解下
    2020-09-09
  • 解決IDEA克隆代碼后在右下角沒有g(shù)it分支的問題

    解決IDEA克隆代碼后在右下角沒有g(shù)it分支的問題

    這篇文章主要介紹了解決IDEA克隆代碼后在右下角沒有g(shù)it分支的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java的文檔注釋之生成幫助文檔的實例

    Java的文檔注釋之生成幫助文檔的實例

    下面小編就為大家分享一篇Java的文檔注釋之生成幫助文檔的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • Java實現(xiàn)將Word轉(zhuǎn)換成Html的示例代碼

    Java實現(xiàn)將Word轉(zhuǎn)換成Html的示例代碼

    在業(yè)務(wù)中,常常會需要在瀏覽器中預(yù)覽Word文檔,或者需要將Word文檔轉(zhuǎn)成HTML文件保存,本文主要為大家詳細介紹了Java實現(xiàn)Word轉(zhuǎn)換成Html的相關(guān)方法,希望對大家有所幫助
    2024-02-02

最新評論