当前位置 : 主页 > 编程语言 > c++ >

quartz 通用的多线程定时任务

来源:互联网 收集:自由互联 发布时间:2021-06-30
TaskManager package mytest.task;import java.text.ParseException;import org.quartz.CronTrigger;import org.quartz.Job;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.impl.StdSched
TaskManager
package mytest.task;

import java.text.ParseException;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 
 * 
 * @author luke Lin
 * @version 1.0.0 2017年8月9日 上午9:37:33
 */
public class TaskManager {
	
	public static 
 
   void insertTask(String jobName,String expression,Class
  
    jobCalss){ try { Scheduler schedule = StdSchedulerFactory.getDefaultScheduler(); CronTrigger cronTrigger = new CronTrigger(jobName, Scheduler.DEFAULT_GROUP,expression); JobDetail jobDetail = new JobDetail(jobName,Scheduler.DEFAULT_GROUP,jobCalss); schedule.scheduleJob(jobDetail, cronTrigger); schedule.start(); } catch (SchedulerException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } }
  
 
BastTask
package mytest.task;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;


/**
 * @author luke Lin
 * @version 1.0.0 2017年8月9日 上午9:40:43
 */
public abstract class BastTask implements Job{

	private static int taskCount = 0;
	
	public static 
 
   void init(String jobName,String expression,Class
  
    jobCalss){ TaskManager.insertTask(jobName, expression, jobCalss); } public void execute(JobExecutionContext context) throws JobExecutionException { long startTime = System.currentTimeMillis(); int currentCount = taskCount++; System.out.println(context.getJobDetail().getFullName() + "-" + currentCount + " 定时任务开始执行"); new Thread(new Runnable() { public void run() { doTask(); } }).start(); long endTime = System.currentTimeMillis(); System.out.println(context.getJobDetail().getFullName() + "-" + currentCount + " 定时任务执行完成,耗时:" + (endTime - startTime) + "毫秒"); } public abstract void doTask(); }
  
 
编码自己任务类
package mytest.task.choice;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import mytest.task.BastTask;

/**
 * 
 * 
 * @author luke Lin
 * @version 1.0.0 2017年8月9日 上午9:50:47
 */
public class DefaultTask extends BastTask{

	private ThreadPoolExecutor cancelPoolExecutor = new ThreadPoolExecutor(10, 100, 0L, TimeUnit.SECONDS,new LinkedBlockingQueue
 
  ()); 

	@Override
	public void doTask() {
		//定时任务自己的任务业务
		System.out.println("我要执行我的任务了!");
		
		for(int i=0;i<10;i++){
			cancelPoolExecutor.submit(new CancelOrderRunnable());
			System.out.println("[cancelPoolExecutor] : " + cancelPoolExecutor.toString());
		}
	}
	
	public static void main(String[] args) {
		DefaultTask.init("DefaultTask", "0/1 * * * * ?", DefaultTask.class);
	}
	
	/**
	 * 线程取消
	 * @author Jews
	 */
	public class CancelOrderRunnable implements Runnable {

		public void run() {
			System.out.println("这里是我的业务逻辑");
			try {
				throw new Exception();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
 
网友评论