资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springboot中如何使用@Scheduled实现定时任务-创新互联

springboot中如何使用@Scheduled实现定时任务,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计制作、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的罗定网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

1、pom.xml中导入必要的依赖:

   org.springframework.boot    spring-boot-starter-parent    2.0.1.RELEASE                  org.springframework.boot      spring-boot-starter-web              org.springframework.boot      spring-boot-starter-tomcat      

2、写一个springboot的启动类:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.scheduling.annotation.EnableScheduling;@ComponentScan(basePackages = { "com.xwj.tasks" })@EnableScheduling // 开启定时任务@EnableAutoConfigurationpublic class App {  public static void main(String[] args) {    SpringApplication.run(App.class, args);  }}

注意这里一定要加上@EnableScheduling注解,用于开启定时任务

3、开始写定时任务:

import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class ScheduleTask {  @Scheduled(fixedRate = 1000)  // @Scheduled(cron = "0 23-25 18 * * ?")  public void testSchedule() {    System.out.println("定时任务:" + System.currentTimeMillis());  }}

解释:

@Scheduled注解:

1、fixedRate 以固定速率执行。以上表示每隔1秒执行一次

2、fixedDelay 以上一个任务开始时间为基准,从上一任务开始执行后再次调用

3、cron表达式。可以实现定时调用。

在使用的过程中,楼主觉得,如果只有一个定时任务,fixedRate与fixedDelay的效果是一样一样的

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联网站建设公司,的支持。


本文名称:springboot中如何使用@Scheduled实现定时任务-创新互联
当前链接:http://cdkjz.cn/article/ddjgge.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220