标签:
首先导入需要的两个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执行,定时的配置信息在以上的第二步的类中。
标签:
原文地址:http://www.cnblogs.com/007sx/p/5957411.html