Example #1
0
 @SuppressWarnings("unchecked")
 public void runAllTasks() throws TaskSystemException {
   try {
     List<String> triggerNames;
     List<Trigger> triggers = new ArrayList<Trigger>();
     String[] groupNames = scheduler.getTriggerGroupNames();
     for (String group : groupNames) {
       triggerNames = new ArrayList<String>(Arrays.asList(scheduler.getTriggerNames(group)));
       for (String triggerName : triggerNames) {
         triggers.add(scheduler.getTrigger(triggerName, group));
       }
     }
     Collections.sort(
         triggers,
         new Comparator<Trigger>() {
           @Override
           public int compare(Trigger trigger1, Trigger trigger2) {
             if (trigger1.getPriority() < trigger2.getPriority()) {
               return -1;
             } else if (trigger1.getPriority() > trigger2.getPriority()) {
               return 1;
             } else {
               return 0;
             }
           }
         });
     for (Trigger trigger : triggers) {
       scheduler.triggerJob(trigger.getJobName(), trigger.getJobGroup());
     }
   } catch (SchedulerException se) {
     throw new TaskSystemException(se);
   }
 }
Example #2
0
  /**
   * Inform the <code>JobStore</code> that the scheduler is now firing the given <code>Trigger
   * </code> (executing its associated <code>Job</code>), that it had previously acquired
   * (reserved).
   *
   * @return null if the trigger or its job or calendar no longer exist, or if the trigger was not
   *     successfully put into the 'executing' state.
   */
  public TriggerFiredBundle triggerFired(SchedulingContext ctxt, Trigger trigger)
      throws JobPersistenceException {
    synchronized (m_triggerLock) {
      if (!isEligibleGroupName(trigger.getGroup())) {
        return null;
      }
      WrappedTrigger wt = m_triggersByGroupAndName.get(getTriggerKey(trigger));
      if ((wt != null) && (wt.getState() == TRIGGER_STATE_CLAIMED)) {
        wt.setState(TRIGGER_STATE_EXECUTING);
        Calendar cal = null;
        if (trigger.getCalendarName() != null) {
          cal = retrieveCalendar(ctxt, trigger.getCalendarName());
        }
        Date prevFiredTime = trigger.getPreviousFireTime();
        trigger.triggered(cal);
        TriggerFiredBundle tfb =
            new TriggerFiredBundle(
                retrieveJob(ctxt, trigger.getJobName(), trigger.getJobGroup()),
                trigger,
                cal,
                false,
                new Date(),
                trigger.getPreviousFireTime(),
                prevFiredTime,
                trigger.getNextFireTime());

        return tfb;
      }
      return null;
    }
  }
Example #3
0
 private long getCustomerId(SchedulingContext ctxt, Trigger t) throws JobPersistenceException {
   JobDetail jd = retrieveJob(ctxt, t.getJobName(), t.getJobGroup());
   if (jd == null) {
     return -1L;
   }
   JobDataMap jdMap = jd.getJobDataMap();
   return jdMap.getLong("customerId");
 }
Example #4
0
 private long scoreTrigger(SchedulingContext ctxt, Trigger t) throws JobPersistenceException {
   JobDetail jd = retrieveJob(ctxt, t.getJobName(), t.getJobGroup());
   if (jd == null) {
     return t.getNextFireTime().getTime();
   }
   JobDataMap jdMap = jd.getJobDataMap();
   int taskPriority = jdMap.getInt("taskPriority");
   int custPriority = jdMap.getInt("custPriority");
   long scheduledTime = t.getNextFireTime().getTime();
   return computeScore(taskPriority, custPriority, scheduledTime);
 }
  public static Trigger convertTriggerFromNativeObject(org.quartz.Trigger quartzTrigger) {
    Trigger spagobiTrigger;

    spagobiTrigger = new Trigger();
    spagobiTrigger.setName(quartzTrigger.getName());
    spagobiTrigger.setGroupName(quartzTrigger.getGroup());
    spagobiTrigger.setDescription(quartzTrigger.getDescription());

    // spagobiTrigger.setCalendarName( quartzTrigger.getCalendarName() );
    Assert.assertTrue(
        quartzTrigger.getCalendarName() == null,
        "quartz trigger calendar name is not null: " + quartzTrigger.getCalendarName());

    spagobiTrigger.setStartTime(quartzTrigger.getStartTime());
    spagobiTrigger.setEndTime(quartzTrigger.getEndTime());

    // triggers that run immediately have a generated name that starts with schedule_uuid_ (see
    // TriggerXMLDeserializer)
    // It would be better anyway to relay on a specific property to recognize if a trigger is
    // thinked to run immediately
    spagobiTrigger.setRunImmediately(spagobiTrigger.getName().startsWith("schedule_uuid_"));

    if (quartzTrigger instanceof org.quartz.CronTrigger) {
      org.quartz.CronTrigger quartzCronTrigger = (org.quartz.CronTrigger) quartzTrigger;
      // dirty trick
      String expression = (String) quartzCronTrigger.getJobDataMap().get(SPAGOBI_CRON_EXPRESSION);
      if (expression != null) {
        quartzCronTrigger.getJobDataMap().remove(SPAGOBI_CRON_EXPRESSION);
      } else {
        // for back compatibility
        expression =
            (String) quartzCronTrigger.getJobDataMap().get(SPAGOBI_CRON_EXPRESSION_DEPRECATED);
        quartzCronTrigger.getJobDataMap().remove(SPAGOBI_CRON_EXPRESSION_DEPRECATED);
      }
      spagobiTrigger.setCronExpression(new CronExpression(expression));
    }

    Job job = new Job();
    job.setName(quartzTrigger.getJobName());
    job.setGroupName(quartzTrigger.getJobGroup());
    job.setVolatile(quartzTrigger.isVolatile());
    Map<String, String> parameters =
        convertParametersFromNativeObject(quartzTrigger.getJobDataMap());
    job.addParameters(parameters);

    spagobiTrigger.setJob(job);

    return spagobiTrigger;
  }
Example #6
0
 /**
  * Get all of the Triggers that are associated to the given Job.
  *
  * <p>If there are no matches, a zero-length array should be returned.
  */
 public Trigger[] getTriggersForJob(SchedulingContext ctxt, String jobName, String groupName)
     throws JobPersistenceException {
   // NOTE: this is linear in the number of known triggers - we may want this to be faster
   List<Trigger> triggers = new LinkedList<Trigger>();
   synchronized (m_triggerLock) {
     for (Iterator<WrappedTrigger> it = m_triggersByGroupAndName.values().iterator();
         it.hasNext(); ) {
       WrappedTrigger wt = it.next();
       Trigger t = wt.getTrigger();
       if ((t.getJobGroup().equals(groupName)) && (t.getJobName().equals(jobName))) {
         triggers.add(t);
       }
     }
   }
   return triggers.toArray(new Trigger[triggers.size()]);
 }
  /**
   * 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);
    }
  }