springboot 定时任务@Scheduled实现解析
发布时间:2020-05-24 00:23:17 所属栏目:Java 来源:互联网
导读:springboot 定时任务@Scheduled实现解析 这篇文章主要介绍了springboot 定时任务@Scheduled实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.pom.xml中导入必要的依赖: parent groupIdorg.spring
|
这篇文章主要介绍了springboot 定时任务@Scheduled实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1、pom.xml中导入必要的依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
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 // 开启定时任务
@EnableAutoConfiguration
public 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;
@Component
public 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的效果是一样一样的 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Java:我有一大串html,需要提取href =“…”文本
- java – org.jgroups.protocols.UDP – 将消息发送到null失
- 简单实现Java版学生管理系统
- java – 从另一个Servlet调用Servlet Post
- java – JMeter Scheduler中的开始时间和结束时间
- Java读写Excel实例分享
- java-8 – Weblogic 12.2.1 Java 8 Spring Data JPA Hibern
- 为什么需要对Java中的某些字符转换进行逐位“和”?
- java – 什么更有效?一个If Else还是一个HashMap?
- spring boot 注入 property的三种方式(推荐)
