java定时器结合springboot_springBoot中使用定时任务

简单示例

导入依赖

springBoot已经默认集成了定时任务的依赖,只需要引入基本的依赖就可以使用定时任务。

org.springframework.boot

spring-boot-parent

2.0.0.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

启动类配置

在启动类中需要加入@EnableScheduling注解,意思是开启定时任务。

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.scheduling.annotation.EnableScheduling;

/**

* Author: YaoQi

* Date: 2018/9/28 21:37

* Description: springBoot schedule

*/

@SpringBootApplication

@EnableScheduling

public class ScheduleApp {

public static void main(String[] args) {

SpringApplication.run(ScheduleApp.class, args);

}

}

定时任务Demo

写一个定时任务demo,每秒种打印一次日志,并打印当前时间验证当前任务执行周期。

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Service;

/**

* Author: YaoQi

* Date: 2018/9/28 21:40

* Description: Scheduled Task

*/

@Service

public class ScheduleTask {

private static final Logger logger = LoggerFactory.getLogger(ScheduleTask.class);

@Scheduled(cron = "*/1 * * * * ?")

public void execute() {

logger.info("print word.");

logger.info(String.valueOf(System.currentTimeMillis()));

}

}

运行结果:

df8d7f229e1d856b7f45066a10beb469.png

示例分析

从上图的结果中看:该任务基本是每秒种执行一次,如果不手动停止,程序会一直执行下去,并且从日志中看,这个任务的执行周期是1s左右,刚好和设置的cron表达式一致;并且执行这个任务的线程一直是poll-1-thread-1这个线程。这就意味着,这个定时任务启动是由单独的一个线程去执行的。

这时候,可能会有几个问题:

如果任务执行的时间比执行周期要短,这个任务会怎么执行?

如果有多个任务执行,还会是一个线程去执行这个任务吗?

首先验证第一个问题,当任务执行的时间比执行周期短时,任务的执行情况。

@Scheduled(cron = "*/1 * * * * ?")

public void execute() {

logger.info("print word.");

logger.info(String.valueOf(System.currentTimeMillis()));

try {

Thread.sleep(6000L);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

执行结果:每次任务的执行时间间隔为7秒,并且是同一个线程在执行。

d88be7cff3c5b85ac8a6f1ffc53b938b.png

如果有多个任务执行,任务的执行情况。

@Scheduled(cron = "*/1 * * * * ?")

public void execute() {

logger.info("print word.");

try {

Thread.sleep(6000L);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

@Scheduled(cron = "*/1 * * * * ?")

public void execute1() {

logger.info(String.valueOf(System.currentTimeMillis()));

logger.info("write message.");

}

执行结果:

7cc17535ebe5989222af0319a0239d79.png

执行结果:第二个每秒执行一次的任务的并没有安装设定的执行周期执行,运行结果并没有达到预期。并且两个任务都是由同一个线程去运行。

多任务模式

示例分析中问题2的原因就是执行多个任务并不是多个线程执行,导致执行第一个任务时,第二个任务进入等待状态。springBoot中提供了多线程运行定时任务的方式。

@Configuration

public class ScheduleConfig implements SchedulingConfigurer {

@Override

public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

taskRegistrar.setScheduler(Executors.newScheduledThreadPool(3));

}

}

增加一个配置类,设置线程池,并设置线程池大小,这里的线程池大小就仁者见仁了,和你程序中的任务的个数有关系,也和机器的核数有关系。

通过配置线程池就能让每个任务独立执行,不受其他任务的影响,因为是在不同的线程中执行的,但如果涉及到公共资源就另当别论了。

执行结果:

00495aa3ab2fbe15caa12d44befca67e.png

配置执行周期

上述中的执行周期都是以cron表达式定义的,这种方式最灵活,但是上文中的表达式都写到代码中去了,不便于修改,这里提供一种配置方式,直接用表达式获取yml中的配置信息。

scheduleTask:

cron1: "*/1 * * * * ?"

cron2: "*/1 * * * * ?"

在yml中添加cron的配置信息。然后在Java注解中可以直接获取。

@Scheduled(cron = "${scheduleTask.cron2}")

public void execute1() {

logger.info(String.valueOf(System.currentTimeMillis()));

logger.info("write message.");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值