依赖
<properties>
<spring.version>4.3.22.RELEASE</spring.version>
</properties>
<!-- quartz定时任务 -->
<dependencys>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
</dependencys>
配置springmvc.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd" >
<!-- 访问静态资源 -->
<mvc:default-servlet-handler />
<!-- 扫描controller -->
<context:component-scan base-package="sys.cdsoft.*"/>
<!-- 定时任务 -->
<task:annotation-driven scheduler="myScheduler" proxy-target-class="true"/>
<task:scheduler id="myScheduler" pool-size="5"/>
<context:annotation-config/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<context:component-scan base-package="sys.cdsoft.ScheduledImpl"/>
<!-- 定时器工作调度的bean -->
<bean id="myJob" class="sys.cdsoft.quartz.job.Captures_job" />
<bean id="myJob1" class="sys.cdsoft.quartz.job.Captures_job2" />
<!-- job的配置开始 -->
<bean id="myJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob" />
</property>
<property name="targetMethod">
<value>get_captures_list</value>
</property>
</bean>
<bean id="myJobDetail2"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="myJob1" />
</property>
<property name="targetMethod">
<value>run</value>
</property>
</bean>
<!-- 调度的配置开始 -->
<bean id="crontestJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean>
<!-- 调度的配置开始 -->
<bean id="crontestJobTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail2" />
</property>
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean>
<!-- quartz定时任务 -->
<bean id="startQuartz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="crontestJobTrigger" />
<ref bean="crontestJobTrigger2" />
</list>
</property>
</bean>
<bean id="quartzManager" class="sys.cdsoft.quartz.QuartzManager" lazy-init="false" init-method="startJobs" >
<!--这个对象一定要注入,这样类才能进行管理,还有在类型要用get set方法,不然会报错。-->
<property name="scheduler" ref="startQuartz" />
</bean>
</beans>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5ee355bce06c21c4d23b0b4ebcb1a9c6.png#pic_center)
定义QuartzManager.java
package sys.cdsoft.quartz;
import org.apache.commons.lang.StringUtils;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
public class QuartzManager {
private Scheduler scheduler;
@SuppressWarnings({ "unchecked", "rawtypes" })
public void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass,
String cron) {
try {
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
triggerBuilder.withIdentity(triggerName, triggerGroupName);
triggerBuilder.startNow();
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
scheduler.scheduleJob(jobDetail, trigger);
if (!scheduler.isShutdown()) {
scheduler.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void modifyJobTime(String jobName, String jobGroupName, String triggerName, String triggerGroupName,
String cron) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(cron)) {
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
triggerBuilder.withIdentity(triggerName, triggerGroupName);
triggerBuilder.startNow();
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
trigger = (CronTrigger) triggerBuilder.build();
scheduler.rescheduleJob(triggerKey, trigger);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
scheduler.pauseTrigger(triggerKey);
scheduler.unscheduleJob(triggerKey);
scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void startJobs() {
try {
scheduler.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void shutdownJobs() {
try {
if (!scheduler.isShutdown()) {
scheduler.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Scheduler getScheduler() {
return scheduler;
}
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public String transCron(String time) {
String seconds = StringUtils.substringAfterLast(time, ":");
String minute = StringUtils.substringAfter(time, ":").substring(0, 2);
String hour = StringUtils.substringAfter(time, " ").substring(0, 2);
String day = StringUtils.substringAfterLast(time, "-").substring(0, 2);
String month = StringUtils.substringAfter(time, "-").substring(0, 2);
return seconds + " " + minute + " " + hour + " " + day + " " + month + " ?";
}
}
定义job1
package sys.cdsoft.quartz.job;
import java.util.List;
import javax.annotation.Resource;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import sys.cdsoft.quartz.QuartzManager;
public class Captures_job implements Job {
@Resource
private QuartzManager quartzManager;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
get_captures_list();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings({ "static-access" })
public void get_captures_list() throws Exception {
System.out.println("kaishizhixing ======");
}
}
定义job2
package sys.cdsoft.quartz.job;
import java.util.List;
import javax.annotation.Resource;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import sys.cdsoft.quartz.QuartzManager;
public class Captures_job2 implements Job {
@Resource
private QuartzManager quartzManager;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
run();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings({ "static-access" })
public void run() throws Exception {
System.out.println("开一 ======");
}
}
运行效果展示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c0c9a4669b0d32dad1bf957736c2cd8a.png#pic_center)