public void run() throws Exception {
    // 通过SchedulerFactory获取一个调度器实例
    SchedulerFactory sf = new StdSchedulerFactory();

    Scheduler sched = sf.getScheduler();

    Date runTime = evenMinuteDate(new Date());

    // 通过过JobDetail封装HelloJob,同时指定Job在Scheduler中所属组及名称,这里,组名为group1,而名称为job1。
    JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

    // 创建一个SimpleTrigger实例,指定该Trigger在Scheduler中所属组及名称。
    // 接着设置调度的时间规则.当前时间运行
    Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();

    // 注册并进行调度
    sched.scheduleJob(job, trigger);

    // 启动调度器
    sched.start();

    try {
      // 当前线程等待65秒
      Thread.sleep(65L * 1000L);
    } catch (Exception e) {

    }

    // 调度器停止运行
    sched.shutdown(true);

    log.error("结束运行。。。。");
  }
示例#2
0
  public void run() throws Exception {
    Logger log = LoggerFactory.getLogger(SimpleExample.class);

    log.info("------- Initializing ----------------------");

    // First we must get a reference to a scheduler
    SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched = sf.getScheduler();

    log.info("------- Initialization Complete -----------");

    // computer a time that is on the next round minute
    Date runTime = evenMinuteDate(new Date());

    log.info("------- Scheduling Job  -------------------");

    // define the job and tie it to our HelloJob class
    JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();

    // Trigger the job to run on the next round minute
    Trigger trigger =
        newTrigger()
            .withIdentity("trigger1", "group1")
            .withIdentity(new TriggerKey("mytrigger"))
            .startAt(runTime)
            .build();

    // Tell quartz to schedule the job using our trigger
    sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " will run at: " + runTime);

    // Start up the scheduler (nothing can actually run until the
    // scheduler has been started)
    sched.start();

    log.info("------- Started Scheduler -----------------");

    // wait long enough so that the scheduler as an opportunity to
    // run the job!
    log.info("------- Waiting 5 seconds... -------------");
    try {
      // wait 65 seconds to show job
      Thread.sleep(5L * 1000L);
      // executing...
    } catch (Exception e) {
    }

    // shut down the scheduler
    log.info("------- Shutting Down ---------------------");
    sched.shutdown(true);
    log.info("------- Shutdown Complete -----------------");
  }