Java Code Examples for org.quartz.spi.OperableTrigger#setJobKey()

The following examples show how to use org.quartz.spi.OperableTrigger#setJobKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JobStoreImplTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testStoreTriggerReplacesTrigger() throws Exception {
  String jobName = "StoreTriggerReplacesTrigger";
  String jobGroup = "StoreTriggerReplacesTriggerGroup";
  JobDetailImpl detail = new JobDetailImpl(jobName, jobGroup, MyJob.class);
  jobStore.storeJob(detail, false);

  String trName = "StoreTriggerReplacesTrigger";
  String trGroup = "StoreTriggerReplacesTriggerGroup";
  OperableTrigger tr = new SimpleTriggerImpl(trName, trGroup, new Date());
  tr.setJobKey(new JobKey(jobName, jobGroup));
  tr.setCalendarName(null);

  jobStore.storeTrigger(tr, false);
  assertEquals(tr, jobStore.retrieveTrigger(tr.getKey()));

  try {
    jobStore.storeTrigger(tr, false);
    fail("an attempt to store duplicate trigger succeeded");
  }
  catch (ObjectAlreadyExistsException oaee) {
    // expected
  }

  tr.setCalendarName("QQ");
  jobStore.storeTrigger(tr, true); //fails here
  assertEquals(tr, jobStore.retrieveTrigger(tr.getKey()));
  assertEquals("StoreJob doesn't replace triggers", "QQ", jobStore.retrieveTrigger(tr.getKey()).getCalendarName());
}
 
Example 2
Source File: AbstractJobStoreTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void testStoreTriggerReplacesTrigger() throws Exception {

  String jobName = "StoreTriggerReplacesTrigger";
  String jobGroup = "StoreTriggerReplacesTriggerGroup";
  JobDetailImpl detail = new JobDetailImpl(jobName, jobGroup, MyJob.class);
  fJobStore.storeJob(detail, false);

  String trName = "StoreTriggerReplacesTrigger";
  String trGroup = "StoreTriggerReplacesTriggerGroup";
  OperableTrigger tr = new SimpleTriggerImpl(trName ,trGroup, new Date());
  tr.setJobKey(new JobKey(jobName, jobGroup));
  tr.setCalendarName(null);

  fJobStore.storeTrigger(tr, false);
  assertEquals(tr,fJobStore.retrieveTrigger(tr.getKey()));

  try {
    fJobStore.storeTrigger(tr, false);
    fail("an attempt to store duplicate trigger succeeded");
  } catch(ObjectAlreadyExistsException oaee) {
    // expected
  }

  tr.setCalendarName("QQ");
  fJobStore.storeTrigger(tr, true); //fails here
  assertEquals(tr, fJobStore.retrieveTrigger(tr.getKey()));
  assertEquals( "StoreJob doesn't replace triggers", "QQ", fJobStore.retrieveTrigger(tr.getKey()).getCalendarName());
}
 
Example 3
Source File: RedisJobStore.java    From redis-quartz with MIT License 5 votes vote down vote up
private void setOperableTriggerFields(TriggerKey triggerKey, Map<String, String> trigger, OperableTrigger operableTrigger) {
	operableTrigger.setKey(triggerKey);
	operableTrigger.setJobKey(new JobKey(trigger.get(JOB_HASH_KEY).split(":")[2], trigger.get(JOB_HASH_KEY).split(":")[1]));
	operableTrigger.setDescription(trigger.get(DESCRIPTION).isEmpty() ? null : trigger.get(DESCRIPTION));
	operableTrigger.setFireInstanceId(trigger.get(FIRE_INSTANCE_ID).isEmpty() ? null : trigger.get(FIRE_INSTANCE_ID));
	operableTrigger.setCalendarName(trigger.get(CALENDAR_NAME).isEmpty() ? null : trigger.get(CALENDAR_NAME));
	operableTrigger.setPriority(Integer.parseInt(trigger.get(PRIORITY)));
	operableTrigger.setMisfireInstruction(Integer.parseInt(trigger.get(MISFIRE_INSTRUCTION)));
	operableTrigger.setStartTime(trigger.get(START_TIME).isEmpty() ? null : new Date(Long.parseLong(trigger.get(START_TIME))));
	operableTrigger.setEndTime(trigger.get(END_TIME).isEmpty() ? null : new Date(Long.parseLong(trigger.get(END_TIME))));
	operableTrigger.setNextFireTime(trigger.get(NEXT_FIRE_TIME).isEmpty() ? null : new Date(Long.parseLong(trigger.get(NEXT_FIRE_TIME))));
	operableTrigger.setPreviousFireTime(trigger.get(PREV_FIRE_TIME).isEmpty() ? null : new Date(Long.parseLong(trigger.get(PREV_FIRE_TIME))));		
}
 
Example 4
Source File: QuartzScheduler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>
 * Add the <code>{@link org.quartz.Job}</code> identified by the given
 * <code>{@link org.quartz.JobDetail}</code> to the Scheduler, and
 * associate the given <code>{@link org.quartz.Trigger}</code> with it.
 * </p>
 * 
 * <p>
 * If the given Trigger does not reference any <code>Job</code>, then it
 * will be set to reference the Job passed with it into this method.
 * </p>
 * 
 * @throws SchedulerException
 *           if the Job or Trigger cannot be added to the Scheduler, or
 *           there is an internal Scheduler error.
 */
public Date scheduleJob(JobDetail jobDetail,
        Trigger trigger) throws SchedulerException {
    validateState();

    if (jobDetail == null) {
        throw new SchedulerException("JobDetail cannot be null");
    }
    
    if (trigger == null) {
        throw new SchedulerException("Trigger cannot be null");
    }
    
    if (jobDetail.getKey() == null) {
        throw new SchedulerException("Job's key cannot be null");
    }

    if (jobDetail.getJobClass() == null) {
        throw new SchedulerException("Job's class cannot be null");
    }
    
    OperableTrigger trig = (OperableTrigger)trigger;

    if (trigger.getJobKey() == null) {
        trig.setJobKey(jobDetail.getKey());
    } else if (!trigger.getJobKey().equals(jobDetail.getKey())) {
        throw new SchedulerException(
            "Trigger does not reference given job!");
    }

    trig.validate();

    Calendar cal = null;
    if (trigger.getCalendarName() != null) {
        cal = resources.getJobStore().retrieveCalendar(trigger.getCalendarName());
    }
    Date ft = trig.computeFirstFireTime(cal);

    if (ft == null) {
        throw new SchedulerException(
                "Based on configured schedule, the given trigger '" + trigger.getKey() + "' will never fire.");
    }

    resources.getJobStore().storeJobAndTrigger(jobDetail, trig);
    notifySchedulerListenersJobAdded(jobDetail);
    notifySchedulerThread(trigger.getNextFireTime().getTime());
    notifySchedulerListenersSchduled(trigger);

    return ft;
}
 
Example 5
Source File: QuartzScheduler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>
 * Remove (delete) the <code>{@link org.quartz.Trigger}</code> with the
 * given name, and store the new given one - which must be associated
 * with the same job.
 * </p>
 * @param newTrigger
 *          The new <code>Trigger</code> to be stored.
 * 
 * @return <code>null</code> if a <code>Trigger</code> with the given
 *         name & group was not found and removed from the store, otherwise
 *         the first fire time of the newly scheduled trigger.
 */
public Date rescheduleJob(TriggerKey triggerKey,
        Trigger newTrigger) throws SchedulerException {
    validateState();

    if (triggerKey == null) {
        throw new IllegalArgumentException("triggerKey cannot be null");
    }
    if (newTrigger == null) {
        throw new IllegalArgumentException("newTrigger cannot be null");
    }

    OperableTrigger trig = (OperableTrigger)newTrigger;
    Trigger oldTrigger = getTrigger(triggerKey);
    if (oldTrigger == null) {
        return null;
    } else {
        trig.setJobKey(oldTrigger.getJobKey());
    }
    trig.validate();

    Calendar cal = null;
    if (newTrigger.getCalendarName() != null) {
        cal = resources.getJobStore().retrieveCalendar(
                newTrigger.getCalendarName());
    }
    Date ft = trig.computeFirstFireTime(cal);

    if (ft == null) {
        throw new SchedulerException(
                "Based on configured schedule, the given trigger will never fire.");
    }
    
    if (resources.getJobStore().replaceTrigger(triggerKey, trig)) {
        notifySchedulerThread(newTrigger.getNextFireTime().getTime());
        notifySchedulerListenersUnscheduled(triggerKey);
        notifySchedulerListenersSchduled(newTrigger);
    } else {
        return null;
    }

    return ft;
    
}