可参考博主:https://blog.csdn.net/earl_yuan/article/details/50668864
源代码地址(Free): https://download.csdn.net/upload/success
1.此次整合项目所需要的jar包
2.spring-quartz.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:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
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/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd">
<!--使用简单作业详情-->
<!-- 使用MethodInvokingJobDetailFactoryBean来创建作业对象 -->
<bean id="mySimpleJob" class="cn.wz.job.MySimpleJob"/>
<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--配置简单作业的目标类-->
<property name="targetObject" ref="mySimpleJob"/>
<!--配置简单作业的目标方法-->
<property name="targetMethod" value="execute"/>
<!--false表示等上一个任务执行完后再开启新的任务-->
<property name="concurrent" value="false"/>
</bean>
<!--使用定时作业详情-->
<bean id="cronJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!--配置简单作业的目标类-->
<property name="jobClass" value="cn.wz.job.MyCronJob"/>
<!--配置耐久性,如果一个任务不是durable,那么当没有Trigger关联它的时候,它就会被自动删除-->
<property name="durability" value="true"/>
</bean>
<!--配置简单触发器-->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="simpleJobDetail"/>
<!-- 延迟3s执行 -->
<property name="startDelay" value="3000"/>
<!-- 每隔10s执行一次 -->
<property name="repeatInterval" value="10000"/>
</bean>
<!--配置定时触发器-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="cronJobDetail"/>
<!--每隔5s执行一次-->
<property name="cronExpression" value="*/5 * * * * ?"/>
</bean>
<!--配置调度器工厂-->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!--配置触发器-->
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
3.定时任务的:MyCronJob.xml
package cn.wz.job;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* 定时任务
*/
public class MyCronJob extends QuartzJobBean {
private static int times = 0;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String time = sdf.format(new Date());
System.out.println(" MyCroJob: [" + time + "] 定时任务 第[" + ++times + "]次执行……");
}
}
4.简单任务的:MySimpleJob.xml
package cn.wz.job;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;
/**
* 简单任务
*/
@Component("mySimpleJob")
public class MySimpleJob {
private static int times = 0;
public void execute() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String time = sdf.format(new Date());
System.out.println("MySimpleJob: [" + time + "] 简单任务 第[" + ++times + "]次执行……");
}
}
5.测试代码:TestMain.java (注意用单元测试 JobTest 测试jar包问题)
package Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
System.out.println("Job start");
ApplicationContext springContext = new ClassPathXmlApplicationContext("classpath*:spring-quartz.xml");
springContext.getBean("schedulerFactoryBean");
System.out.println("Job end");
}
}
6.运行结果(TestMain.java)