Spring-1 定时任务

定时任务 

  • 入门案例

创建一个 Spring Initializr项目

 

 

/*
 * 开启定时任务
 */
@EnableScheduling
@SpringBootApplication
public class CrontabApplication {

    public static void main(String[] args) {
        SpringApplication.run(CrontabApplication.class, args);
    }

    /*
     *9:00-20:00每半小时执行一次
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void task01() {
        System.out.println(Thread.currentThread().getName() + "执行定时任务01 - " + DateFormat.getDateTimeInstance().format(new Date()));
    }

    /*
     *9:00-20:00每四小时执行一次
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void task02() {
        System.out.println(Thread.currentThread().getName() + "执行定时任务02 - " + DateFormat.getDateTimeInstance().format(new Date()));
    }
}


/*
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-25 12:40:41.490  INFO 1276 --- [           main] com.toroidal.crontab.CrontabApplication  : Starting CrontabApplication on DESKTOP-3CU3HOD with PID 1276 (E:\work_space\java_work\crontab\target\classes started by Toroidal in E:\work_space\java_work\crontab)
2020-04-25 12:40:41.493  INFO 1276 --- [           main] com.toroidal.crontab.CrontabApplication  : No active profile set, falling back to default profiles: default
2020-04-25 12:40:41.799  INFO 1276 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2020-04-25 12:40:41.810  INFO 1276 --- [           main] com.toroidal.crontab.CrontabApplication  : Started CrontabApplication in 0.552 seconds (JVM running for 0.832)
scheduling-1执行定时任务02 - 2020-4-25 12:40:42
scheduling-1执行定时任务01 - 2020-4-25 12:40:45
scheduling-1执行定时任务02 - 2020-4-25 12:40:48
scheduling-1执行定时任务01 - 2020-4-25 12:40:50
scheduling-1执行定时任务02 - 2020-4-25 12:40:54
scheduling-1执行定时任务01 - 2020-4-25 12:40:55
scheduling-1执行定时任务02 - 2020-4-25 12:41:00
scheduling-1执行定时任务01 - 2020-4-25 12:41:00
scheduling-1执行定时任务01 - 2020-4-25 12:41:05
scheduling-1执行定时任务02 - 2020-4-25 12:41:06
*/
  • cron表达式

设置定时任务排除某段时间

在线生成cron表达式:http://cron.qqe2.com/

  • 异步多线程实现

/**
 * @author Toroidal
 * @date 2020/4/25 11:13
 */
/*
 * 开启定时任务
 * 开启异步执行定时任务
 */
@EnableScheduling
@EnableAsync
@SpringBootApplication
public class CrontabApplication {

    public static void main(String[] args) {
        SpringApplication.run(CrontabApplication.class, args);
    }

    /*
     *9:00-20:00每半小时执行一次
     * 开启异步执行定时任务
     */
    @Async
    @Scheduled(cron = "0/5 * * * * ?")
    public void task01() {
        System.out.println(Thread.currentThread().getName() + "执行定时任务01 - " + DateFormat.getDateTimeInstance().format(new Date()));
    }

    /*
     *9:00-20:00每四小时执行一次
     * 开启异步执行定时任务
     *      */
    @Async
    @Scheduled(cron = "0/6 * * * * ?")
    public void task02() {
        System.out.println(Thread.currentThread().getName() + "执行定时任务02 - " + DateFormat.getDateTimeInstance().format(new Date()));
    }
}


/*
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-25 12:36:12.065  INFO 8600 --- [           main] com.toroidal.crontab.CrontabApplication  : Starting CrontabApplication on DESKTOP-3CU3HOD with PID 8600 (E:\work_space\java_work\crontab\target\classes started by Toroidal in E:\work_space\java_work\crontab)
2020-04-25 12:36:12.068  INFO 8600 --- [           main] com.toroidal.crontab.CrontabApplication  : No active profile set, falling back to default profiles: default
2020-04-25 12:36:12.521  INFO 8600 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2020-04-25 12:36:12.538  INFO 8600 --- [           main] com.toroidal.crontab.CrontabApplication  : Started CrontabApplication in 0.762 seconds (JVM running for 1.128)
2020-04-25 12:36:15.007  INFO 8600 --- [   scheduling-1] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
task-1执行定时任务01 - 2020-4-25 12:36:15
task-2执行定时任务02 - 2020-4-25 12:36:18
task-3执行定时任务01 - 2020-4-25 12:36:20
task-4执行定时任务02 - 2020-4-25 12:36:24
task-5执行定时任务01 - 2020-4-25 12:36:25
task-6执行定时任务01 - 2020-4-25 12:36:30
task-7执行定时任务02 - 2020-4-25 12:36:30

Process finished with exit code -1
*/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Toroidals

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值