Spring集成Quartz定时任务

Posted 两小无猜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring集成Quartz定时任务相关的知识,希望对你有一定的参考价值。

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

以上是关于Spring集成Quartz定时任务的主要内容,如果未能解决你的问题,请参考以下文章

Spring集成Quartz定时任务

Spring Boot集成quartz实现定时任务并支持切换任务数据源

spring集成quartz实现定时任务

SpringBoot系列:Spring Boot集成定时任务Quartz

SpringBoot系列:Spring Boot集成定时任务Quartz

使用Quartz框架集成Spring,动态配置定时任务(个人思考)