/** Creates a SimpleTrigger with an interval computed from the hours, minutes and seconds. */ private static AbstractTrigger<?> createTrigger(ScheduledTask task, Recurrence.Often recurrence) throws FrameworkException, ApplicationExceptions { // Compute the repeatInterval for the SimpleTrigger in milliseconds long repeatInterval = 0; if (recurrence.getHours() != null) repeatInterval += recurrence.getHours() * 60 * 60 * 1000; if (recurrence.getMinutes() != null) repeatInterval += recurrence.getMinutes() * 60 * 1000; if (recurrence.getSeconds() != null) repeatInterval += recurrence.getSeconds() * 1000; SimpleTriggerImpl trigger = new SimpleTriggerImpl(); if (repeatInterval > 0) { trigger.setRepeatInterval(repeatInterval); trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); } return trigger; }
@Test public void testScheduleJobWithRepeatTime() throws Exception { JobDetail job1 = buildJob("Job1", DEFAULT_GROUP, MyJob.class); final SimpleTriggerImpl o = (SimpleTriggerImpl) buildTrigger("key1", DEFAULT_GROUP, job1); o.setRepeatInterval(100); o.setRepeatCount(10); scheduler.scheduleJob(job1, o); Thread.sleep(750); assertEquals(MyJob.count, 8); assertEquals(MyJob.triggerKeys.poll(), "key1"); }
@Test public void testScheduleJobWithNoRepeatTime() throws Exception { JobDetail job1 = buildJob("Job11", DEFAULT_GROUP, MyJob.class); final SimpleTriggerImpl o = (SimpleTriggerImpl) buildTrigger("key11", DEFAULT_GROUP, job1); o.setRepeatInterval(100); o.setRepeatCount(0); scheduler.scheduleJob(job1, o); // it really needs to wait to be sure that it is not found as a lost trigger Thread.sleep(60000); assertEquals(MyJob.count, 1); assertEquals(MyJob.triggerKeys.poll(), "key11"); }
@Test public void testScheduleJobWithRepeatTimeWithConcurrentExecutionDisallowed() throws Exception { JobDetail job1 = buildJob("CJob1", DEFAULT_GROUP, MyNoConcurrentJob.class); final SimpleTriggerImpl o = (SimpleTriggerImpl) buildTrigger("Ckey1", DEFAULT_GROUP, job1); o.setRepeatInterval(100); o.setRepeatCount(10); MyNoConcurrentJob.waitTime = 300; scheduler.scheduleJob(job1, o); Thread.sleep(850); // since MyNoCocurrent job takes 300 ms to finish assertEquals(MyNoConcurrentJob.count, 3); assertEquals(MyNoConcurrentJob.triggerKeys.poll(), "Ckey1"); }
@Test public void testScheduleJobWithRepeatTimeWithConcurrentExecutionDisallowedAfterEnd() throws Exception { JobDetail job1 = buildJob("CJob1", DEFAULT_GROUP, MyNoConcurrentJob.class); final SimpleTriggerImpl o = (SimpleTriggerImpl) buildTrigger("Ckey1", DEFAULT_GROUP, job1); o.setRepeatInterval(100); o.setRepeatCount(1); MyNoConcurrentJob.waitTime = 200; scheduler.scheduleJob(job1, o); Thread.sleep(750); // since MyNoCocurrent job takes 300 ms to finish assertEquals(MyNoConcurrentJob.count, 2); assertEquals(MyNoConcurrentJob.triggerKeys.poll(), "Ckey1"); }
@Test public void testScheduleJobWithRepeatTimeWithConcurrentExecutionDisallowed_withFastJob() throws Exception { JobDetail job1 = buildJob("CJob2", DEFAULT_GROUP, MyNoConcurrentJob.class); final SimpleTriggerImpl o = (SimpleTriggerImpl) buildTrigger("Ckey2", DEFAULT_GROUP, job1); o.setRepeatInterval(100); o.setRepeatCount(10); MyNoConcurrentJob.waitTime = 10; scheduler.scheduleJob(job1, o); Thread.sleep(750); // since MyNoCocurrent job takes 300 ms to finish assertEquals(MyNoConcurrentJob.count, 8); assertEquals(MyNoConcurrentJob.triggerKeys.poll(), "Ckey2"); }
@Test public void serialization(){ SimpleTrigger trigger = TriggerBuilder.newTrigger() .forJob("testJob", "testGroup") .withIdentity("testTrigger", "testTriggerGroup") .usingJobData("timeout", 5) .withDescription("A description!") .withSchedule(SimpleScheduleBuilder.repeatHourlyForever()) .build(); Map<String, String> triggerMap = mapper.convertValue(trigger, new TypeReference<HashMap<String, String>>() {}); assertThat(triggerMap, hasKey("name")); assertEquals("testTrigger", triggerMap.get("name")); assertThat(triggerMap, hasKey("group")); assertEquals("testTriggerGroup", triggerMap.get("group")); assertThat(triggerMap, hasKey("jobName")); assertEquals("testJob", triggerMap.get("jobName")); SimpleTriggerImpl simpleTrigger = mapper.convertValue(triggerMap, SimpleTriggerImpl.class); assertEquals(trigger.getKey().getName(), simpleTrigger.getKey().getName()); assertEquals(trigger.getKey().getGroup(), simpleTrigger.getKey().getGroup()); assertEquals(trigger.getStartTime(), simpleTrigger.getStartTime()); assertEquals(trigger.getRepeatInterval(), simpleTrigger.getRepeatInterval()); }
protected void scheduleMessage(PersistedMessageBO message) throws SchedulerException { LOG.debug("Scheduling execution of a delayed asynchronous message."); Scheduler scheduler = KSBServiceLocator.getScheduler(); JobDataMap jobData = new JobDataMap(); jobData.put(MessageServiceExecutorJob.MESSAGE_KEY, message); JobDetailImpl jobDetail = new JobDetailImpl("Delayed_Asynchronous_Call-" + Math.random(), "Delayed_Asynchronous_Call", MessageServiceExecutorJob.class); jobDetail.setJobDataMap(jobData); scheduler.getListenerManager().addJobListener( new MessageServiceExecutorJobListener()); SimpleTriggerImpl trigger = new SimpleTriggerImpl("Delayed_Asynchronous_Call_Trigger-" + Math.random(), "Delayed_Asynchronous_Call", message.getQueueDate()); trigger.setJobDataMap(jobData);// 1.6 bug required or derby will choke scheduler.scheduleJob(jobDetail, trigger); }
public void scheduleExecution(Throwable throwable, PersistedMessageBO message, String description) throws Exception { KSBServiceLocator.getMessageQueueService().delete(message); PersistedMessageBO messageCopy = message.copy(); Scheduler scheduler = KSBServiceLocator.getScheduler(); JobDataMap jobData = new JobDataMap(); jobData.put(MessageServiceExecutorJob.MESSAGE_KEY, messageCopy); JobDetailImpl jobDetail = new JobDetailImpl("Exception_Message_Job " + Math.random(), "Exception Messaging", MessageServiceExecutorJob.class); jobDetail.setJobDataMap(jobData); if (!StringUtils.isBlank(description)) { jobDetail.setDescription(description); } scheduler.getListenerManager().addJobListener( new MessageServiceExecutorJobListener()); SimpleTriggerImpl trigger = new SimpleTriggerImpl("Exception_Message_Trigger " + Math.random(), "Exception Messaging", messageCopy .getQueueDate()); trigger.setJobDataMap(jobData);// 1.6 bug required or derby will choke scheduler.scheduleJob(jobDetail, trigger); }
@Test public void shouldDeleteAssociatedTriggersWhenDeletingJob() throws JobPersistenceException { final String jobId = id("job"); JobDetail job = newJob(DummyJobListener.class) .withIdentity(jobId, "bargroup") .build(); couchdbStore.storeJob(job, false); final String tridderName = id("trigger"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(tridderName, "borgroup") .forJob(JobKey.jobKey(jobId, "bargroup")) .startAt(new Date(2010, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); couchdbStore.storeTrigger(trigger, false); couchdbStore.removeJob(JobKey.jobKey(jobId, "bargroup")); assertNull(couchdbStore.retrieveTrigger(TriggerKey.triggerKey(tridderName, "borgroup"))); }
@Test public void shouldStoreAndRetrieveTrigger() throws JobPersistenceException { final String triggerName = id("fuuid"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); couchdbStore.storeTrigger(trigger, false); assertNull(couchdbStore.retrieveTrigger(TriggerKey.triggerKey("something", "something"))); assertEquals(new Date(2010, 10, 20), couchdbStore.retrieveTrigger(TriggerKey.triggerKey(triggerName, "borgroup")).getStartTime()); }
@Test public void shouldDeleteExistingTrigger() throws JobPersistenceException { final String triggerName = id("fuuid"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); couchdbStore.storeTrigger(trigger, false); couchdbStore.removeTrigger(TriggerKey.triggerKey(triggerName, "borgroup")); assertNull(couchdbStore.retrieveTrigger(TriggerKey.triggerKey(triggerName, "borgroup"))); }
@Test public void shouldCheckWhetherTriggerExists() throws JobPersistenceException { final String triggerName = id("fuuid"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); assertFalse(couchdbStore.checkExists(TriggerKey.triggerKey(triggerName, "borgroup"))); couchdbStore.storeTrigger(trigger, false); assertTrue(couchdbStore.checkExists(TriggerKey.triggerKey(triggerName, "borgroup"))); }
@Test public void shouldFireTriggers() throws JobPersistenceException { final String triggerName = id("fuuid1"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup1") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010 - 1900, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); trigger.computeFirstFireTime(null); couchdbStore.storeTrigger(trigger, false); List<TriggerFiredResult> firedResults = couchdbStore.triggersFired(Arrays.<OperableTrigger>asList(trigger)); assertEquals(1, firedResults.size()); assertEquals(TriggerKey.triggerKey(triggerName, "borgroup1"), firedResults.get(0).getTriggerFiredBundle().getTrigger().getKey()); }
@Test public void shouldUpdateTriggerForMisfireNowWithExistingRepeatCount() throws Exception { String triggerName = id("fuuid"); DateTime now = new DateTime(); DateTime startTime = now.minusMinutes(1); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(startTime.toDate()) .withSchedule(simpleSchedule() .withIntervalInMinutes(1) .withRepeatCount(2) .withMisfireHandlingInstructionNowWithExistingCount()) .build(); trigger.computeFirstFireTime(null); couchdbStore.storeTrigger(trigger, false); List<OperableTrigger> acquiredTriggers = couchdbStore.acquireNextTriggers(now.getMillis(), 1, 0); assertEquals(1, acquiredTriggers.size()); SimpleTrigger acquiredTrigger = (SimpleTrigger) acquiredTriggers.get(0); assertTrue(acquiredTrigger.getNextFireTime().getTime() - now.getMillis() < 5000); assertEquals(2, acquiredTrigger.getRepeatCount()); }
@Test public void shouldUpdateTriggerForMisfireNextWithRemainingRepeatCount() throws Exception { String triggerName = id("fuuid"); DateTime now = new DateTime(); DateTime startTime = now.minusMinutes(1); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(startTime.toDate()) .withSchedule(simpleSchedule() .withIntervalInMinutes(1) .withRepeatCount(2) .withMisfireHandlingInstructionNowWithRemainingCount()) .build(); trigger.computeFirstFireTime(null); couchdbStore.storeTrigger(trigger, false); List<OperableTrigger> acquiredTriggers = couchdbStore.acquireNextTriggers(now.getMillis(), 1, 0); assertEquals(1, acquiredTriggers.size()); SimpleTrigger acquiredTrigger = (SimpleTrigger) acquiredTriggers.get(0); assertTrue(acquiredTrigger.getNextFireTime().getTime() - now.plusMinutes(1).getMillis() < 5000); assertEquals(1, acquiredTrigger.getRepeatCount()); }
@Test public void shouldGetTriggerState() throws Exception { final String triggerName = id("fuuid1"); final String triggerGroup = id("borgroup1"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, triggerGroup) .forJob(JobKey.jobKey(id("fooid"), id("bargroup"))) .startAt(new Date(2010 - 1900, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .withRepeatCount(0)) .build(); trigger.computeFirstFireTime(null); couchdbStore.storeTrigger(trigger, false); assertEquals(Trigger.TriggerState.NORMAL, couchdbStore.getTriggerState(TriggerKey.triggerKey(triggerName, triggerGroup))); }
@Test public void shouldUpdateTriggerStateToAcquired() throws JobPersistenceException { final String triggerName = id("fuuid1"); final String triggerGroup = id("borgroup1"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, triggerGroup) .forJob(JobKey.jobKey(id("fooid"), id("bargroup"))) .startAt(new Date(2010 - 1900, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .withRepeatCount(0)) .build(); trigger.computeFirstFireTime(null); couchdbStore.storeTrigger(trigger, false); CouchDbTrigger storedTrigger = new CouchDbSimpleTrigger((SimpleTriggerImpl) couchdbStore.retrieveTrigger(TriggerKey.triggerKey(triggerName, triggerGroup))); assertEquals(CouchDbTriggerState.WAITING, storedTrigger.getState()); couchdbStore.acquireNextTriggers(new Date().getTime(), 1, 0); CouchDbSimpleTrigger acquiredTrigger = (CouchDbSimpleTrigger) couchdbStore.getTriggerStore().getTriggerByKey(TriggerKey.triggerKey(triggerName, triggerGroup)); assertEquals(CouchDbTriggerState.ACQUIRED, acquiredTrigger.getState()); }
@Test public void shouldReleaseAcquiredTriggers() throws JobPersistenceException { String triggerName = id("fuuid1"); String triggerGroup = id("borgroup1"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, triggerGroup) .forJob(JobKey.jobKey(id("fooid"), id("bargroup"))) .startAt(new Date(2010 - 1900, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .withRepeatCount(0)) .build(); trigger.computeFirstFireTime(null); couchdbStore.getTriggerStore().storeTrigger(new CouchDbSimpleTrigger(trigger, CouchDbTriggerState.ACQUIRED), false); couchdbStore.releaseAcquiredTrigger(trigger); CouchDbTrigger dbTrigger = couchdbStore.getTriggerStore().getTriggerByKey(TriggerKey.triggerKey(triggerName, triggerGroup)); assertEquals(CouchDbTriggerState.WAITING, dbTrigger.getState()); }
@Test public void shouldReleaseAcquiredTriggersAfterFiring() throws JobPersistenceException { String triggerName = id("fuuid1"); String triggerGroup = id("borgroup1"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, triggerGroup) .forJob(JobKey.jobKey(id("fooid"), id("bargroup"))) .startAt(new Date(2010 - 1900, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .withRepeatCount(0)) .build(); trigger.computeFirstFireTime(null); couchdbStore.getTriggerStore().storeTrigger(new CouchDbSimpleTrigger(trigger, CouchDbTriggerState.ACQUIRED), false); couchdbStore.releaseAcquiredTrigger(trigger); CouchDbTrigger dbTrigger = couchdbStore.getTriggerStore().getTriggerByKey(TriggerKey.triggerKey(triggerName, triggerGroup)); assertEquals(CouchDbTriggerState.WAITING, dbTrigger.getState()); }
protected OperableTrigger createRecoveryTrigger(TriggerWrapper tw, JobWrapper jw, String name, FiredTrigger recovering) { final SimpleTriggerImpl recoveryTrigger = new SimpleTriggerImpl(name, Scheduler.DEFAULT_RECOVERY_GROUP, new Date(recovering.getScheduledFireTime())); recoveryTrigger.setJobName(jw.getKey().getName()); recoveryTrigger.setJobGroup(jw.getKey().getGroup()); recoveryTrigger.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY); recoveryTrigger.setPriority(tw.getPriority()); return recoveryTrigger; }
/** * Build the actual Trigger -- NOT intended to be invoked by end users, * but will rather be invoked by a TriggerBuilder which this * ScheduleBuilder is given to. * * @see TriggerBuilder#withSchedule(ScheduleBuilder) */ @Override public MutableTrigger build() { SimpleTriggerImpl st = new SimpleTriggerImpl(); st.setRepeatInterval(interval); st.setRepeatCount(repeatCount); st.setMisfireInstruction(misfireInstruction); return st; }
public static OperableTrigger newTrigger(CompositeData cData) throws ParseException { SimpleTriggerImpl result = new SimpleTriggerImpl(); result.setRepeatCount(((Integer) cData.get("repeatCount")).intValue()); result.setRepeatInterval(((Long) cData.get("repeatInterval")).longValue()); result.setTimesTriggered(((Integer) cData.get("timesTriggered")).intValue()); TriggerSupport.initializeTrigger(result, cData); return result; }
public static OperableTrigger newTrigger(Map<String, Object> attrMap) throws ParseException { SimpleTriggerImpl result = new SimpleTriggerImpl(); if(attrMap.containsKey("repeatCount")) { result.setRepeatCount(((Integer) attrMap.get("repeatCount")).intValue()); } if(attrMap.containsKey("repeatInterval")) { result.setRepeatInterval(((Long) attrMap.get("repeatInterval")).longValue()); } if(attrMap.containsKey("timesTriggered")) { result.setTimesTriggered(((Integer) attrMap.get("timesTriggered")).intValue()); } TriggerSupport.initializeTrigger(result, attrMap); return result; }
public void addReminderJob(TaskReminder reminder,ProcessInstance processInstance,Task task) { try { AbstractTrigger<? extends Trigger> trigger=null; if(reminder.getType().equals(ReminderType.Once)){ SimpleTriggerImpl simpleTrigger=new SimpleTriggerImpl(); simpleTrigger.setRepeatCount(0); trigger=simpleTrigger; long executeTime=reminder.getStartDate().getTime()+10000; long now=(new Date()).getTime(); if(executeTime<=now){ return; } }else{ CronTriggerImpl cronTrigger=new CronTriggerImpl(); cronTrigger.setCronExpression(reminder.getCron()); trigger=cronTrigger; } trigger.setName("trigger_"+reminder.getId()); trigger.setStartTime(reminder.getStartDate()); ReminderJobDetail jobDetail=new ReminderJobDetail(); jobDetail.setJobClass(ReminderJob.class); ReminderHandler handler=(ReminderHandler)applicationContext.getBean(reminder.getReminderHandlerBean()); jobDetail.setReminderHandlerBean(handler); if(task==null){ task=taskService.getTask(reminder.getTaskId()); } jobDetail.setTask(task); jobDetail.setProcessInstance(processService.getProcessInstanceById(task.getProcessInstanceId())); jobDetail.setKey(new JobKey(JOB_NAME_PREFIX+reminder.getId(),JOB_GROUP_PREFIX)); Calendar calendar=getCalendar(reminder,processInstance,task); if(calendar!=null){ String calendarName=REMINDER_CALENDAR_PREFIX+reminder.getId(); scheduler.addCalendar(calendarName, calendar,false, false); trigger.setCalendarName(calendarName); } scheduler.scheduleJob(jobDetail, trigger); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void afterPropertiesSet() { if (this.name == null) { this.name = this.beanName; } if (this.group == null) { this.group = Scheduler.DEFAULT_GROUP; } if (this.jobDetail != null) { this.jobDataMap.put("jobDetail", this.jobDetail); } if (this.startDelay > 0 || this.startTime == null) { this.startTime = new Date(System.currentTimeMillis() + this.startDelay); } SimpleTriggerImpl sti = new SimpleTriggerImpl(); sti.setName(this.name); sti.setGroup(this.group); sti.setJobKey(this.jobDetail.getKey()); sti.setJobDataMap(this.jobDataMap); sti.setStartTime(this.startTime); sti.setRepeatInterval(this.repeatInterval); sti.setRepeatCount(this.repeatCount); sti.setPriority(this.priority); sti.setMisfireInstruction(this.misfireInstruction); sti.setDescription(this.description); this.simpleTrigger = sti; }
@Test public void testTriggerStates() throws Exception { JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("job1", "jobGroup1").storeDurably(true).build(); this.jobStore.storeJob(jobDetail, false); OperableTrigger trigger = new SimpleTriggerImpl("trigger1", "triggerGroup1", jobDetail.getKey().getName(), jobDetail.getKey().getGroup(), new Date(System.currentTimeMillis() + 100000), new Date(System.currentTimeMillis() + 200000), 2, 2000); trigger.computeFirstFireTime(null); assertEquals(TriggerState.NONE, this.jobStore.getTriggerState(trigger.getKey())); this.jobStore.storeTrigger(trigger, false); assertEquals(TriggerState.NORMAL, this.jobStore.getTriggerState(trigger.getKey())); this.jobStore.pauseTrigger(trigger.getKey()); assertEquals(TriggerState.PAUSED, this.jobStore.getTriggerState(trigger.getKey())); this.jobStore.resumeTrigger(trigger.getKey()); assertEquals(TriggerState.NORMAL, this.jobStore.getTriggerState(trigger.getKey())); trigger = this.jobStore.acquireNextTriggers( new Date(trigger.getNextFireTime().getTime()).getTime() + 10000, 1, 1L).get(0); assertNotNull(trigger); this.jobStore.releaseAcquiredTrigger(trigger); trigger = this.jobStore.acquireNextTriggers( new Date(trigger.getNextFireTime().getTime()).getTime() + 10000, 1, 1L).get(0); assertNotNull(trigger); assertTrue(this.jobStore.acquireNextTriggers( new Date(trigger.getNextFireTime().getTime()).getTime() + 10000, 1, 1L).isEmpty()); }
@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()); }
private OperableTrigger toOperableTrigger(TriggerKey triggerKey, Map<String, String> trigger) { if (TRIGGER_TYPE_SIMPLE.equals(trigger.get(TRIGGER_TYPE))) { SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl(); setOperableTriggerFields(triggerKey, trigger, simpleTrigger); if (trigger.get(REPEAT_COUNT) != null && !trigger.get(REPEAT_COUNT).isEmpty()) simpleTrigger.setRepeatCount(Integer.parseInt(trigger.get(REPEAT_COUNT))); if (trigger.get(REPEAT_INTERVAL) != null && !trigger.get(REPEAT_INTERVAL).isEmpty()) simpleTrigger.setRepeatInterval(Long.parseLong(trigger.get(REPEAT_INTERVAL))); if (trigger.get(TIMES_TRIGGERED) != null && !trigger.get(TIMES_TRIGGERED).isEmpty()) simpleTrigger.setTimesTriggered(Integer.parseInt(trigger.get(TIMES_TRIGGERED))); return simpleTrigger; } else if (TRIGGER_TYPE_CRON.equals(trigger.get(TRIGGER_TYPE))) { CronTriggerImpl cronTrigger = new CronTriggerImpl(); setOperableTriggerFields(triggerKey, trigger, cronTrigger); if (trigger.get(TIME_ZONE_ID) != null && !trigger.get(TIME_ZONE_ID).isEmpty()) cronTrigger.getTimeZone().setID(trigger.get(TIME_ZONE_ID).isEmpty() ? null : trigger.get(TIME_ZONE_ID)); try { if (trigger.get(CRON_EXPRESSION) != null && !trigger.get(CRON_EXPRESSION).isEmpty()) cronTrigger.setCronExpression(trigger.get(CRON_EXPRESSION).isEmpty() ? null : trigger.get(CRON_EXPRESSION)); } catch (ParseException ex) { log.warn("could not parse cron_expression: " + trigger.get(CRON_EXPRESSION) + " for trigger: " + createTriggerHashKey(triggerKey.getGroup(), triggerKey.getName())); } return cronTrigger; } else { // other trigger types are not supported throw new UnsupportedOperationException(); } }
private void scheduleJob() { LOG.debug("Queueing processing job"); try { Scheduler scheduler = KSBServiceLocator.getScheduler(); if (synchronous) { LOG.debug("Invoking job synchronously in Thread " + Thread.currentThread()); MessageProcessingJob job = new MessageProcessingJob(messageId, mode, user, cause); job.run(); } else { String uniqueTriggerName = jobName + "-Trigger-" + System.currentTimeMillis() + Math.random(); SimpleTriggerImpl trigger = new SimpleTriggerImpl(uniqueTriggerName, jobGroup + "-Trigger"); LOG.debug("Scheduling trigger: " + trigger); JobDataMap data = new JobDataMap(); data.put("mode", mode.name()); data.put("user", user); data.put("cause", cause); data.put("messageId", messageId); trigger.setJobName(jobName); trigger.setJobGroup(jobGroup); trigger.setJobDataMap(data); scheduler.scheduleJob(trigger); } } catch (SchedulerException se) { throw new RuntimeException(se); } }
@Test() public void testTriggerStates() throws Exception { this.jobStore.storeJob(fJobDetail, false); final OperableTrigger trigger = new SimpleTriggerImpl("trigger1", "triggerGroup1", this.fJobDetail.getName(), this.fJobDetail.getGroup(), new Date(System.currentTimeMillis() + 100000), new Date( System.currentTimeMillis() + 200000), 2, 2000); trigger.computeFirstFireTime(null); assertEquals(Trigger.TriggerState.NONE, this.jobStore.getTriggerState(trigger.getKey())); this.jobStore.storeTrigger(trigger, false); assertEquals(Trigger.TriggerState.NORMAL, this.jobStore.getTriggerState(trigger.getKey())); this.jobStore.pauseTrigger(trigger.getKey()); assertEquals(Trigger.TriggerState.PAUSED, this.jobStore.getTriggerState(trigger.getKey())); this.jobStore.resumeTrigger(trigger.getKey()); assertEquals(Trigger.TriggerState.NORMAL, this.jobStore.getTriggerState(trigger.getKey())); final OperableTrigger rTrigger1 = this.jobStore.acquireNextTriggers( new Date(trigger.getNextFireTime().getTime()).getTime() + 10000, 1, 1L) .get(0); assertNotNull(rTrigger1); this.jobStore.releaseAcquiredTrigger(rTrigger1); final OperableTrigger rTrigger2 = this.jobStore.acquireNextTriggers( new Date(rTrigger1.getNextFireTime().getTime()).getTime() + 10000, 1, 1L).get(0); assertNotNull(rTrigger2); assertTrue(this.jobStore.acquireNextTriggers( new Date(rTrigger2.getNextFireTime().getTime()).getTime() + 10000, 1, 1L).isEmpty()); }
@Test() public void testStoreTriggerReplacesTrigger() throws Exception { String jobName = "StoreTriggerReplacesTrigger"; String jobGroup = "StoreTriggerReplacesTriggerGroup"; JobDetailImpl detail = new JobDetailImpl(jobName, jobGroup, NoOpJob.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("QQ", jobStore.retrieveTrigger(tr.getKey()).getCalendarName(), "StoreJob doesn't replace triggers"); }
private CouchDbTrigger createCouchDbTrigger(OperableTrigger newTrigger) { CouchDbTrigger couchdbTrigger = null; if (newTrigger instanceof SimpleTriggerImpl) { couchdbTrigger = new CouchDbSimpleTrigger((SimpleTriggerImpl) newTrigger); } else if (newTrigger instanceof CronTriggerImpl) { couchdbTrigger = new CouchDbCronTrigger((CronTriggerImpl) newTrigger); } else if (newTrigger instanceof CalendarIntervalTriggerImpl) { couchdbTrigger = new CouchDbCalendarIntervalTrigger((CalendarIntervalTriggerImpl) newTrigger); } return couchdbTrigger; }
@Test public void shouldDeleteAllJobsTriggersAndCalendars() throws JobPersistenceException { JobDetail job = newJob(JobListener.class) .withIdentity(id("fooid"), id("bargroup")) .usingJobData("foo", "bar") .build(); couchdbStore.storeJob(job, false); Calendar testCalendar = new WeeklyCalendar(); String calendarName = id("weeklyCalendar"); couchdbStore.storeCalendar(calendarName, testCalendar, false, false); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(id("fuuid"), id("borgroup")) .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .modifiedByCalendar(calendarName) .build(); couchdbStore.storeTrigger(trigger, false); couchdbStore.clearAllSchedulingData(); assertEquals(0, couchdbStore.getNumberOfJobs()); assertEquals(0, couchdbStore.getNumberOfCalendars()); assertEquals(0, couchdbStore.getNumberOfTriggers()); assertEquals(0, couchdbStore.getJobGroupNames().size()); assertEquals(0, couchdbStore.getCalendarNames().size()); }
@Test public void shouldDeleteExistingTriggers() throws JobPersistenceException { final String triggerName = id("fuuid1"); SimpleTriggerImpl trigger1 = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); final String triggerName2 = id("fuuid2"); SimpleTriggerImpl trigger2 = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName2, "borgroup") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); couchdbStore.storeTrigger(trigger1, false); couchdbStore.storeTrigger(trigger2, false); couchdbStore.removeTriggers(asList( TriggerKey.triggerKey(triggerName, "borgroup"), TriggerKey.triggerKey(triggerName2, "borgroup") )); assertNull(couchdbStore.retrieveTrigger(TriggerKey.triggerKey(triggerName, "borgroup"))); assertNull(couchdbStore.retrieveTrigger(TriggerKey.triggerKey(triggerName2, "borgroup"))); }
@Test public void shouldNotReplaceNonExistingTrigger() throws JobPersistenceException { final String triggerName = id("fuuid"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); assertFalse(couchdbStore.replaceTrigger(TriggerKey.triggerKey(triggerName, "borgroup"), trigger)); }
@Test public void shouldReturnTriggerState() throws JobPersistenceException { final String triggerName = id("fuuid1"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup1") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); couchdbStore.storeTrigger(trigger, false); assertEquals(Trigger.TriggerState.NORMAL, couchdbStore.getTriggerState(TriggerKey.triggerKey(triggerName, "borgroup1"))); }
@Test public void shouldAcquireTriggersToFire() throws JobPersistenceException { int numberOfTriggers = couchdbStore.acquireNextTriggers(new Date(2010 - 1900, 10, 21).getTime(), Integer.MAX_VALUE, 0).size(); final String triggerName1 = id("fuuid1"); SimpleTriggerImpl trigger1 = (SimpleTriggerImpl) newTrigger().withIdentity(triggerName1, "borgroup1") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010 - 1900, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); trigger1.computeFirstFireTime(null); couchdbStore.storeTrigger(trigger1, false); final String triggerName2 = id("fuuid2"); SimpleTriggerImpl trigger2 = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName2, "borgroup2") .forJob(JobKey.jobKey("fooid", "bargroup")) .startAt(new Date(2010 - 1900, 10, 22)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .repeatForever()) .build(); trigger2.computeFirstFireTime(null); couchdbStore.storeTrigger(trigger2, false); List<OperableTrigger> triggers = couchdbStore.acquireNextTriggers(new Date(2010 - 1900, 10, 21).getTime(), Integer.MAX_VALUE, 0); assertEquals(numberOfTriggers + 1, triggers.size()); assertEquals(TriggerKey.triggerKey(triggerName1, "borgroup1"), triggers.get(0).getKey()); }
@Test public void shouldDeleteTriggerAfterFire() throws JobPersistenceException { final String jobName = id("job"); JobDetail job = newJob(CouchDbStoreCalendarRepeatingJobIT.TestListener.class) .withIdentity(jobName, "bargroup") .usingJobData("foo", "bar") .usingJobData("fuu", "baz") .build(); couchdbStore.storeJob(job, false); final String triggerName = id("fuuid1"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, "borgroup1") .forJob(JobKey.jobKey(jobName, "bargroup")) .startAt(new Date(2010 - 1900, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .withRepeatCount(0)) .build(); trigger.computeFirstFireTime(null); couchdbStore.storeTrigger(trigger, false); trigger.triggered(null); couchdbStore.triggeredJobComplete(trigger, job, null); assertNull(couchdbStore.retrieveTrigger(TriggerKey.triggerKey(triggerName, "borgroup1"))); }
@Test public void shouldAcquireWaitingTriggersOnly() throws JobPersistenceException { String triggerName = id("fuuid1"); String triggerGroup = id("borgroup1"); SimpleTriggerImpl trigger = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName, triggerGroup) .forJob(JobKey.jobKey(id("fooid"), id("bargroup"))) .startAt(new Date(2010 - 1900, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .withRepeatCount(0)) .build(); trigger.computeFirstFireTime(null); couchdbStore.storeTrigger(trigger, false); String triggerName2 = id("fuuid2"); String triggerGroup2 = id("borgroup2"); SimpleTriggerImpl trigger2 = (SimpleTriggerImpl) newTrigger() .withIdentity(triggerName2, triggerGroup2) .forJob(JobKey.jobKey(id("fooid"), id("bargroup"))) .startAt(new Date(2010 - 1900, 10, 20)) .withSchedule(simpleSchedule() .withIntervalInMinutes(2) .withRepeatCount(0)) .build(); trigger2.computeFirstFireTime(null); CouchDbSimpleTrigger couchdbTrigger2 = new CouchDbSimpleTrigger(trigger2); couchdbTrigger2.setState(CouchDbTriggerState.ACQUIRED); couchdbStore.getTriggerStore().storeTrigger(couchdbTrigger2, true); List<OperableTrigger> acquiredTriggers = couchdbStore.acquireNextTriggers(new Date().getTime(), 2, 0); assertEquals(1, acquiredTriggers.size()); assertEquals(TriggerKey.triggerKey(triggerName, triggerGroup), acquiredTriggers.get(0).getKey()); }