Exemplo n.º 1
0
  public static void main(String[] args) throws SchedulerException {
    //		实现 Job 接口,可使 Java 类变为可调度的任务

    //		创建描述 Job 的 JobDetail 对象
    JobDetailImpl jobDetailImpl = new JobDetailImpl();

    jobDetailImpl.setName("myJob2");
    jobDetailImpl.setGroup("tem");
    jobDetailImpl.setJobClass(MyJob2.class);

    jobDetailImpl.setName("myJob");
    jobDetailImpl.setGroup("Two");
    jobDetailImpl.setJobClass(MyJob.class);

    //		创建 SimpleTrigger 对象
    SimpleTriggerImpl trigger = new SimpleTriggerImpl();
    trigger.setName("myTrigger");
    trigger.setGroup("Two");

    //		设置触发 Job 执行的时间规则
    trigger.setStartTime(new Date());
    trigger.setRepeatCount(10);
    trigger.setRepeatInterval(1000 * 3);

    //		通过 SchedulerFactory 获取 Scheduler 对象
    StdSchedulerFactory factory = new StdSchedulerFactory();
    Scheduler scheduler = factory.getScheduler();
    //		向 SchedulerFactory 中注册 JobDetail 和 Trigger
    scheduler.scheduleJob(jobDetailImpl, trigger);

    //		启动调度任务
    scheduler.start();
  }
  @Override
  public void afterPropertiesSet() throws Exception {
    // 参数验证
    Assert.notNull(this.triggerRule);
    Assert.notNull(this.mainTableName);
    Class<?> jClass = Class.forName(jobClass);
    Assert.isAssignable(Job.class, jClass);

    if (this.name == null) {
      this.name = this.beanName;
    }
    if (this.group == null) {
      this.group = Scheduler.DEFAULT_GROUP;
    }
    if (this.startDelay > 0 || this.startTime == null) {
      this.startTime = new Date(System.currentTimeMillis() + this.startDelay);
    }
    this.triggerClass = this.triggerRule.getTriggerClass();

    // jobDetail
    JobDetailImpl jdi = new JobDetailImpl();
    jdi.setName(this.name);
    jdi.setGroup(this.group);
    jdi.setJobClass(QuartzJob.class);
    jdi.setJobDataMap(this.jobDataMap);
    //        jdi.setDurability(true);
    jdi.setDescription(this.description);
    this.jobDetail = jdi;

    // jobDataMap
    jobDataMap.put("jobDetail", jdi);
    jobDataMap.put(QuartzJob.JOB_CLASS, jClass);
    jobDataMap.put(QuartzJob.MAIN_TABLE_NAME, mainTableName);
    jobDataMap.put(QuartzJob.THREAD_COUNT, threadCount);
    jobDataMap.put(QuartzJob.TIMEOUT, timeoutMinutesForMultiThread);
    jobDataMap.put(QuartzJob.ALLOW_RETRY, allowRetry);

    // trigger
    this.trigger = triggerRule.initializeTrigger();
    trigger.setName(this.name);
    trigger.setGroup(this.group);
    trigger.setJobKey(this.jobDetail.getKey());
    trigger.setJobDataMap(this.jobDataMap);
    trigger.setStartTime(this.startTime);
    trigger.setPriority(this.priority);
    trigger.setMisfireInstruction(this.misfireInstruction);
    trigger.setDescription(this.description);
  }