多线程 quartz(入门)


 
package others;

import static org.quartz.DateBuilder.evenSecondDateAfterNow;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

/**
 * quartz 学习入门
 * @author
 */
public class TestQuartz {
  public void run() throws Exception {
    //1、创建 Scheduler的工厂
    SchedulerFactory sf = new StdSchedulerFactory();
    //2、从工厂中获取调度器
    Scheduler sched = sf.getScheduler();

    //3、创建JobDetail
    JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

    //时间
    Date runTime = evenSecondDateAfterNow();
   
    //4、触发器
//  Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
    Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime)
    		.withSchedule(simpleSchedule().withIntervalInSeconds(5).withRepeatCount(2)).build();

    //5、注册任务和触发条件
    sched.scheduleJob(job, trigger);

    //6、启动
    sched.start();

    try {
      //5秒后停止
      Thread.sleep(50L * 1000L);
      // executing...
    } catch (Exception e) {
    }

    sched.shutdown(true);
}

public static void main(String[] args) throws Exception {
    TestQuartz example = new TestQuartz();
    example.run();
  }
}

package others;

import java.util.Date;

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

public class HelloJob implements Job {
	public HelloJob() {
		
	}

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println("-----------Start----------");
		System.out.println("Hello World!-"+new Date());
		System.out.println("---------end---------");
	}
	
	
}

你可能感兴趣的:(多线程)