quartz 動態配置任務

package com.sanss.monitor.server.task;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.DateBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.triggers.CalendarIntervalTriggerImpl;
import org.springframework.stereotype.Service;

import com.sanss.monitor.service.JobTask;


@Service
public class TableSchedulerImpl implements TaskScheduler  {
	
	static JobTask jobTask;//事物類,當前類初始化時帶註解bean還沒有裝載完成,所以用set注入

	private static Log logger = LogFactory.getLog(TableSchedulerImpl.class);
	
	static Scheduler scheduler;		
	
	public TableSchedulerImpl(){
		try {
			if(scheduler==null){
				scheduler = new org.quartz.impl.StdSchedulerFactory().getScheduler();
				scheduler.start();
			}
			ArrayList list=this.getAllTables();
			for(int i=0;i<list.size();i++){
				this.addTask(list.get(i));//項目啓動時就添加任務
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}	
	
	@Override
	public void addTask(Object task) {
		HashMap map=(HashMap)task;
		JobDetailImpl job = new  JobDetailImpl();
		JobKey key = JobKey.jobKey("task_"+map.get("table").toString(), Scheduler.DEFAULT_GROUP);
		job.setKey(key);
		job.setJobClass(TableSchedulerImpl.class);
		job.getJobDataMap().put("table", map.get("table").toString());
		job.getJobDataMap().put("column", map.get("column").toString());
		try {
			if(scheduler.getJobDetail(key)==null){
				//週期執行
				CalendarIntervalTriggerImpl trigger = new CalendarIntervalTriggerImpl();
				trigger.setRepeatIntervalUnit(DateBuilder.IntervalUnit.SECOND);
				trigger.setRepeatInterval(5);
				trigger.setName("tir_"+map.get("table").toString());
				trigger.setStartTime(new Date());
				try {
					scheduler.scheduleJob(job, trigger);
				} catch (SchedulerException e) {
					e.printStackTrace();
				}
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void pauseTask(String taskKey) {//暫停job
		try {
			if(scheduler.checkExists(JobKey.jobKey("task_"+taskKey,Scheduler.DEFAULT_GROUP))){
				scheduler.pauseJob(JobKey.jobKey("task_"+taskKey,Scheduler.DEFAULT_GROUP));								
			}
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void removeTask(String taskKey) {
		try {
			if(scheduler.checkExists(JobKey.jobKey("task_"+taskKey,Scheduler.DEFAULT_GROUP))){
				System.out.println("removeTask->"+taskKey);
				scheduler.deleteJob(JobKey.jobKey("task_"+taskKey,Scheduler.DEFAULT_GROUP));
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void resumeTask(String taskKey) {//恢復job
		try {
			if(scheduler.checkExists(JobKey.jobKey("task_plan_"+taskKey,Scheduler.DEFAULT_GROUP))){
				scheduler.resumeJob(JobKey.jobKey("task_plan_"+taskKey,Scheduler.DEFAULT_GROUP));				
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		
		try {
			JobDataMap map = context.getJobDetail().getJobDataMap();			
			System.out.println(map.get("table"));
			jobTask.startJob(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ArrayList getAllTables(){
		ArrayList list=new ArrayList();
		Properties property = new Properties();
		InputStream is;
		try {
			String path = TableSchedulerImpl.class.getResource("/").getPath();
            path = path.substring(1, path.indexOf("classes"));
			//D:/apache-tomcat-6.0.35/webapps/monitorSys/WEB-INF/
			is = new FileInputStream(new File(path + "/tableCheck.properties"));
			property.load(is);
			is.close();
			String[] tables=(property.getProperty("table")).split(",");
			String[] columns=(property.getProperty("column")).split(",");
			String[] times=(property.getProperty("time")).split(",");
			for(int i=0;i<tables.length;i++){
				HashMap map=new HashMap();
				map.put("table", tables[i]);
				map.put("column", columns[i]);
				map.put("time", times[i]);
				list.add(map);
			}
		} catch (FileNotFoundException e1) {
			logger.info("找不到配置文件");
			e1.printStackTrace();
		} catch (IOException e) {
			logger.info("讀取配置文件失敗");
			e.printStackTrace();
		}
		
		return list;
	}

	public JobTask getJobTask() {
		return jobTask;
	}

	public void setJobTask(JobTask jobTask) {
		this.jobTask = jobTask;
	}

	
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章