博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring定时器Quartz的用法
阅读量:6589 次
发布时间:2019-06-24

本文共 4084 字,大约阅读时间需要 13 分钟。

首先导入需要的两个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;@Configurationpublic class QuartzConfiguration {    @SuppressWarnings("unused")    private final Logger log = LoggerFactory.getLogger(getClass());    @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.textile.quartz;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;/** * Created by Administrator on 2016/12/4. */@Service@Transactionalpublic class WorkorderCheckJob extends QuartzJobBean{    @SuppressWarnings("unused")    private final Logger log = LoggerFactory.getLogger(getClass());    private static boolean isRun = false;    protected void executeInternal(JobExecutionContext executionContext) throws JobExecutionException {        if (isRun) {            System.out.println("前一次未执行完,跳过本次任务!");            return;        }        isRun = true;        task(executionContext);        isRun = false;        System.out.println("执行逻辑-isRunFinish");    }    private void task(JobExecutionContext executionContext) {        try {            ApplicationContext applicationContext = (ApplicationContext) executionContext.getScheduler().getContext().get("applicationContext");            //AppInstanceService appInstanceService = (AppInstanceService)applicationContext.getBean(AppInstanceService.class);            //appInstanceService.clearInstanceTime();        } catch (BeansException e) {            e.printStackTrace();        } catch (SchedulerException e) {            e.printStackTrace();        }        catch (Exception e) {            e.printStackTrace();        }    }}

 

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

转载于:https://www.cnblogs.com/007sx/p/5957411.html

你可能感兴趣的文章
XIB的是用
查看>>
Learning Data Structure_2_线性表、栈和队列
查看>>
驱动外置+原版安装方式『XLOS_Windows8_Pro_X86纯净版_V1.0』
查看>>
Oracle创建表语句(Create table)语法详解及示例
查看>>
Java基础之Http协议的理解与总结
查看>>
Android Arcface人脸识别sdk使用工具类
查看>>
android studio单个工程文件的代理设置
查看>>
Agent admitted failure to sign using the key
查看>>
grep 应用
查看>>
我的友情链接
查看>>
Linux实验室 CentOS关机大法
查看>>
一行命令获取当前JVM所有可设置的参数以及当前默认值
查看>>
spring与struts2 mvc共存web.xml简单配置
查看>>
2015年终总结
查看>>
Python web爬虫
查看>>
Python捕捉命令输出、错误输出及赋值命令到变量的方法
查看>>
js解析json
查看>>
详解性能调优命令
查看>>
Linux mint 14下的powerDNS+mysql+powerAdmin搭建个性DNS域名解析服务器
查看>>
Red Hat EnterPrise Linux 5.4下web服务器的综合使用(普通站点、虚拟主机、安全性、...
查看>>