怎么在Spring中使用TaskScheduler方法
今天就跟大家聊聊有关怎么在Spring中使用TaskScheduler方法,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

洛宁网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联公司2013年成立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
TaskScheduler
提供对计划任务提供支持;
使用@EnableScheduling开启计划任务支持
使用@Scheduled来注解计划任务的方法;
示例
演示后台间断执行任务和定时计划任务
计划任务的配置
@Configuration
@EnableScheduling
public class DemoConfig {
}计划配置任务类
package com.wisely.task.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class DemoScheduledTask {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000) //每五秒执行一次
public void reportCurrentTime() {
System.out.println("每隔五秒执行一次 " + dateFormat.format(new Date()));
}
@Scheduled(cron = "0 22 11 ? * *" ) //每天上午11点22执行
public void fixTimeExecution(){
System.out.println("在指定时间 " + dateFormat.format(new Date())+"执行");
}
}测试
package com.wisely.task.scheduler;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
@SuppressWarnings({ "unused", "resource" })
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.task.scheduler");
}
}输出结果
每隔五秒执行一次 11:21:42 每隔五秒执行一次 11:21:47 每隔五秒执行一次 11:21:52 每隔五秒执行一次 11:21:57 在指定时间 11:22:00执行 每隔五秒执行一次 11:22:02
看完上述内容,你们对怎么在Spring中使用TaskScheduler方法有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
网站栏目:怎么在Spring中使用TaskScheduler方法
标题URL:http://www.jxjierui.cn/article/ijdopp.html


咨询
建站咨询
