java:关于Quartz定时器的使用详解

1.在pom.xml中添加maven依赖

<!--Quartz任务调度 -->
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>

2.在classpath路径下配置quartz-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    default-lazy-init="false">
    <bean id="WhatJob" class="com.suriot.web.washCar.service.WhatJob" />
    <bean id="rsh_jobDetail_1"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="WhatJob" />
        </property>
        <property name="targetMethod">
            <value>execute</value><!-- 方法名称 -->
        </property>
    </bean><!-- CronTrigger——调度触发 使用cron表达式 -->
    <bean id="rsh_cronTrigger_1"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="rsh_jobDetail_1" />
        <property name="cronExpression">
            <value>0 03 17 11 * ?</value>
            <!-- <value>0 0 0 * * ? *</value> --><!-- 每天晚上0点执行 -->
        </property>
    </bean>
    <bean id="startQuertz"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list><!-- <ref bean="rsh_simpleTrigger1" /> -->
                <ref bean="rsh_cronTrigger_1" />
            </list>
        </property>
    </bean>
</beans>

3.配置web.xml中quartz-config.xml的文件路径

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml,classpath:quartz-config.xml</param-value>
    </context-param>

4.建立执行文件WhatJob.java

package com.suriot.web.washCar.service;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suriot.Pay.util.ExDateUtil;

@Service
public class WhatJob {

    public void execute() {
        try {
            System.out.println("执行时间 : " + ExDateUtil.ExDate() + ",执行事件:");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

上面这种事随着项目启动定时器自启动的,还有一种如果是需要触发执行的话,参考如下,需要注意的是,只能触发一次,然后就失效了,触发第二次这个任务会抛异常告诉你任务被占用过期了。

WahtJob.class和自启动是有一些不一样的,对比参考下

package com.suriot.web.washCar.service;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suriot.Pay.util.ExDateUtil;

@Service
public class WhatJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            System.out.println("执行时间 : " + ExDateUtil.ExDate() + ",执行事件:");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
package com.suriot.web.washCar.util;

import java.text.ParseException;

import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.suriot.web.washCar.service.WhatJob;

/**
 * 工作类的具体实现,即需要定时执行的“某件事”
 */
public class Quartz {

    public static void Times() throws ParseException, SchedulerException {
        // 任务名称
        String jobName = "times";
        // 任务组
        String jobGroup = "times";

        // 构建JobDetail
        JobDetail jobDetail = JobBuilder.newJob(WhatJob.class).withIdentity(jobName, jobGroup).build();

        // 触发名称
        String triName = "times";
        // 出发表达式
        String triPress = "0 00 00 1 * ?";// 每月1日00:00分运行。
        // eg:"0 30 10 15 * ?" 每月15日10:30分运行。
        CronExpression express = new CronExpression(triPress);
        // 构建触发器
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triName, jobGroup).startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(express)).build();

        // 创建调度器(Scheduler)
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        // 注册调度器(Scheduler)
        sched.scheduleJob(jobDetail, trigger);

        // 启动调度器(Scheduler)
        sched.start();

    }


}

关于定时器要用到的cron表达式参考链接:http://www.what21.com/sys/view/java_java-frame_1478840380252.html,可以试着执行几次摸索一下规律。

个人见解,首篇博文欢迎指教,如有疑问或是侵权什么的,可以邮箱留言 [email protected]

猜你喜欢

转载自www.cnblogs.com/divingCat/p/12066183.html