Ejemplo n.º 1
0
 public Trigger getTrigger() {
   Trigger trigger = null;
   if (getClass().equals(SimpleTriggerDescriptor.class)) {
     trigger = new SimpleTrigger(name, group);
   } else {
     trigger = new CronTrigger(name, group);
   }
   trigger.setJobName(jobName);
   trigger.setJobGroup(group);
   trigger.setStartTime(dateTimeService.getCurrentDate());
   completeTriggerDescription(trigger);
   return trigger;
 }
  public void afterPropertiesSet() throws ParseException {
    customTrigger = BeanUtils.instantiateClass(triggerClass);

    if (triggerAttributes.containsKey(QuartzConstants.START_DELAY)) {
      Number startDelay = (Number) triggerAttributes.remove(QuartzConstants.START_DELAY);
      customTrigger.setStartTime(new Date(System.currentTimeMillis() + startDelay.longValue()));
    }

    if (jobDetail != null) {
      customTrigger.setJobName(jobDetail.getName());
      customTrigger.setJobGroup(jobDetail.getGroup());
    }

    BeanWrapper customTriggerWrapper = PropertyAccessorFactory.forBeanPropertyAccess(customTrigger);
    customTriggerWrapper.registerCustomEditor(String.class, new StringEditor());
    customTriggerWrapper.setPropertyValues(triggerAttributes);
  }
  /**
   * Schedules a given job and trigger (both wrapped by a <code>JobSchedulingBundle</code>).
   *
   * @param job job wrapper.
   * @param sched job scheduler.
   * @param localOverWriteExistingJobs locally overwrite existing jobs.
   * @exception SchedulerException if the Job or Trigger cannot be added to the Scheduler, or there
   *     is an internal Scheduler error.
   */
  public void scheduleJob(
      JobSchedulingBundle job, Scheduler sched, boolean localOverWriteExistingJobs)
      throws SchedulerException {
    if ((job != null) && job.isValid()) {
      JobDetail detail = job.getJobDetail();

      JobDetail dupeJ = sched.getJobDetail(detail.getName(), detail.getGroup());

      if ((dupeJ != null) && !localOverWriteExistingJobs) {
        getLog().info("Not overwriting existing job: " + dupeJ.getFullName());
        return;
      }

      if (dupeJ != null) {
        getLog().info("Replacing job: " + detail.getFullName());
      } else {
        getLog().info("Adding job: " + detail.getFullName());
      }

      if (job.getTriggers().size() == 0 && !job.getJobDetail().isDurable()) {
        if (dupeJ == null) {
          throw new SchedulerException(
              "A new job defined without any triggers must be durable: " + detail.getFullName());
        }

        if ((dupeJ.isDurable()
            && (sched.getTriggersOfJob(detail.getName(), detail.getGroup()).length == 0))) {
          throw new SchedulerException(
              "Can't make a durable job without triggers non-durable: " + detail.getFullName());
        }
      }

      sched.addJob(detail, true);

      for (Iterator iter = job.getTriggers().iterator(); iter.hasNext(); ) {
        Trigger trigger = (Trigger) iter.next();

        trigger.setJobName(detail.getName());
        trigger.setJobGroup(detail.getGroup());

        if (trigger.getStartTime() == null) {
          trigger.setStartTime(new Date());
        }

        boolean addedTrigger = false;
        while (addedTrigger == false) {
          Trigger dupeT = sched.getTrigger(trigger.getName(), trigger.getGroup());
          if (dupeT != null) {
            if (getLog().isDebugEnabled()) {
              getLog()
                  .debug(
                      "Rescheduling job: "
                          + detail.getFullName()
                          + " with updated trigger: "
                          + trigger.getFullName());
            }
            if (!dupeT.getJobGroup().equals(trigger.getJobGroup())
                || !dupeT.getJobName().equals(trigger.getJobName())) {
              getLog().warn("Possibly duplicately named triggers in jobs xml file!");
            }
            sched.rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
          } else {
            if (getLog().isDebugEnabled()) {
              getLog()
                  .debug(
                      "Scheduling job: "
                          + detail.getFullName()
                          + " with trigger: "
                          + trigger.getFullName());
            }

            try {
              sched.scheduleJob(trigger);
            } catch (ObjectAlreadyExistsException e) {
              if (getLog().isDebugEnabled()) {
                getLog()
                    .debug(
                        "Adding trigger: "
                            + trigger.getFullName()
                            + " for job: "
                            + detail.getFullName()
                            + " failed because the trigger already existed.  "
                            + "This is likely due to a race condition between multiple instances "
                            + "in the cluster.  Will try to reschedule instead.");
              }
              continue;
            }
          }
          addedTrigger = true;
        }
      }

      addScheduledJob(job);
    }
  }
  public static org.quartz.Trigger convertTriggerToNativeObject(Trigger spagobiTrigger) {
    org.quartz.Trigger quartzTrigger;

    logger.debug("IN");

    quartzTrigger = null;
    try {
      Assert.assertNotNull(spagobiTrigger, "Input parameter [spagobiTrigger] csannot be null");

      if (spagobiTrigger.isRunImmediately()) {

        quartzTrigger = TriggerUtils.makeImmediateTrigger(spagobiTrigger.getName(), 0, 10000);
        quartzTrigger.setJobName(spagobiTrigger.getJob().getName());
        quartzTrigger.setJobGroup(spagobiTrigger.getJob().getGroupName());
        JobDataMap jobDataMap =
            convertParametersToNativeObject(spagobiTrigger.getJob().getParameters());
        quartzTrigger.setJobDataMap(jobDataMap);

      } else {

        if (spagobiTrigger.isSimpleTrigger()) {
          quartzTrigger = new org.quartz.SimpleTrigger();
        } else {
          org.quartz.CronTrigger quartzCronTrigger = new org.quartz.CronTrigger();
          String quartzCronExpression =
              convertCronExpressionToNativeObject(
                  spagobiTrigger.getChronExpression(), spagobiTrigger.getStartTime());
          quartzCronTrigger.setCronExpression(quartzCronExpression);
          quartzTrigger = quartzCronTrigger;
          // dirty trick
          spagobiTrigger
              .getJob()
              .addParameter(
                  SPAGOBI_CRON_EXPRESSION, spagobiTrigger.getChronExpression().getExpression());
        }

        quartzTrigger.setName(spagobiTrigger.getName());
        quartzTrigger.setDescription(spagobiTrigger.getDescription());
        if (spagobiTrigger.getGroupName() == null) {
          quartzTrigger.setGroup(Scheduler.DEFAULT_GROUP);
        } else {
          quartzTrigger.setGroup(spagobiTrigger.getGroupName());
        }

        quartzTrigger.setStartTime(spagobiTrigger.getStartTime());
        if (spagobiTrigger.getEndTime() != null) {
          quartzTrigger.setEndTime(spagobiTrigger.getEndTime());
        }
        quartzTrigger.setJobName(spagobiTrigger.getJob().getName());
        if (spagobiTrigger.getJob().getGroupName() == null) {
          quartzTrigger.setJobGroup(Scheduler.DEFAULT_GROUP);
        } else {
          quartzTrigger.setJobGroup(spagobiTrigger.getJob().getGroupName());
        }

        quartzTrigger.setVolatility(spagobiTrigger.getJob().isVolatile());

        JobDataMap jobDataMap =
            convertParametersToNativeObject(spagobiTrigger.getJob().getParameters());
        quartzTrigger.setJobDataMap(jobDataMap);
      }
    } catch (Throwable t) {
      throw new SpagoBIRuntimeException(
          "An unexpected error occured while converting Trigger to native object", t);
    } finally {
      logger.debug("OUT");
    }

    return quartzTrigger;
  }