标签:
Quartz中提供了两种触发器,分别是CronTrigger和SimpleTrigger。
1. SimpleTrigger
每隔若干毫秒来触发纳入进度的任务。
2. CronTrigger
在特定“格林日历”时刻触发纳入进程的任务。
调度程序如下:
public class TestQuartz { public void myTask() throws Exception { String tim = FileProperties.getMyProperty("Socket_TIM_ZZH"); System.out.println(tim); //定时器任务定义 Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); //定时器任务 JobDetail testJob = new JobDetail("testJob", Scheduler.DEFAULT_GROUP, TestTask.class);
//CronTrigger触发器 //Trigger trigger = new CronTrigger("testCronTrigger", Scheduler.DEFAULT_GROUP, tim.trim()); //结束时间 long end = System.currentTimeMillis() + 9000L; //执行10次,每3秒执行一次,到9秒后结束 SimpleTrigger trigger = new SimpleTrigger("test",null,new Date(),new Date(end),10,3000L); // 定时器任务监听 scheduler.scheduleJob(testJob, trigger); } }
任务执行程序如下:
public class TestTask extends Thread implements Job{ //private static Logger logger = Logger.getLogger(TestTask.class.getName()); private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private Date today = null; public TestTask() { Calendar rightNow = Calendar.getInstance(); rightNow.add(Calendar.DATE,-1); this.today = rightNow.getTime(); } @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { try { System.out.println("testTask:" + format.format(this.today) + "------------------------------------"); //logger.info("testTask:" + format.format(this.today) + "------------------------------------"); } catch (Exception e) { e.printStackTrace(); //logger.error("error:" + e.getMessage()); } } }
main函数:
public static void main(String[] args) { try { TestQuartz testQuartz = new TestQuartz(); testQuartz.myTask(); }catch(Exception e) { e.printStackTrace(); } }
除了引入quartz包,还要使用Commons-logging.jar
标签:
原文地址:http://www.cnblogs.com/fengyunyue/p/5951622.html