public class MyApplicationRunner implements ApplicationRunner {
@Autowired
private AcquireHttpUtil acquireHttpUtil;
@Override
public void run(ApplicationArguments args) throws Exception {
SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
log.info("计时器开启。。。。。。");
while (true){
String s=df.format(new Date());
// System.out.println(s);
if ("23:50".equals(s)){//当时间为23:50
log.info("当前时间为:"+df.format(new Date())+"开启数据入库--------");
acquireHttpUtil.timeTask();
break;//退出这个while(true)循环
AcquireHttpUtil 类 period设置为24小时
@Slf4j
@Component
public class AcquireHttpUtil {
@Autowired
private HuYuDataBaseService huYuDataBaseService;
public void timeTask() {
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
executorService.scheduleAtFixedRate(() -> {
log.info("huYuDataBaseService");
}, 0, 24*60*60, TimeUnit.SECONDS
当springboot项目启动时MyApplicationRunner启动while(true)当到达指定时间退出while(true),并且同启动AcquireHttpUtil类的timeTask()方法。ScheduledExecutorService线程池启动,设定的时间为24小时,这样就实现了每天固定时间执行线程。
下面这个类当springboot启动时启动,@Component@Slf4jpublic class MyApplicationRunner implements ApplicationRunner { @Autowired private AcquireHttpUtil acquireHttpUtil; @Override public void ru...
FixedTimesScheduledExecutorService
您可以在工作线程上运行任务(Runnable或Callable),以指定执行计划(如ScheduledExecutorService),并指定执行时间。
ScheduledFuture<?> schedule( Runnable command, int executeTime, long delay, TimeUnit unit) {}
ScheduledFuture<?> schedule( Runnable command, int executeTime, long delay, TimeUnit unit, FixedTimesTaskListener listener) {}
< V> ScheduledFuture< V> schedule( Callable< V> callable, in
在spring的项目中,需要在容器启动之后执行一些操作。spring提供了ApplicationRunner和CommandLineRunner两个接口可以帮助我们实现这种需求。
当项目中实现了多个ApplicationRunner接口,并且其中一个使用了类似于while(true)这样不会推出的循环体。将会导致后续的ApplicationRunner接口不会被调用。
@Compo...
文章目录简介注意事项@Schedule默认线程池大小固定延迟与固定速率SpringBoot @Schedule原理ScheduledAnnotationBeanPostProcessorDestructionAwareBeanPostProcessor封装任务ApplicationListener执行任务
之前使用@Schedule一直没有遇到什么问题,那种拿来就用的感觉还挺好,最近使用@Schedule遇到一点问题,才仔细的研究了一下@Schedule的一些细节和原理问题。
这篇文章就将分享一下,使
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wqh8522/article/details/79224290
原文地址:SpringBoot几种定时任务的实现方式
————————————————
版权声明:本文为CSDN博主「wqh3520」的原创文章,遵循 CC 4.0 BY-S...
2、利用break:break是跳出整个循环,直接执行跳出循环后的下面的代码;
3、利用continue:continue是终止当次循环,不执行下面的代码,直接进入下一次循环