Spring定时器Quartz的用法

首先导入需要的两个jar:

spring-context-support-4.1.1.RELEASE.jar

quartz-2.2.1.jar

1.创建两个类:

2.

QuartzConfiguration:

package com.baibeiyun.yunbang.common.webservice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfiguration {

    @SuppressWarnings("unused")
    private final Logger log = LoggerFactory.getLogger(QuartzConfiguration.class);

    @Bean
    public SchedulerFactoryBean schedulerFactory() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean().getObject());
        schedulerFactoryBean.setTaskExecutor(threadPoolTaskExecutor());
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
        return schedulerFactoryBean;
    }
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean().getObject());
        cronTriggerFactoryBean.setCronExpression("0 0/1 * * * ?");
        //cronTriggerFactoryBean.setCronExpression("0/5 * * * * ?");
        return cronTriggerFactoryBean;
    }
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(WorkorderCheckJob.class);
        jobDetailFactoryBean.setDurability(true);
        return jobDetailFactoryBean;
    }
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(10);
        threadPoolTaskExecutor.setMaxPoolSize(20);
        threadPoolTaskExecutor.setQueueCapacity(5);
        threadPoolTaskExecutor.setKeepAliveSeconds(100);
        return threadPoolTaskExecutor;
    }

}

3.WorkorderCheckJob:

package com.baibeiyun.yunbang.common.webservice;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baibeiyun.yunbang.driver.webservice.DrYbDriverController;

/**
 * Job
 *
 * @author
 * @version 3.0
 */
@Service
@Transactional
public class WorkorderCheckJob extends  QuartzJobBean {//  implements Job {

    @SuppressWarnings("unused")
    private final Logger log = LoggerFactory.getLogger(WorkorderCheckJob.class);

    protected void executeInternal(JobExecutionContext executionContext) throws JobExecutionException {
        System.out.println("定时器运行中");
        try {
            ApplicationContext applicationContext = (ApplicationContext) executionContext.getScheduler().getContext().get("applicationContext");

            //AppInstanceService appInstanceService = (AppInstanceService)applicationContext.getBean(AppInstanceService.class);
            //appInstanceService.clearInstanceTime();

            DrYbDriverController drYbDriverController = (DrYbDriverController)applicationContext.getBean(DrYbDriverController.class);
            Integer statusCode = drYbDriverController.addLineCarLngLatToDb();
            if (statusCode==0) {
                System.out.println("缓存数据存入数据库成功");
            }else{
                System.out.println("缓存数据存入数据库出错");
            }    

        } catch (BeansException e) {
            e.printStackTrace();

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

4.定时的任务在第三步的类中的executeInternal执行,定时的配置信息在以上的第二步的类中。

时间: 2025-01-14 16:33:08

Spring定时器Quartz的用法的相关文章

spring定时器quartz版本问题

如果quartz的版本是1.8.5启动会报错,修改给2.0版本以上即可 <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.0</version> </dependency>

Spring中Quartz调度器的使用

一.Quartz的特点 * 按作业类的继承方式来分,主要有以下两种: 1.作业类继承org.springframework.scheduling.quartz.QuartzJobBean类的方式 2.作业类不继承org.springframework.scheduling.quartz.QuartzJobBean类的方式 注:个人比较推崇第二种,因为这种方式下的作业类仍然是POJO. * 按任务调度的触发时机来分,主要有以下两种: 1.每隔指定时间则触发一次,对应的调度器为org.springf

Spring中Quartz定时器

Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在Spring中怎样配置Quartz: 首先我们来写一个被调度的类: package com.kay.quartz; public class QuartzJob { public void work()      {      System.out.println("Quartz的任务调度!!!");      } } Spring的配置文件: <?xml version="

Spring定时器配置的两种实现方式OpenSymphony Quartz和java Timer详解

原创整理不易,转载请注明出处:Spring定时器配置的两种实现方式OpenSymphony Quartz和java Timer详解 代码下载地址:http://www.zuidaima.com/share/1772648445103104.htm 有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz. 1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 import java.util.TimerTask; p

spring启动quartz定时器

在很多中经常要用到定时任务,quartz是定时器中比较好用的,在Spring中使用quartz是很容易的事情,首先在spring的applicationContext.xml文件中增加如下配置: <!-- quartz定时器定义 --> <!-- 要调用的工作类,即任务处理类 --> <bean id="quartzJob" class="com.mdnet.travel.core.model.QuartzJob"></be

Spring的quartz定时器重复执行二次的问题解决

Spring的quartz定时器同一时刻重复执行二次的问题解决 最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候,重复启了两个quartz线程. 研究下来发现quartz确实会加载两次: 第一次:web容器启动的时候,读取applicationContext.xml文件时,会加载一次. 第二次:Spring本身会加载applicationConte

Spring整合Quartz实现动态定时器

一 : 首先了解spring定时器所使用到的场景 比如 : 定时统计报表,或者登录账户密码验证失败冻结账户后,每晚12点将账户解锁等等... 使用jar包 : 2.0使用方法不同 <!-- 定时器 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.6</versio

两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz

1.Java Timer定时 首先继承java.util.TimerTask类实现run方法 import java.util.TimerTask; public class EmailReportTask extends TimerTask{ @Override public void run() { ... } } 在Spring定义 ... 配置Spring定时器 <bean id="scheduleReportTask" class="org.springfra

Spring集成Quartz定时器

<!-- Spring集成Quartz开始 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list>