Esempio n. 1
0
  public boolean start() {

    try {
      scheduler.start();
    } catch (SchedulerException e) {
      throw new SynapseException("Error starting the scheduler", e);
    }

    Trigger trigger;
    TriggerBuilder<Trigger> triggerBuilder = newTrigger().withIdentity(name + "-trigger");

    if (cronExpression == null || "".equals(cronExpression)) {
      trigger =
          triggerBuilder
              .withSchedule(
                  simpleSchedule()
                      .withIntervalInMilliseconds(interval)
                      .repeatForever()
                      .withMisfireHandlingInstructionNextWithRemainingCount())
              .build();
    } else {
      trigger =
          triggerBuilder
              .startNow()
              .withSchedule(
                  CronScheduleBuilder.cronSchedule(cronExpression)
                      .withMisfireHandlingInstructionDoNothing())
              .build();
    }

    JobDataMap jobDataMap = getJobDataMap();
    jobDataMap.put(MessageProcessorConstants.PARAMETERS, parameters);

    JobBuilder jobBuilder = getJobBuilder();
    JobDetail jobDetail = jobBuilder.usingJobData(jobDataMap).build();

    try {
      scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException e) {
      throw new SynapseException(
          "Error scheduling job : " + jobDetail + " with trigger " + trigger, e);
    }
    if (logger.isDebugEnabled()) {
      logger.debug("Started message processor. [" + getName() + "].");
    }

    return true;
  }
  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();
  }