Example #1
0
  @Test
  public void testDurableStorageFunctions() throws Exception {
    Scheduler sched = createScheduler("testDurableStorageFunctions", 2);
    try {
      // test basic storage functions of scheduler...

      JobDetail job = newJob().ofType(TestJob.class).withIdentity("j1").storeDurably().build();

      assertFalse("Unexpected existence of job named 'j1'.", sched.checkExists(jobKey("j1")));

      sched.addJob(job, false);

      assertTrue("Unexpected non-existence of job named 'j1'.", sched.checkExists(jobKey("j1")));

      JobDetail nonDurableJob = newJob().ofType(TestJob.class).withIdentity("j2").build();

      try {
        sched.addJob(nonDurableJob, false);
        fail("Storage of non-durable job should not have succeeded.");
      } catch (SchedulerException expected) {
        assertFalse("Unexpected existence of job named 'j2'.", sched.checkExists(jobKey("j2")));
      }

      sched.addJob(nonDurableJob, false, true);

      assertTrue("Unexpected non-existence of job named 'j2'.", sched.checkExists(jobKey("j2")));
    } finally {
      sched.shutdown(true);
    }
  }
Example #2
0
 public RunTimeJobDetail scheduleJob(JobDetail jobDetail, Trigger trigger)
     throws SchedulerException, JobAlreadyExistsException {
   if (quartzScheduler.checkExists(jobDetail.getKey()))
     throw new JobAlreadyExistsException(jobDetail.getKey().getName() + " already exists");
   quartzScheduler.scheduleJob(jobDetail, trigger);
   return getJobDetails(jobDetail.getKey().getGroup(), jobDetail.getKey().getName());
 }
  public void executeJob(final ScheduledJobDetail scheduledJobDetail, String triggerType) {
    try {
      final JobDataMap jobDataMap = new JobDataMap();
      if (triggerType == null) {
        triggerType = SchedulerServiceConstants.TRIGGER_TYPE_APPLICATION;
      }
      jobDataMap.put(SchedulerServiceConstants.TRIGGER_TYPE_REFERENCE, triggerType);
      jobDataMap.put(
          SchedulerServiceConstants.TENANT_IDENTIFIER,
          ThreadLocalContextUtil.getTenant().getTenantIdentifier());
      final String key = scheduledJobDetail.getJobKey();
      final JobKey jobKey = constructJobKey(key);
      final String schedulerName = getSchedulerName(scheduledJobDetail);
      final Scheduler scheduler = this.schedulers.get(schedulerName);
      if (scheduler == null || !scheduler.checkExists(jobKey)) {
        final JobDetail jobDetail = createJobDetail(scheduledJobDetail);
        final String tempSchedulerName = "temp" + scheduledJobDetail.getId();
        final Scheduler tempScheduler =
            createScheduler(tempSchedulerName, 1, schedulerJobListener, schedulerStopListener);
        tempScheduler.addJob(jobDetail, true);
        jobDataMap.put(SchedulerServiceConstants.SCHEDULER_NAME, tempSchedulerName);
        this.schedulers.put(tempSchedulerName, tempScheduler);
        tempScheduler.triggerJob(jobDetail.getKey(), jobDataMap);
      } else {
        scheduler.triggerJob(jobKey, jobDataMap);
      }

    } catch (final Exception e) {
      final String msg = "Job execution failed for job with id:" + scheduledJobDetail.getId();
      logger.error(msg, e);
      throw new PlatformInternalServerException(
          "error.msg.sheduler.job.execution.failed", msg, scheduledJobDetail.getId());
    }
  }
 /**
  * This method checks if there is a {@link #JOBKEY_DATABASE_UPDATE_REGULAR} job existing in the
  * scheduler.
  *
  * @return <b>true</b> if a regular job exists in the scheduler, otherwise <b>false</b>
  */
 public boolean isOnRegularSchedule() {
   try {
     return sched.checkExists(JOBKEY_DATABASE_UPDATE_REGULAR);
   } catch (SchedulerException ex) {
     Logger.getLogger(QuartzSchedulerController.class.getName()).log(Level.SEVERE, null, ex);
     throw new WebApplicationException(PreparedServerResponses.ERROR_SCHEDULER);
   }
 }
 /**
  * Check if the given job is scheduled.
  *
  * @param job the job to check
  * @return true if the job is scheduled, false else
  * @throws JobSchedulerException thrown if an exception occurs while checking if the job is
  *     scheduled
  */
 public boolean isScheduled(final org.easybatch.core.job.Job job) throws JobSchedulerException {
   try {
     return scheduler.checkExists(
         TriggerKey.triggerKey(TRIGGER_NAME_PREFIX + job.getExecutionId()));
   } catch (SchedulerException e) {
     throw new JobSchedulerException("Unable to check if the job " + job + " is scheduled", e);
   }
 }
 void deleteJob(String id) {
   JobKey jobKey = createJobKey(id);
   try {
     if (scheduler.checkExists(jobKey)) {
       scheduler.deleteJob(jobKey);
     }
   } catch (SchedulerException e) {
     throw new BusinessException("更新任务失败", e, 500);
   }
 }
Example #7
0
 public boolean unscheduleJob(TriggerKey triggerKey) {
   boolean flag = false;
   try {
     if (scheduler.checkExists(triggerKey)) {
       flag = scheduler.unscheduleJob(triggerKey);
     }
   } catch (SchedulerException e) {
     logger.error(e.getMessage());
   }
   return flag;
 }
Example #8
0
 public boolean pauseJob(JobKey jobKey) {
   try {
     if (scheduler.checkExists(jobKey)) {
       scheduler.pauseJob(jobKey);
     }
     return true;
   } catch (SchedulerException e) {
     logger.error("调度器暂停JOB异常:" + e.getMessage());
     return false;
   }
 }
Example #9
0
 public boolean removeJob(JobKey jobKey) {
   boolean flag = false;
   try {
     if (scheduler.checkExists(jobKey)) {
       flag = scheduler.deleteJob(jobKey);
     }
   } catch (SchedulerException e) {
     logger.error("调度器删除JOB异常:" + e.getMessage());
   }
   return flag;
 }
 /** This method adds the {@link #JOBKEY_DATABASE_UPDATE_INSTANT} job to the scheduler. */
 public void startInstantDatabaseUpdate() {
   try {
     if (!sched.checkExists(JOBKEY_DATABASE_UPDATE_INSTANT)) {
       sched.addJob(instantDatabaseUpdateJob, true);
     }
     if (!QuartzUpdateDatabaseJob.IS_RUNNING) {
       sched.triggerJob(JOBKEY_DATABASE_UPDATE_INSTANT);
     }
   } catch (SchedulerException ex) {
     Logger.getLogger(QuartzSchedulerController.class.getName()).log(Level.SEVERE, null, ex);
     throw new WebApplicationException(PreparedServerResponses.ERROR_SCHEDULER);
   }
 }
  /**
   * remove a scheduled job
   *
   * @param oldScheduler scheduler to stop
   * @return boolean value
   * @throws SchedulerException
   */
  public static boolean unSchedule(am.projects.webserver.report.vo.Scheduler oldScheduler)
      throws SchedulerException {

    if (null == scheduler) return true;

    String jobName = buildJobName(oldScheduler);
    String groupName = buildJobGroupName(oldScheduler);
    JobKey jobKey = new JobKey(jobName, groupName);

    boolean success = true;
    if (scheduler.checkExists(jobKey)) {
      success = scheduler.deleteJob(jobKey);
      logger.info(String.format("unSchedule %s", jobName));
    }

    return success;
  }
Example #12
0
 private void scheduleJob(final CronTrigger trigger) throws SchedulerException {
   if (!scheduler.checkExists(jobDetail.getKey())) {
     scheduler.scheduleJob(jobDetail, trigger);
   }
   scheduler.start();
 }
Example #13
0
  @Test
  public void testBasicStorageFunctions() throws Exception {
    Scheduler sched = createScheduler("testBasicStorageFunctions", 2);

    // test basic storage functions of scheduler...

    JobDetail job = newJob().ofType(TestJob.class).withIdentity("j1").storeDurably().build();

    assertFalse("Unexpected existence of job named 'j1'.", sched.checkExists(jobKey("j1")));

    sched.addJob(job, false);

    assertTrue(
        "Expected existence of job named 'j1' but checkExists return false.",
        sched.checkExists(jobKey("j1")));

    job = sched.getJobDetail(jobKey("j1"));

    assertNotNull("Stored job not found!", job);

    sched.deleteJob(jobKey("j1"));

    Trigger trigger =
        newTrigger()
            .withIdentity("t1")
            .forJob(job)
            .startNow()
            .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(5))
            .build();

    assertFalse("Unexpected existence of trigger named 't1'.", sched.checkExists(triggerKey("t1")));

    sched.scheduleJob(job, trigger);

    assertTrue(
        "Expected existence of trigger named 't1' but checkExists return false.",
        sched.checkExists(triggerKey("t1")));

    job = sched.getJobDetail(jobKey("j1"));

    assertNotNull("Stored job not found!", job);

    trigger = sched.getTrigger(triggerKey("t1"));

    assertNotNull("Stored trigger not found!", trigger);

    job = newJob().ofType(TestJob.class).withIdentity("j2", "g1").build();

    trigger =
        newTrigger()
            .withIdentity("t2", "g1")
            .forJob(job)
            .startNow()
            .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(5))
            .build();

    sched.scheduleJob(job, trigger);

    job = newJob().ofType(TestJob.class).withIdentity("j3", "g1").build();

    trigger =
        newTrigger()
            .withIdentity("t3", "g1")
            .forJob(job)
            .startNow()
            .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(5))
            .build();

    sched.scheduleJob(job, trigger);

    List<String> jobGroups = sched.getJobGroupNames();
    List<String> triggerGroups = sched.getTriggerGroupNames();

    assertTrue("Job group list size expected to be = 2 ", jobGroups.size() == 2);
    assertTrue("Trigger group list size expected to be = 2 ", triggerGroups.size() == 2);

    Set<JobKey> jobKeys = sched.getJobKeys(GroupMatcher.jobGroupEquals(JobKey.DEFAULT_GROUP));
    Set<TriggerKey> triggerKeys =
        sched.getTriggerKeys(GroupMatcher.triggerGroupEquals(TriggerKey.DEFAULT_GROUP));

    assertTrue("Number of jobs expected in default group was 1 ", jobKeys.size() == 1);
    assertTrue("Number of triggers expected in default group was 1 ", triggerKeys.size() == 1);

    jobKeys = sched.getJobKeys(GroupMatcher.jobGroupEquals("g1"));
    triggerKeys = sched.getTriggerKeys(GroupMatcher.triggerGroupEquals("g1"));

    assertTrue("Number of jobs expected in 'g1' group was 2 ", jobKeys.size() == 2);
    assertTrue("Number of triggers expected in 'g1' group was 2 ", triggerKeys.size() == 2);

    TriggerState s = sched.getTriggerState(triggerKey("t2", "g1"));
    assertTrue("State of trigger t2 expected to be NORMAL ", s.equals(TriggerState.NORMAL));

    sched.pauseTrigger(triggerKey("t2", "g1"));
    s = sched.getTriggerState(triggerKey("t2", "g1"));
    assertTrue("State of trigger t2 expected to be PAUSED ", s.equals(TriggerState.PAUSED));

    sched.resumeTrigger(triggerKey("t2", "g1"));
    s = sched.getTriggerState(triggerKey("t2", "g1"));
    assertTrue("State of trigger t2 expected to be NORMAL ", s.equals(TriggerState.NORMAL));

    Set<String> pausedGroups = sched.getPausedTriggerGroups();
    assertTrue("Size of paused trigger groups list expected to be 0 ", pausedGroups.size() == 0);

    sched.pauseTriggers(GroupMatcher.triggerGroupEquals("g1"));

    // test that adding a trigger to a paused group causes the new trigger to be paused also...
    job = newJob().ofType(TestJob.class).withIdentity("j4", "g1").build();

    trigger =
        newTrigger()
            .withIdentity("t4", "g1")
            .forJob(job)
            .startNow()
            .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(5))
            .build();

    sched.scheduleJob(job, trigger);
    // TODO: nexus hack: JobStoreImpl DOES NOT "remember" paused groups
    sched.pauseJob(jobKey("j4", "g1"));

    pausedGroups = sched.getPausedTriggerGroups();
    assertTrue(
        "Size of paused trigger groups list expected to be 1: " + pausedGroups,
        pausedGroups.size() == 1);

    s = sched.getTriggerState(triggerKey("t2", "g1"));
    assertTrue("State of trigger t2 expected to be PAUSED ", s.equals(TriggerState.PAUSED));

    s = sched.getTriggerState(triggerKey("t4", "g1"));
    assertTrue("State of trigger t4 expected to be PAUSED ", s.equals(TriggerState.PAUSED));

    sched.resumeTriggers(GroupMatcher.triggerGroupEquals("g1"));
    s = sched.getTriggerState(triggerKey("t2", "g1"));
    assertTrue("State of trigger t2 expected to be NORMAL ", s.equals(TriggerState.NORMAL));
    s = sched.getTriggerState(triggerKey("t4", "g1"));
    assertTrue("State of trigger t4 expected to be NORMAL ", s.equals(TriggerState.NORMAL));
    pausedGroups = sched.getPausedTriggerGroups();
    assertTrue("Size of paused trigger groups list expected to be 0 ", pausedGroups.size() == 0);

    assertFalse(
        "Scheduler should have returned 'false' from attempt to unschedule non-existing trigger. ",
        sched.unscheduleJob(triggerKey("foasldfksajdflk")));

    assertTrue(
        "Scheduler should have returned 'true' from attempt to unschedule existing trigger. ",
        sched.unscheduleJob(triggerKey("t3", "g1")));

    jobKeys = sched.getJobKeys(GroupMatcher.jobGroupEquals("g1"));
    triggerKeys = sched.getTriggerKeys(GroupMatcher.triggerGroupEquals("g1"));

    assertTrue(
        "Number of jobs expected in 'g1' group was 1 ",
        jobKeys.size() == 2); // job should have been deleted also, because it is non-durable
    assertTrue("Number of triggers expected in 'g1' group was 1 ", triggerKeys.size() == 2);

    assertTrue(
        "Scheduler should have returned 'true' from attempt to unschedule existing trigger. ",
        sched.unscheduleJob(triggerKey("t1")));

    jobKeys = sched.getJobKeys(GroupMatcher.jobGroupEquals(JobKey.DEFAULT_GROUP));
    triggerKeys = sched.getTriggerKeys(GroupMatcher.triggerGroupEquals(TriggerKey.DEFAULT_GROUP));

    assertTrue(
        "Number of jobs expected in default group was 1 ",
        jobKeys.size() == 1); // job should have been left in place, because it is non-durable
    assertTrue("Number of triggers expected in default group was 0 ", triggerKeys.size() == 0);

    sched.shutdown(true);
  }