我在一个 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
...
...
LEATH
相关分类