quartz2的example1

quartz2的example1java

http://www.quartz-scheduler.org/documentation/quartz-2.2.x/examples/ide

先寫任務jobui

HelloJobcode

package com.imddy.test.quartz2.example1;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloJob implements Job {
	
	private static Logger _log = LoggerFactory.getLogger(HelloJob.class);

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		_log.info("Hello world !!    這個纔是任務調度!!          Now: " + new Date());
	}

}

 

再寫任務調度demoget

package com.imddy.test.quartz2.example1;

import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleExample {

	private void run() throws SchedulerException {
		Logger log = LoggerFactory.getLogger(SimpleExample.class);
		log.info("------- Initializing ----------------------");
		
		//建立一個標準的調度器工廠
		SchedulerFactory sf = new StdSchedulerFactory();
		//從調度器工廠獲取一個調度器
		Scheduler scheduler = sf.getScheduler();
		
		log.info("------- Initialization Complete -----------");
		Date runTime = evenMinuteDate(new Date());
		
		log.info("開始工做時間:" + runTime);
		
		log.info("------- Scheduling Job  -------------------");
		JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
		Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
		
		scheduler.scheduleJob(job, trigger);
		log.info(job.getKey() + " will run at: " + runTime);
		
		scheduler.start();
		log.info("------- 開始調度 -----------------");
		
		log.info("------- 等25秒... -------------");
	    try {
	      // wait 65 seconds to show job
	      Thread.sleep(25L * 1000L);
	      // executing...
	    } catch (Exception e) {
	      //
	    }
	    
	    // shut down the scheduler
	    log.info("------- 調度器關閉 ---------------------");
	    scheduler.shutdown(true);
	    log.info("------- Shutdown Complete -----------------");
		
	}
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
		SimpleExample simpleExample = new SimpleExample();
		simpleExample.run();

	}

}

 

執行以下it

相關文章
相關標籤/搜索