SSM框架整合笔记(十)注解方式配置Quartz定时器

Quartz定时器介紹

Quartz定时器环境配置

  • Quartz 2.3.0

pom.xml 添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
<quartz.version>2.3.0</quartz.version>
<!--Quartz定时器的依赖-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>${quartz.version}</version>
</dependency>
<!--Quartz定时器的依赖-->

定时器调度类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Component
public class MainSchedule {
private static final Logger logger = LoggerFactory.getLogger(MainSchedule.class);

public MainSchedule() {
logger.info("-------------------------定时器MainSchedule创建成功-------------------------");
}

@Scheduled(cron = "0/1 * * * * ?")
public static void run() {
logger.info("-------------------------定时器调度start-------------------------");
logger.info("-------------------------定时器调度end-------------------------");
}
}

spring-quartz.xml配置文件添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

<description>Quartz定时器配置</description>

<context:component-scan base-package="com.weyoung.platform.quartz.schedule"/>
<!--开启@Scheduled注解-->
<task:annotation-driven/>
</beans>

ssm项目从数据库中动态获取定时器配置;

定时任务工具类QuartzManager(主要是对quartz的新增、更改、关闭等)