1.导入jar包
2.配置applicationContext.xml文件
<!-- 任务调度1 -->
<!-- bean id="simpleJob" class="com.shprj.quartz.SimpleJob"></bean>
<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="simpleJob"></property>
<property name="targetMethod" value="showTime"></property>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="simpleJobDetail" />
<property name="startDelay" value="1000" />
<property name="repeatInterval" value="2000" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="simpleJobDetail"/>
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
</list>
</property>
</bean-->
<!-- 任务调度2 -->
<!-- bean id="firstJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.shprj.quartz.FirstJobDetail"></property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="firstJobDetail" />
<property name="cronExpression" value="0/5 * * ? * *" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean-->
<!-- 任务调度3 注解 -->
<task:annotation-driven />
一共三种配置方法
1.
applicationContext.xml:
//simpleJob指定任务操作
< bean id="simpleJob" class="com.shprj.quartz.SimpleJob"></bean>
<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="simpleJob"></property>
<property name="targetMethod" value="showTime"></property>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="simpleJobDetail" />
<property name="startDelay" value="1000" />开始时间
<property name="repeatInterval" value="2000" />间隔时间
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="simpleJobDetail"/>
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
</list>
</property>
</bean>
simpleJob.java:
package com.shprj.quartz;
import java.util.Date;
public class SimpleJob {
public void showTime(){
System.out.println("简单任务1:"+new Date().toLocaleString());
}
}
Tomcat启动就会自动执行
2.
applicationContext.xml:
<bean id="firstJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.quartz.FirstJobDetail"></property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="firstJobDetail" />
<property name="cronExpression" value="0 0 20 * * ?" />这里写cron表达式
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
firstJobDetail.java:
package com.quartz;
import java.util.Date;
import com.dao.PianoRecordDao;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class FirstJobDetail extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
PianoRecordDao pianoRecordDao = new PianoRecordDao();
int i = pianoRecordDao.insert();
System.out.println("Quartz任务执行!");
}
}
Tomcat启动自动执行
3.没看,,以后写
参考:https://blog.csdn.net/noaman_wgs/article/details/80984873