首页 > 代码库 > Spring定时器Quartz的用法
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;@Configurationpublic 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@Transactionalpublic 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执行,定时的配置信息在以上的第二步的类中。
Spring定时器Quartz的用法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。