コード例 #1
0
  @Test
  public void testUpdate2() throws Exception {
    SchedulerResponse schedulerResponse =
        _quartzSchedulerEngine.getScheduledJob(
            _TEST_JOB_NAME_0, _MEMORY_TEST_GROUP_NAME, StorageType.MEMORY);

    Trigger trigger = schedulerResponse.getTrigger();

    CalendarIntervalTrigger calendarIntervalTrigger =
        (CalendarIntervalTrigger) trigger.getWrappedTrigger();

    Assert.assertEquals(_DEFAULT_INTERVAL, calendarIntervalTrigger.getRepeatInterval());

    String cronExpression = "0 0 12 * * ?";

    Trigger newTrigger =
        _quartzTriggerFactory.createTrigger(
            _TEST_JOB_NAME_0, _MEMORY_TEST_GROUP_NAME, null, null, cronExpression);

    _quartzSchedulerEngine.update(newTrigger, StorageType.MEMORY);

    schedulerResponse =
        _quartzSchedulerEngine.getScheduledJob(
            _TEST_JOB_NAME_0, _MEMORY_TEST_GROUP_NAME, StorageType.MEMORY);

    trigger = schedulerResponse.getTrigger();

    CronTrigger cronTrigger = (CronTrigger) trigger.getWrappedTrigger();

    Assert.assertEquals(cronExpression, cronTrigger.getCronExpression());
  }
コード例 #2
0
  /**
   * Register listener.
   *
   * @param trigger the trigger
   */
  private void registerListener(Trigger trigger) {
    String jobKey = getJobKey(trigger);

    if (jobs.containsKey(jobKey)) {
      _log.fatal("Listener already exists for " + jobKey);
      jobs.get(jobKey);
    } else {
      SchedulerEventMessageListenerWrapper schedulerEventMessageListenerWrapper =
          new SchedulerEventMessageListenerWrapper();

      SchedulerEntryImpl schedulerEntry = new SchedulerEntryImpl();

      schedulerEntry.setTrigger(trigger);
      schedulerEntry.setDescription("sample description");
      schedulerEntry.setEventListenerClass(LogMessageListener.class.getName());

      schedulerEventMessageListenerWrapper.setSchedulerEntry(schedulerEntry);
      schedulerEventMessageListenerWrapper.setMessageListener(new LogMessageListener());

      _log.fatal(
          "Register listener for:: jobName["
              + trigger.getJobName()
              + "] groupName["
              + trigger.getGroupName()
              + "] startDate["
              + trigger.getStartDate()
              + "]");

      MessageBusUtil.registerMessageListener(
          DestinationNames.SCHEDULER_DISPATCH, schedulerEventMessageListenerWrapper);

      jobs.put(jobKey, schedulerEventMessageListenerWrapper);
    }
  }
コード例 #3
0
    @Override
    public void update(Trigger trigger) throws SchedulerException {
      SchedulerResponse schedulerResponse =
          getScheduledJob(trigger.getJobName(), trigger.getGroupName());

      schedulerResponse.setTrigger(trigger);
    }
コード例 #4
0
  @Test
  public void testUpdate1() throws Exception {
    SchedulerResponse schedulerResponse =
        _quartzSchedulerEngine.getScheduledJob(
            _TEST_JOB_NAME_0, _MEMORY_TEST_GROUP_NAME, StorageType.MEMORY);

    Trigger trigger = schedulerResponse.getTrigger();

    CalendarIntervalTrigger calendarIntervalTrigger =
        (CalendarIntervalTrigger) trigger.getWrappedTrigger();

    Assert.assertEquals(_DEFAULT_INTERVAL, calendarIntervalTrigger.getRepeatInterval());

    Trigger newTrigger =
        _quartzTriggerFactory.createTrigger(
            _TEST_JOB_NAME_0,
            _MEMORY_TEST_GROUP_NAME,
            null,
            null,
            _DEFAULT_INTERVAL * 2,
            TimeUnit.SECOND);

    _quartzSchedulerEngine.update(newTrigger, StorageType.MEMORY);

    schedulerResponse =
        _quartzSchedulerEngine.getScheduledJob(
            _TEST_JOB_NAME_0, _MEMORY_TEST_GROUP_NAME, StorageType.MEMORY);

    trigger = schedulerResponse.getTrigger();

    calendarIntervalTrigger = (CalendarIntervalTrigger) trigger.getWrappedTrigger();

    Assert.assertEquals(_DEFAULT_INTERVAL * 2, calendarIntervalTrigger.getRepeatInterval());
  }
コード例 #5
0
  private void _assertTriggerContent(SchedulerResponse schedulerResponse, long expectedInterval) {

    Trigger trigger = schedulerResponse.getTrigger();

    long interval = (Long) trigger.getTriggerContent();

    Assert.assertEquals(expectedInterval, interval);
  }
コード例 #6
0
  @Override
  public void schedule(
      com.liferay.portal.kernel.scheduler.Trigger trigger,
      String description,
      String destination,
      Message message,
      StorageType storageType)
      throws SchedulerException {

    try {
      Scheduler scheduler = getScheduler(storageType);

      Trigger quartzTrigger = (Trigger) trigger.getWrappedTrigger();

      if (quartzTrigger == null) {
        return;
      }

      description = fixMaxLength(description, _descriptionMaxLength, storageType);

      message = message.clone();

      message.put(SchedulerEngine.GROUP_NAME, trigger.getGroupName());
      message.put(SchedulerEngine.JOB_NAME, trigger.getJobName());

      schedule(scheduler, storageType, quartzTrigger, description, destination, message);
    } catch (RuntimeException re) {
      if (PortalRunMode.isTestMode()) {
        StackTraceElement[] stackTraceElements = re.getStackTrace();

        for (StackTraceElement stackTraceElement : stackTraceElements) {
          String className = stackTraceElement.getClassName();

          if (className.contains(ServerDetector.class.getName())) {
            if (_log.isInfoEnabled()) {
              _log.info(re, re);
            }

            return;
          }

          throw new SchedulerException("Unable to schedule job", re);
        }
      } else {
        throw new SchedulerException("Unable to schedule job", re);
      }
    } catch (Exception e) {
      throw new SchedulerException("Unable to schedule job", e);
    }
  }
コード例 #7
0
  protected void update(
      Scheduler scheduler,
      com.liferay.portal.kernel.scheduler.Trigger trigger,
      StorageType storageType)
      throws Exception {

    Trigger quartzTrigger = (Trigger) trigger.getWrappedTrigger();

    if (quartzTrigger == null) {
      return;
    }

    TriggerKey triggerKey = quartzTrigger.getKey();

    if (scheduler.getTrigger(triggerKey) != null) {
      scheduler.rescheduleJob(triggerKey, quartzTrigger);
    } else {
      JobKey jobKey = quartzTrigger.getJobKey();

      JobDetail jobDetail = scheduler.getJobDetail(jobKey);

      if (jobDetail == null) {
        return;
      }

      synchronized (this) {
        scheduler.deleteJob(jobKey);
        scheduler.scheduleJob(jobDetail, quartzTrigger);
      }

      updateJobState(scheduler, jobKey, TriggerState.NORMAL, true);
    }
  }
コード例 #8
0
    @Override
    public void schedule(
        Trigger trigger, String description, String destinationName, Message message)
        throws SchedulerException {

      String jobName = trigger.getJobName();

      if (!jobName.startsWith(_TEST_JOB_NAME_PREFIX)) {
        throw new SchedulerException("Invalid job name " + jobName);
      }

      String groupName = trigger.getGroupName();

      StorageType storageType = _getStorageType(groupName);

      _addJobs(trigger.getJobName(), groupName, storageType, trigger, message);
    }
コード例 #9
0
    public MockScheduler(StorageType storageType, String defaultGroupName) {
      for (int i = 0; i < _DEFAULT_JOB_NUMBER; i++) {
        Trigger trigger =
            _quartzTriggerFactory.createTrigger(
                _TEST_JOB_NAME_PREFIX + i,
                defaultGroupName,
                null,
                null,
                _DEFAULT_INTERVAL,
                TimeUnit.SECOND);

        addJob(
            _TEST_JOB_NAME_PREFIX + i,
            defaultGroupName,
            storageType,
            (org.quartz.Trigger) trigger.getWrappedTrigger());
      }
    }
コード例 #10
0
 /**
  * Gets the job key.
  *
  * @param trigger the trigger
  * @return the job key
  */
 public String getJobKey(Trigger trigger) {
   return trigger.getGroupName() + "_" + trigger.getJobName();
 }
コード例 #11
0
  protected Trigger getQuartzTrigger(
      com.liferay.portal.kernel.scheduler.Trigger trigger, StorageType storageType)
      throws SchedulerException {

    if (trigger == null) {
      return null;
    }

    Date endDate = trigger.getEndDate();
    String jobName = fixMaxLength(trigger.getJobName(), _jobNameMaxLength, storageType);
    String groupName = fixMaxLength(trigger.getGroupName(), _groupNameMaxLength, storageType);

    Date startDate = trigger.getStartDate();

    if (startDate == null) {
      startDate = new Date(System.currentTimeMillis());
    }

    TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();

    triggerBuilder.endAt(endDate);
    triggerBuilder.forJob(jobName, groupName);
    triggerBuilder.startAt(startDate);
    triggerBuilder.withIdentity(jobName, groupName);

    TriggerType triggerType = trigger.getTriggerType();

    if (triggerType == TriggerType.CRON) {
      triggerBuilder.withSchedule(
          CronScheduleBuilder.cronSchedule((String) trigger.getTriggerContent()));

      return triggerBuilder.build();
    }

    ObjectValuePair<Integer, TimeUnit> objectValuePair =
        (ObjectValuePair<Integer, TimeUnit>) trigger.getTriggerContent();

    int interval = objectValuePair.getKey();

    if (interval < 0) {
      if (_log.isWarnEnabled()) {
        _log.warn("Not scheduling " + trigger.getJobName() + " because interval is less than 0");
      }

      return null;
    } else if (interval == 0) {
      return triggerBuilder.build();
    }

    TimeUnit timeUnit = objectValuePair.getValue();

    if (timeUnit == TimeUnit.MILLISECOND) {
      SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();

      simpleScheduleBuilder.withIntervalInMilliseconds(interval);
      simpleScheduleBuilder.withRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);

      triggerBuilder.withSchedule(simpleScheduleBuilder);
    } else {
      CalendarIntervalScheduleBuilder calendarIntervalScheduleBuilder =
          CalendarIntervalScheduleBuilder.calendarIntervalSchedule();

      calendarIntervalScheduleBuilder.withInterval(interval, IntervalUnit.valueOf(timeUnit.name()));

      triggerBuilder.withSchedule(calendarIntervalScheduleBuilder);
    }

    return triggerBuilder.build();
  }