@SuppressWarnings("unchecked") public void add(Class<?> klass, Scheduled scheduled) throws SchedulerException { String name = klass.getName(); if (!Strings.isBlank(scheduled.cron())) { try { log.debugf("job define name=%s cron=%s", name, scheduled.cron()); cron(scheduled.cron(), klass); return; } catch (SchedulerException e) { throw new RuntimeException(e); } } if (scheduled.fixedRate() > 0) { log.debugf( "job define name=%s fixedRate=%s count=%s initialDelay=%s", name, scheduled.fixedRate(), scheduled.count(), scheduled.initialDelay()); SimpleScheduleBuilder schedule = SimpleScheduleBuilder.simpleSchedule(); if (scheduled.fixedRate() > 0) schedule.withIntervalInSeconds(scheduled.fixedRate()); if (scheduled.count() > 0) { schedule.withRepeatCount(scheduled.count()); } else { schedule.repeatForever(); } TriggerBuilder<SimpleTrigger> trigger = TriggerBuilder.newTrigger().withIdentity(name).withSchedule(schedule); if (scheduled.initialDelay() > 0) trigger.startAt(new Date(System.currentTimeMillis() + scheduled.initialDelay() * 1000)); JobDetail job = JobBuilder.newJob((Class<? extends Job>) klass).withIdentity(name).build(); scheduler.scheduleJob(job, trigger.build()); } }
@Override protected Trigger getTrigger() { final TriggerBuilder<? extends Trigger> triggerBuilder = trigger.getTriggerBuilder(); triggerBuilder.withIdentity((TriggerKey) null); if (getDelay() != null) { triggerBuilder.startAt(new DateTime().plus(getDelay()).toDate()); } return triggerBuilder.build(); }
private void registerJob( final String jobName, final Job jobInstance, final String cronExpression, final Date startAt, final Map<String, Object> jobMap) throws SchedulerException, ParseException { synchronized (scheduler.getScheduler()) { boolean jobAlreadyRunning = false; for (JobExecutionContext jobCtx : scheduler.getScheduler().getCurrentlyExecutingJobs()) { if (jobName.equals(jobCtx.getJobDetail().getKey().getName()) && Scheduler.DEFAULT_GROUP.equals(jobCtx.getJobDetail().getKey().getGroup())) { jobAlreadyRunning = true; LOG.debug("Job {} already running, cancel", jobCtx.getJobDetail().getKey()); } } if (jobAlreadyRunning) { return; } } // 0. unregister job unregisterJob(jobName); // 1. Job bean ApplicationContextProvider.getBeanFactory().registerSingleton(jobName, jobInstance); // 2. JobDetail bean JobBuilder jobDetailBuilder = JobBuilder.newJob(jobInstance.getClass()) .withIdentity(jobName) .usingJobData(new JobDataMap(jobMap)); // 3. Trigger if (cronExpression == null && startAt == null) { // Jobs added with no trigger must be durable scheduler.getScheduler().addJob(jobDetailBuilder.storeDurably().build(), true); } else { TriggerBuilder<?> triggerBuilder; if (cronExpression == null) { triggerBuilder = TriggerBuilder.newTrigger() .withIdentity(JobNamer.getTriggerName(jobName)) .startAt(startAt); } else { triggerBuilder = TriggerBuilder.newTrigger() .withIdentity(JobNamer.getTriggerName(jobName)) .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)); if (startAt == null) { triggerBuilder = triggerBuilder.startNow(); } else { triggerBuilder = triggerBuilder.startAt(startAt); } } scheduler.getScheduler().scheduleJob(jobDetailBuilder.build(), triggerBuilder.build()); } }
protected Trigger getQuartzTrigger( com.liferay.portal.kernel.scheduler.Trigger trigger, StorageType storageType) throws SchedulerException { if (trigger == null) { return null; } Date endDate = trigger.getEndDate(); String jobName = fixMaxLength(trigger.getJobName(), _jobNameMaxLength, storageType); String groupName = fixMaxLength(trigger.getGroupName(), _groupNameMaxLength, storageType); Date startDate = trigger.getStartDate(); if (startDate == null) { startDate = new Date(System.currentTimeMillis()); } TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger(); triggerBuilder.endAt(endDate); triggerBuilder.forJob(jobName, groupName); triggerBuilder.startAt(startDate); triggerBuilder.withIdentity(jobName, groupName); TriggerType triggerType = trigger.getTriggerType(); if (triggerType == TriggerType.CRON) { triggerBuilder.withSchedule( CronScheduleBuilder.cronSchedule((String) trigger.getTriggerContent())); return triggerBuilder.build(); } ObjectValuePair<Integer, TimeUnit> objectValuePair = (ObjectValuePair<Integer, TimeUnit>) trigger.getTriggerContent(); int interval = objectValuePair.getKey(); if (interval < 0) { if (_log.isWarnEnabled()) { _log.warn("Not scheduling " + trigger.getJobName() + " because interval is less than 0"); } return null; } else if (interval == 0) { return triggerBuilder.build(); } TimeUnit timeUnit = objectValuePair.getValue(); if (timeUnit == TimeUnit.MILLISECOND) { SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule(); simpleScheduleBuilder.withIntervalInMilliseconds(interval); simpleScheduleBuilder.withRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); triggerBuilder.withSchedule(simpleScheduleBuilder); } else { CalendarIntervalScheduleBuilder calendarIntervalScheduleBuilder = CalendarIntervalScheduleBuilder.calendarIntervalSchedule(); calendarIntervalScheduleBuilder.withInterval(interval, IntervalUnit.valueOf(timeUnit.name())); triggerBuilder.withSchedule(calendarIntervalScheduleBuilder); } return triggerBuilder.build(); }