quartz 定时任务, jeesite里面用的(虽然网上一大堆,不过自己用的还是记一下吧)

首先在自己项目的pom.xml里添加jar依赖


		
			org.quartz-scheduler
			quartz
			2.2.1
		
		
			org.quartz-scheduler
			quartz-jobs
			2.2.1
		
		

然后建quartz的配置文件,一般命名方式为applicationContext-quartz.xml



quartz Configuration 



  

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

        

 

在spring配置文件applicationContext.xml里引入quartz的配置文件


	

 

package com.thinkgem.jeesite.modules.hg_project_code.utils;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Service;

@Service("jobFactory")
public class JobFactory extends AdaptableJobFactory {

	@Autowired
	private AutowireCapableBeanFactory capableBeanFactory;

	@Override
	protected Object createJobInstance(TriggerFiredBundle bundle)
			throws Exception {
		Object jobInstance = super.createJobInstance(bundle);
		capableBeanFactory.autowireBean(jobInstance);
		return jobInstance;
	}

}

编写自己的业务方法,这个是通告过期后修改状态用的

package com.thinkgem.jeesite.modules.hg_project_code.web.hrManage.inform;


import java.text.ParseException;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import com.thinkgem.jeesite.modules.hg_project_code.service.hrManage.inform.HgRlglInformService;
/**
 * 定时设置通知任务 状态
 * 
 * @author itcast
 *
 */
public class PromotionJob implements Job {
	@Autowired
	private HgRlglInformService hgRlglInformService;

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		// 每分钟执行一次,当前时间 大于 promotion数据表 endDate, 活动已经过期,设置status='2'
		try {
			hgRlglInformService.execute(context);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 

你可能感兴趣的:(成长历程)