第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

spring task:scheduler XML配置問題

spring task:scheduler XML配置問題

有只小跳蛙 2022-05-25 09:53:49
我在一個 Bean 中有一些函數(shù),我想每 5 秒運(yùn)行一次,變量應(yīng)該是“通用的”而不是硬編碼:工作代碼如下:(我已經(jīng)寫在相關(guān)的 XML 中)@Component@EnableSchedulingpublic class CacheManager{    @Scheduled(initialDelay=0,fixedDelay=5000)      public void updateConfigurations() {        Some Code Here    }    @Scheduled(initialDelay=0,fixedDelay=5000)    public void updateMapping() {        Some Code Here    }}這兩個函數(shù)每 5 秒執(zhí)行一次。現(xiàn)在當(dāng)我想把它移到 XML 中時:<task:scheduler id="ttScheduler" pool-size="1" />    <task:scheduled-tasks scheduler="ttScheduler">        <task:scheduled ref="cacheManager" method="updateConfigurations" fixed-delay="5000" initial-delay="0" />        <task:scheduled ref="cacheManager" method="updateMapping" fixed-delay="5000" initial-delay="0" />    </task:scheduled-tasks>@Componentpublic class CacheManager{    public void updateConfigurations() {        log.info("update configurations");        Some Code        log.info("end update configurations");    }    public void updateMapping() {        Some Code Here    }}這兩個函數(shù)都可以毫無延遲地執(zhí)行。輸出:20190127-17:24:48.254  INFO [ttScheduler-1] CacheManager[109] - end update  configurations20190127-17:24:48.255  INFO [ttScheduler-1] CacheManager[105] - update  configurations20190127-17:24:48.282  INFO [ttScheduler-1] CacheManager[109] - end update  configurations20190127-17:24:48.283  INFO [ttScheduler-1] CacheManager[105] - update  configurations20190127-17:24:48.311  INFO [ttScheduler-1] CacheManager[109] - end update  configurations20190127-17:24:48.312  INFO [ttScheduler-1] CacheManager[105] - update  configurations20190127-17:24:48.337  INFO [ttScheduler-1] CacheManager[109] - end update  configurations20190127-17:24:48.337  INFO [ttScheduler-1] CacheManager[105] - update  configurations20190127-17:24:48.362  INFO [ttScheduler-1] CacheManager[109] - end update  configuration......
查看完整描述

1 回答

?
LEATH

TA貢獻(xiàn)1936條經(jīng)驗 獲得超7個贊

這可能是因為在 XML 中您使用固定速率,而在 Java 配置中您使用固定延遲。正如官方 Spring Doc所述:


...固定延遲指示每個任務(wù)執(zhí)行完成后等待的毫秒數(shù)。另一個選項是fixed-rate,表示該方法應(yīng)該多久執(zhí)行一次,而不管之前的執(zhí)行需要多長時間......


因此,如果您的示例中的某些代碼在某個時間間隔內(nèi)花費(fèi)的時間比您的固定費(fèi)率多得多,那么連續(xù)的任務(wù)可能剛剛在該時間間隔內(nèi)排隊。一旦完成繁重的執(zhí)行并且如果下一次執(zhí)行恰好是輕量級的,那么您將在日志中看到您在日志中看到的內(nèi)容,直到 ScheduledExecutorService 的隊列被耗盡。之后,您的輕量級任務(wù)將開始以固定速率毫秒運(yùn)行。


嘗試在日志中搜索第一次執(zhí)行以查看它們所花費(fèi)的時間。您還可以注釋掉一些代碼并重新啟動您的應(yīng)用程序以嘗試定位根本原因。下面是我的模擬示例,預(yù)計每 1 秒運(yùn)行一次,但第一次運(yùn)行需要 5 秒:


public class CacheManager {

    private static final Logger LOG = LoggerFactory.getLogger(CacheManager.class);


    private final AtomicInteger counter = new AtomicInteger();


    public void updateConfigurations() throws InterruptedException {

        LOG.info("update configurations");

        if (counter.getAndIncrement() == 0) {

            Thread.sleep(5000);

        }

        LOG.info("end update configurations");

    }


    public static void main(String[] args) {

        new ClassPathXmlApplicationContext("spring.xml");

    }

}

<beans ...>

    <bean id="cacheManager" class="CacheManager"/>


    <task:scheduler id="ttScheduler" pool-size="1"/>

    <task:scheduled-tasks scheduler="ttScheduler">

        <task:scheduled ref="cacheManager" method="updateConfigurations" fixed-rate="1000" initial-delay="0"/>

    </task:scheduled-tasks>

</beans>

輸出:


21:00:58.703 [ttScheduler-1] INFO  CacheManager - update configurations

21:01:03.706 [ttScheduler-1] INFO  CacheManager - end update configurations

21:01:03.706 [ttScheduler-1] INFO  CacheManager - update configurations

21:01:03.707 [ttScheduler-1] INFO  CacheManager - end update configurations

21:01:03.707 [ttScheduler-1] INFO  CacheManager - update configurations

21:01:03.707 [ttScheduler-1] INFO  CacheManager - end update configurations

21:01:03.707 [ttScheduler-1] INFO  CacheManager - update configurations

21:01:03.707 [ttScheduler-1] INFO  CacheManager - end update configurations

21:01:03.707 [ttScheduler-1] INFO  CacheManager - update configurations

21:01:03.708 [ttScheduler-1] INFO  CacheManager - end update configurations

21:01:03.708 [ttScheduler-1] INFO  CacheManager - update configurations

21:01:03.708 [ttScheduler-1] INFO  CacheManager - end update configurations

21:01:04.707 [ttScheduler-1] INFO  CacheManager - update configurations

21:01:04.708 [ttScheduler-1] INFO  CacheManager - end update configurations

21:01:05.706 [ttScheduler-1] INFO  CacheManager - update configurations

21:01:05.706 [ttScheduler-1] INFO  CacheManager - end update configurations

21:01:06.704 [ttScheduler-1] INFO  CacheManager - update configurations

21:01:06.704 [ttScheduler-1] INFO  CacheManager - end update configurations


查看完整回答
反對 回復(fù) 2022-05-25
  • 1 回答
  • 0 關(guān)注
  • 238 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號