/** 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; }
/** The following logic is used to build the cron expression. * seconds: Set to startOnSecond. * minutes: Set to startOnMinute. * hours: Set to startOnHour. * day-of-month: Set to No specific value. * month: All values * day-of-week: Set to MON-FRI, if weekDays is true. Else set to All values. */ private static AbstractTrigger<?> createTrigger(ScheduledTask task, Recurrence.Daily recurrence) throws FrameworkException, ApplicationExceptions { StringBuilder buf = new StringBuilder(); // startOn values for seconds, minutes and hours buf.append(task.getStartOn().second()) .append(' ') .append(task.getStartOn().minute()) .append(' ') .append(task.getStartOn().hourOfDay()); // No specific value for day-of-month buf.append(' ').append('?'); // All values for month buf.append(' ').append('*'); // Compute day-of-week buf.append(' '); if (recurrence.getWeekDaysOnly() != null && recurrence.getWeekDaysOnly()) buf.append("MON-FRI"); else buf.append('*'); return createCronTrigger(buf.toString()); }
/** The following logic is used to build the cron expression. * seconds: Set to startOnSecond. * minutes: Set to startOnMinute. * hours: Set to startOnHour. * day-of-month: Set to No specific value. * month: All values * day-of-week: If frequency equals LAST, set to {day}#L. Else set to {day}#{frequency}. */ private static AbstractTrigger<?> createTrigger(ScheduledTask task, Recurrence.Weekly recurrence) throws FrameworkException, ApplicationExceptions { StringBuilder buf = new StringBuilder(); // startOn values for seconds, minutes and hours buf.append(task.getStartOn().second()) .append(' ') .append(task.getStartOn().minute()) .append(' ') .append(task.getStartOn().hourOfDay()); // No specific value for day-of-month buf.append(' ').append('?'); // All values for month buf.append(' ').append('*'); // Compute day-of-week buf.append(' ').append(recurrence.getDay().ordinal() + 1); if (recurrence.getFrequency() == Recurrence.WeekFrequency.LAST) buf.append('L'); else if (recurrence.getFrequency() != Recurrence.WeekFrequency.EVERY) buf.append('#').append(recurrence.getFrequency().ordinal() + 1); return createCronTrigger(buf.toString()); }
/** The following logic is used to build the cron expression. * seconds: Set to startOnSecond. * minutes: Set to startOnMinute. * hours: Set to startOnHour. * day-of-month: Set to 1. * month: Set to {on}. * day-of-week: Set to No specific value. * year: Incremental by given frequency, starting at startOnYear. */ private static AbstractTrigger<?> createTrigger(ScheduledTask task, Recurrence.Yearly recurrence) throws FrameworkException, ApplicationExceptions { StringBuilder buf = new StringBuilder(); // startOn values for seconds, minutes and hours buf.append(task.getStartOn().second()) .append(' ') .append(task.getStartOn().minute()) .append(' ') .append(task.getStartOn().hourOfDay()); // Set day-of-month to 1 buf.append(' ').append('1'); // Compute month buf.append(' ').append(recurrence.getOn().ordinal() + 1); // No specific value for day-of-week buf.append(' ').append('?'); // Compute year buf.append(' ').append(task.getStartOn().year()).append('/').append(recurrence.getFrequency()); return createCronTrigger(buf.toString()); }
/** The following logic is used to build the cron expression. * seconds: Set to startOnSecond. * minutes: Set to startOnMinute. * hours: Set to startOnHour. * day-of-month: Set to {day} if passed, else set to No specific value. * month: Set to a comma-separated list of months. * day-of-week: Set to No specific value if {day} is passed. Else If weekFrequency equals LAST, set to {weekDay}#L. Else set to {weekDay}#{weekFrequency}. */ private static AbstractTrigger<?> createTrigger(ScheduledTask task, Recurrence.Monthly recurrence) throws FrameworkException, ApplicationExceptions { StringBuilder buf = new StringBuilder(); // startOn values for seconds, minutes and hours buf.append(task.getStartOn().second()) .append(' ') .append(task.getStartOn().minute()) .append(' ') .append(task.getStartOn().hourOfDay()); // Compute day-of-month buf.append(' '); if (recurrence.getDay() != null && recurrence.getDay() > 0) buf.append(recurrence.getDay()); else buf.append('?'); // Compute month buf.append(' '); for (int i = 0; i < recurrence.getMonths().length; i++) { if (i > 0) buf.append(','); buf.append(recurrence.getMonths()[i].ordinal() + 1); } // Compute day-of-week buf.append(' '); if (recurrence.getDay() != null && recurrence.getDay() > 0) buf.append('?'); else { buf.append(recurrence.getWeekDay().ordinal() + 1); if (recurrence.getWeekFrequency() == Recurrence.WeekFrequency.LAST) buf.append('L'); else if (recurrence.getWeekFrequency() != Recurrence.WeekFrequency.EVERY) buf.append('#').append(recurrence.getWeekFrequency().ordinal() + 1); } return createCronTrigger(buf.toString()); }
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); } }
public void done(StatusEnum status) { if (status != StatusEnum.UNKNOWN) { objectParameters.put(KEY_JOB_STATUS, status.name()); } Date endDate = new Date(); jobDetail.setEndTime(endDate); if (log.isDebugEnabled()) { log.debug("Job complete: " + jobId.toString()); } //stop the firing if (trigger instanceof AbstractTrigger) { AbstractTrigger abstractTrigger = (AbstractTrigger) trigger; abstractTrigger.setEndTime(endDate); abstractTrigger.setNextFireTime(null); } else { if (log.isErrorEnabled()) { log.error("Unexpected trigger type, not AbstractTrigger."); } } try { scheduler.unscheduleJob(jobId); } catch (WinderException e) { log.error("Failure unscheduling job: " + jobId.toString(), e); } }
/** Uses the cron expression of the Custom Recurrence. */ private static AbstractTrigger<?> createTrigger(ScheduledTask task, Recurrence.Custom recurrence) throws FrameworkException, ApplicationExceptions { return createCronTrigger(recurrence.getCustomPattern()); }