• 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

  • 相关阅读:
    不要随便用#define 沉沉_
    转载堆和栈的区别 沉沉_
    转载include包含源文件引发的错误 沉沉_
    浅议C和C++中的内存分配 沉沉_
    volatile 的应用 沉沉_
    Debian显示txt乱码
    Debian 64位安装wink
    Debian安装vmware虚拟机
    Debian安装scim中文、日语输入法
    Debian安装chrome
  • 原文地址:https://www.cnblogs.com/charlottepl/p/12608065.html
Copyright © 2020-2023  润新知