springboot(十)定时任务
2016-12-30 16:15
851 查看
1、在demoApplication里添加@EnableScheduling,开启定时任务
2、写定时任务
5秒执行一次。
说明
//定义一个按时间执行的定时任务,在每天16:00执行一次。
@Scheduled(cron = "0 0 16 * * ?")
//定义一个按一定频率执行的定时任务,每隔1分钟执行一次
@Scheduled(fixedRate = 1000 * 60)
//定义一个按一定频率执行的定时任务,每隔1分钟执行一次,延迟1秒执行
@Scheduled(fixedRate = 1000 * 60,initialDelay = 1000)
// 每天早八点到晚八点,间隔2分钟执行任务@Scheduled(cron="0 0/2 8-20 * * ?")
// 每天早八点到晚八点,间隔3分钟执行任务@Scheduled(cron="0 0/3 8-20 * * ?")
// 每天早八点到晚八点,间隔1分钟执行任务@Scheduled(cron="0 0/1 8-20 * * ?")
cron表达式中各时间元素使用空格进行分割,分别表示如下含义:
按顺序依次为
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
7.年份(1970-2099)
package com.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @ServletComponentScan @EnableScheduling public class DemoApplication extends SpringBootServletInitializer{ @RequestMapping("/") public String home(){ return "hello world"; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
2、写定时任务
package com.demo.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDate; import java.time.LocalTime; /** * Created by huguoju on 2016/12/30. */ @Component public class ScheduledTasks { @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System.out.println("The time is now " + LocalTime.now()); } }
5秒执行一次。
说明
//定义一个按时间执行的定时任务,在每天16:00执行一次。
@Scheduled(cron = "0 0 16 * * ?")
//定义一个按一定频率执行的定时任务,每隔1分钟执行一次
@Scheduled(fixedRate = 1000 * 60)
//定义一个按一定频率执行的定时任务,每隔1分钟执行一次,延迟1秒执行
@Scheduled(fixedRate = 1000 * 60,initialDelay = 1000)
// 每天早八点到晚八点,间隔2分钟执行任务@Scheduled(cron="0 0/2 8-20 * * ?")
// 每天早八点到晚八点,间隔3分钟执行任务@Scheduled(cron="0 0/3 8-20 * * ?")
// 每天早八点到晚八点,间隔1分钟执行任务@Scheduled(cron="0 0/1 8-20 * * ?")
cron表达式中各时间元素使用空格进行分割,分别表示如下含义:
按顺序依次为
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
7.年份(1970-2099)
相关文章推荐
- spring boot环境下使用quartz设置定时任务
- Spring Boot 定时任务的使用
- spring boot 定时任务
- springboot定时任务
- 【SpringBoot】——创建定时任务
- SpringBoot原生定时任务解析
- SpringBoot 定时任务遇到的坑
- 基于Spring Boot 实现定时任务
- Spring Boot:在Spring Boot中使用定时任务
- (14)Spring Boot定时任务的使用【从零开始学Spring Boot】
- Spring Boot:在Spring Boot中使用定时任务
- spring boot调用spring自带定时任务
- Springboot 之 使用Scheduled做定时任务
- 14. Spring Boot定时任务的使用【从零开始学Spring Boot】
- Spring Boot使用方法小札(2):执行定时任务
- Spring Boot:在Spring Boot中使用定时任务
- spring-boot之简单定时任务
- spring boot创建定时任务
- 【springboot】 springboot集成quartz实现定时任务
- 在Spring Boot中使用@Scheduled实现定时任务