猿问

spring task:scheduler XML配置问题

我在一个 Bean 中有一些函数,我想每 5 秒运行一次,变量应该是“通用的”而不是硬编码:


工作代码如下:(我已经写在相关的 XML 中)


@Component

@EnableScheduling

public class CacheManager{



    @Scheduled(initialDelay=0,fixedDelay=5000)  

    public void updateConfigurations() {


        Some Code Here

    }


    @Scheduled(initialDelay=0,fixedDelay=5000)

    public void updateMapping() {


        Some Code Here


    }


}

这两个函数每 5 秒执行一次。


现在当我想把它移到 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>



@Component

public class CacheManager{



    public void updateConfigurations() {


        log.info("update configurations");


        Some Code


        log.info("end update configurations");

    }


    public void updateMapping() {


        Some Code Here


    }


}

这两个函数都可以毫无延迟地执行。输出:


20190127-17:24:48.254  INFO [ttScheduler-1] CacheManager[109] - end update  configurations

20190127-17:24:48.255  INFO [ttScheduler-1] CacheManager[105] - update  configurations

20190127-17:24:48.282  INFO [ttScheduler-1] CacheManager[109] - end update  configurations

20190127-17:24:48.283  INFO [ttScheduler-1] CacheManager[105] - update  configurations

20190127-17:24:48.311  INFO [ttScheduler-1] CacheManager[109] - end update  configurations

20190127-17:24:48.312  INFO [ttScheduler-1] CacheManager[105] - update  configurations

20190127-17:24:48.337  INFO [ttScheduler-1] CacheManager[109] - end update  configurations

20190127-17:24:48.337  INFO [ttScheduler-1] CacheManager[105] - update  configurations

20190127-17:24:48.362  INFO [ttScheduler-1] CacheManager[109] - end update  configuration

...

...


有只小跳蛙
浏览 223回答 1
1回答

LEATH

这可能是因为在 XML 中您使用固定速率,而在 Java 配置中您使用固定延迟。正如官方 Spring Doc所述:...固定延迟指示每个任务执行完成后等待的毫秒数。另一个选项是fixed-rate,表示该方法应该多久执行一次,而不管之前的执行需要多长时间......因此,如果您的示例中的某些代码在某个时间间隔内花费的时间比您的固定费率多得多,那么连续的任务可能刚刚在该时间间隔内排队。一旦完成繁重的执行并且如果下一次执行恰好是轻量级的,那么您将在日志中看到您在日志中看到的内容,直到 ScheduledExecutorService 的队列被耗尽。之后,您的轻量级任务将开始以固定速率毫秒运行。尝试在日志中搜索第一次执行以查看它们所花费的时间。您还可以注释掉一些代码并重新启动您的应用程序以尝试定位根本原因。下面是我的模拟示例,预计每 1 秒运行一次,但第一次运行需要 5 秒:public class CacheManager {&nbsp; &nbsp; private static final Logger LOG = LoggerFactory.getLogger(CacheManager.class);&nbsp; &nbsp; private final AtomicInteger counter = new AtomicInteger();&nbsp; &nbsp; public void updateConfigurations() throws InterruptedException {&nbsp; &nbsp; &nbsp; &nbsp; LOG.info("update configurations");&nbsp; &nbsp; &nbsp; &nbsp; if (counter.getAndIncrement() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(5000);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; LOG.info("end update configurations");&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new ClassPathXmlApplicationContext("spring.xml");&nbsp; &nbsp; }}<beans ...>&nbsp; &nbsp; <bean id="cacheManager" class="CacheManager"/>&nbsp; &nbsp; <task:scheduler id="ttScheduler" pool-size="1"/>&nbsp; &nbsp; <task:scheduled-tasks scheduler="ttScheduler">&nbsp; &nbsp; &nbsp; &nbsp; <task:scheduled ref="cacheManager" method="updateConfigurations" fixed-rate="1000" initial-delay="0"/>&nbsp; &nbsp; </task:scheduled-tasks></beans>输出:21:00:58.703 [ttScheduler-1] INFO&nbsp; CacheManager - update configurations21:01:03.706 [ttScheduler-1] INFO&nbsp; CacheManager - end update configurations21:01:03.706 [ttScheduler-1] INFO&nbsp; CacheManager - update configurations21:01:03.707 [ttScheduler-1] INFO&nbsp; CacheManager - end update configurations21:01:03.707 [ttScheduler-1] INFO&nbsp; CacheManager - update configurations21:01:03.707 [ttScheduler-1] INFO&nbsp; CacheManager - end update configurations21:01:03.707 [ttScheduler-1] INFO&nbsp; CacheManager - update configurations21:01:03.707 [ttScheduler-1] INFO&nbsp; CacheManager - end update configurations21:01:03.707 [ttScheduler-1] INFO&nbsp; CacheManager - update configurations21:01:03.708 [ttScheduler-1] INFO&nbsp; CacheManager - end update configurations21:01:03.708 [ttScheduler-1] INFO&nbsp; CacheManager - update configurations21:01:03.708 [ttScheduler-1] INFO&nbsp; CacheManager - end update configurations21:01:04.707 [ttScheduler-1] INFO&nbsp; CacheManager - update configurations21:01:04.708 [ttScheduler-1] INFO&nbsp; CacheManager - end update configurations21:01:05.706 [ttScheduler-1] INFO&nbsp; CacheManager - update configurations21:01:05.706 [ttScheduler-1] INFO&nbsp; CacheManager - end update configurations21:01:06.704 [ttScheduler-1] INFO&nbsp; CacheManager - update configurations21:01:06.704 [ttScheduler-1] INFO&nbsp; CacheManager - end update configurations
随时随地看视频慕课网APP

相关分类

Java
我要回答