Search in sources :

Example 91 with Trigger

use of org.quartz.Trigger in project deltaspike by apache.

the class DynamicExpressionObserverJob method execute.

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    JobDataMap jobDataMap = context.getMergedJobDataMap();
    String configExpression = jobDataMap.getString(CONFIG_EXPRESSION_KEY);
    String triggerId = jobDataMap.getString(TRIGGER_ID_KEY);
    String activeCronExpression = jobDataMap.getString(ACTIVE_CRON_EXPRESSION_KEY);
    String configKey = configExpression.substring(1, configExpression.length() - 1);
    String configuredValue = ConfigResolver.getPropertyAwarePropertyValue(configKey, activeCronExpression);
    if (!activeCronExpression.equals(configuredValue)) {
        //both #put calls are needed currently
        context.getJobDetail().getJobDataMap().put(ACTIVE_CRON_EXPRESSION_KEY, configuredValue);
        context.getTrigger().getJobDataMap().put(ACTIVE_CRON_EXPRESSION_KEY, configuredValue);
        BeanProvider.injectFields(this);
        JobKey observerJobKey = context.getJobDetail().getKey();
        String observedJobName = observerJobKey.getName().substring(0, observerJobKey.getName().length() - OBSERVER_POSTFIX.length());
        JobKey observedJobKey = new JobKey(observedJobName, observerJobKey.getGroup());
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerId).forJob(observedJobName, observedJobKey.getGroup()).withSchedule(CronScheduleBuilder.cronSchedule(configuredValue)).build();
        //use rescheduleJob instead of delete + add
        //(unwrap is ok here, because this class will only get active in case of a quartz-scheduler)
        org.quartz.Scheduler quartzScheduler = scheduler.unwrap(org.quartz.Scheduler.class);
        try {
            quartzScheduler.rescheduleJob(trigger.getKey(), trigger);
        } catch (SchedulerException e) {
            LOG.warning("failed to updated cron-expression for " + observedJobKey);
        }
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) JobKey(org.quartz.JobKey) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException)

Example 92 with Trigger

use of org.quartz.Trigger in project deltaspike by apache.

the class AbstractQuartzScheduler method createExpressionObserverJob.

private void createExpressionObserverJob(JobKey jobKey, UUID triggerKey, String configExpression, String cronExpression) throws SchedulerException {
    if (!ClassDeactivationUtils.isActivated(DynamicExpressionObserverJob.class)) {
        return;
    }
    JobKey observerJobKey = new JobKey(jobKey.getName() + DynamicExpressionObserverJob.OBSERVER_POSTFIX, jobKey.getGroup());
    JobDetail jobDetail = JobBuilder.newJob(DynamicExpressionObserverJob.class).usingJobData(DynamicExpressionObserverJob.CONFIG_EXPRESSION_KEY, configExpression).usingJobData(DynamicExpressionObserverJob.TRIGGER_ID_KEY, triggerKey.toString()).usingJobData(DynamicExpressionObserverJob.ACTIVE_CRON_EXPRESSION_KEY, cronExpression).withDescription("Config observer for: " + jobKey).withIdentity(observerJobKey).build();
    Trigger trigger = TriggerBuilder.newTrigger().forJob(observerJobKey).withSchedule(CronScheduleBuilder.cronSchedule(SchedulerBaseConfig.JobCustomization.DYNAMIC_EXPRESSION_OBSERVER_INTERVAL)).build();
    this.scheduler.scheduleJob(jobDetail, trigger);
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger)

Example 93 with Trigger

use of org.quartz.Trigger in project deltaspike by apache.

the class AbstractQuartzScheduler method createTrigger.

private Trigger createTrigger(Scheduled scheduled, JobKey jobKey, String cronExpression) throws SchedulerException {
    UUID triggerKey = UUID.randomUUID();
    if (!scheduled.cronExpression().endsWith(cronExpression)) {
        createExpressionObserverJob(jobKey, triggerKey, scheduled.cronExpression(), cronExpression);
    }
    Trigger trigger = TriggerBuilder.newTrigger().forJob(jobKey).withIdentity(triggerKey.toString()).withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
    return trigger;
}
Also used : Trigger(org.quartz.Trigger) UUID(java.util.UUID)

Example 94 with Trigger

use of org.quartz.Trigger in project oxCore by GluuFederation.

the class QuartzSchedulerManager method schedule.

public void schedule(@Observes TimerEvent timerEvent) {
    checkInitialized();
    JobDataMap dataMap = new JobDataMap();
    dataMap.put(TimerJob.KEY_TIMER_EVENT, timerEvent);
    String uuid = UUID.randomUUID().toString();
    JobDetail timerJob = JobBuilder.newJob(TimerJob.class).withIdentity(TimerJob.class.getSimpleName() + "_" + uuid, TimerJob.TIMER_JOB_GROUP).usingJobData(dataMap).build();
    TimerSchedule timerSchedule = timerEvent.getSchedule();
    Date triggerStartTime = new Date(System.currentTimeMillis() + timerSchedule.getDelay() * 1000L);
    Trigger timerTrigger = TriggerBuilder.newTrigger().withIdentity(uuid, TimerJob.TIMER_JOB_GROUP).startAt(triggerStartTime).withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(timerSchedule.getInterval())).build();
    try {
        scheduler.scheduleJob(timerJob, timerTrigger);
    } catch (SchedulerException ex) {
        throw new IllegalStateException("Failed to schedule Timer Event", ex);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) TimerSchedule(org.xdi.service.timer.schedule.TimerSchedule) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException) Date(java.util.Date)

Example 95 with Trigger

use of org.quartz.Trigger in project sling by apache.

the class WebConsolePrinter method printConfiguration.

/**
     * Print out the configuration
     * @see org.apache.felix.webconsole.ConfigurationPrinter#printConfiguration(java.io.PrintWriter)
     */
public void printConfiguration(PrintWriter pw) {
    pw.println(HEADLINE);
    pw.println();
    final Map<String, SchedulerProxy> proxies = this.scheduler.getSchedulers();
    if (!proxies.isEmpty()) {
        pw.println("Status : active");
        pw.println("Discovery : " + (QuartzJobExecutor.DISCOVERY_AVAILABLE.get() ? "available" : "not available"));
        for (final Map.Entry<String, SchedulerProxy> entry : proxies.entrySet()) {
            final Scheduler s = entry.getValue().getScheduler();
            try {
                pw.print("Name      : ");
                pw.println(s.getSchedulerName());
                pw.print("ThreadPool: ");
                pw.println(entry.getKey());
                pw.print("Id        : ");
                pw.println(s.getSchedulerInstanceId());
                pw.println();
                final List<JobInfo> activeJobs = new ArrayList<>();
                final List<JobInfo> disabledJobs = new ArrayList<>();
                for (final String group : s.getJobGroupNames()) {
                    final Set<JobKey> keys = s.getJobKeys(GroupMatcher.jobGroupEquals(group));
                    for (final JobKey key : keys) {
                        final JobDetail detail = s.getJobDetail(key);
                        final QuartzJobExecutor.JobDesc desc = new QuartzJobExecutor.JobDesc(detail.getJobDataMap());
                        // only print jobs started through the sling scheduler
                        if (desc.isKnownJob()) {
                            final JobInfo info = new JobInfo();
                            info.name = desc.name;
                            info.className = desc.job.getClass().getName();
                            info.concurrent = !detail.isConcurrentExectionDisallowed();
                            // check run on information
                            if (desc.runOn != null) {
                                if (desc.isRunOnLeader()) {
                                    info.runOn = "LEADER";
                                } else if (desc.isRunOnSingle()) {
                                    info.runOn = "SINGLE";
                                } else {
                                    info.runOn = Arrays.toString(desc.runOn);
                                }
                                if (desc.isRunOnLeader() || desc.isRunOnSingle()) {
                                    if (QuartzJobExecutor.DISCOVERY_AVAILABLE.get()) {
                                        if (QuartzJobExecutor.DISCOVERY_INFO_AVAILABLE.get()) {
                                            if (desc.isRunOnLeader() || QuartzJobExecutor.FORCE_LEADER.get()) {
                                                if (!QuartzJobExecutor.IS_LEADER.get()) {
                                                    info.reason = "not leader";
                                                }
                                            } else {
                                                final String id = desc.shouldRunAsSingleOn();
                                                if (id != null) {
                                                    info.reason = "single distributed elsewhere " + id;
                                                }
                                            }
                                        } else {
                                            info.reason = "no discovery info";
                                        }
                                    } else {
                                        info.reason = "no discovery";
                                    }
                                } else {
                                    // sling IDs
                                    final String myId = QuartzJobExecutor.SLING_ID;
                                    if (myId == null) {
                                        info.reason = "no Sling settings";
                                    } else {
                                        boolean schedule = false;
                                        for (final String id : desc.runOn) {
                                            if (myId.equals(id)) {
                                                schedule = true;
                                                break;
                                            }
                                        }
                                        if (!schedule) {
                                            info.reason = "Sling ID";
                                        }
                                    }
                                }
                            }
                            info.bundleId = (Long) detail.getJobDataMap().get(QuartzScheduler.DATA_MAP_BUNDLE_ID);
                            info.serviceId = (Long) detail.getJobDataMap().get(QuartzScheduler.DATA_MAP_SERVICE_ID);
                            int index = 0;
                            final List<? extends Trigger> triggers = s.getTriggersOfJob(key);
                            info.triggers = new String[triggers.size()];
                            for (final Trigger trigger : triggers) {
                                info.triggers[index] = trigger.toString();
                                index++;
                            }
                            if (info.reason != null) {
                                disabledJobs.add(info);
                            } else {
                                activeJobs.add(info);
                            }
                        }
                    }
                }
                if (!activeJobs.isEmpty()) {
                    pw.println();
                    pw.println("Active Jobs");
                    pw.println("-----------");
                    for (final JobInfo info : activeJobs) {
                        print(pw, info);
                    }
                }
                if (!disabledJobs.isEmpty()) {
                    pw.println();
                    pw.println("Inactive Jobs");
                    pw.println("-------------");
                    for (final JobInfo info : disabledJobs) {
                        print(pw, info);
                    }
                }
            } catch (final SchedulerException se) {
                pw.print("Unable to print complete configuration: ");
                pw.println(se.getMessage());
            }
            pw.println();
        }
    } else {
        pw.println("Status : not active");
    }
    pw.println();
}
Also used : SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) ArrayList(java.util.ArrayList) JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) Map(java.util.Map)

Aggregations

Trigger (org.quartz.Trigger)104 JobDetail (org.quartz.JobDetail)51 SchedulerException (org.quartz.SchedulerException)40 CronTrigger (org.quartz.CronTrigger)29 Test (org.junit.Test)23 Scheduler (org.quartz.Scheduler)22 JobDataMap (org.quartz.JobDataMap)21 SimpleTrigger (org.quartz.SimpleTrigger)20 TriggerKey (org.quartz.TriggerKey)20 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)19 JobKey (org.quartz.JobKey)16 ArrayList (java.util.ArrayList)13 Date (java.util.Date)12 List (java.util.List)5 Command (org.openhab.core.types.Command)5 InetSocketAddress (java.net.InetSocketAddress)4 SocketChannel (java.nio.channels.SocketChannel)4 ParseException (java.text.ParseException)4 FormProcessor (org.akaza.openclinica.control.form.FormProcessor)4 ProtoTrigger (co.cask.cdap.proto.ProtoTrigger)3