码迷,mamicode.com
首页 > 编程语言 > 详细

Spring集成Quartz定时任务

时间:2020-03-31 20:59:44      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:let   cront   自动   str   package   href   tail   指定   jar   

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定时任务

标签:let   cront   自动   str   package   href   tail   指定   jar   

原文地址:https://www.cnblogs.com/charlottepl/p/12608065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!