Search in sources :

Example 61 with JobKey

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

the class QuartzScheduler method bundleChanged.

/**
     * @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent)
     */
@Override
public void bundleChanged(final BundleEvent event) {
    if (event.getType() == BundleEvent.STOPPED) {
        final Long bundleId = event.getBundle().getBundleId();
        final Map<String, SchedulerProxy> proxies;
        synchronized (this.schedulers) {
            if (this.active) {
                proxies = new HashMap<>(this.schedulers);
            } else {
                proxies = Collections.emptyMap();
            }
        }
        for (final SchedulerProxy proxy : proxies.values()) {
            synchronized (proxy) {
                try {
                    final List<String> groups = proxy.getScheduler().getJobGroupNames();
                    for (final String group : groups) {
                        final Set<JobKey> keys = proxy.getScheduler().getJobKeys(GroupMatcher.jobGroupEquals(group));
                        for (final JobKey key : keys) {
                            final JobDetail detail = proxy.getScheduler().getJobDetail(key);
                            if (detail != null) {
                                final String jobName = (String) detail.getJobDataMap().get(QuartzScheduler.DATA_MAP_NAME);
                                final Object job = detail.getJobDataMap().get(QuartzScheduler.DATA_MAP_OBJECT);
                                if (jobName != null && job != null) {
                                    final Long jobBundleId = (Long) detail.getJobDataMap().get(QuartzScheduler.DATA_MAP_BUNDLE_ID);
                                    if (jobBundleId != null && jobBundleId.equals(bundleId)) {
                                        proxy.getScheduler().deleteJob(key);
                                        this.logger.debug("Unscheduling job with name {}", jobName);
                                    }
                                }
                            }
                        }
                    }
                } catch (final SchedulerException ignore) {
                // we ignore this as there is nothing to do
                }
            }
        }
    }
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException)

Example 62 with JobKey

use of org.quartz.JobKey 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)

Example 63 with JobKey

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

the class WhiteboardHandlerTest method testAddingService.

@Test
public void testAddingService() throws SchedulerException {
    Thread service = new Thread();
    String schedulerName = "testScheduler";
    Long period = 1L;
    Integer times = 2;
    Dictionary<String, Object> serviceProps = new Hashtable<>();
    serviceProps.put(Scheduler.PROPERTY_SCHEDULER_RUN_ON, Scheduler.VALUE_RUN_ON_LEADER);
    serviceProps.put(Scheduler.PROPERTY_SCHEDULER_CONCURRENT, Boolean.FALSE);
    serviceProps.put(Scheduler.PROPERTY_SCHEDULER_IMMEDIATE, Boolean.FALSE);
    serviceProps.put(Scheduler.PROPERTY_SCHEDULER_NAME, schedulerName);
    serviceProps.put(Scheduler.PROPERTY_SCHEDULER_PERIOD, period);
    serviceProps.put(Scheduler.PROPERTY_SCHEDULER_TIMES, times);
    serviceProps.put(Constants.SERVICE_PID, "1");
    serviceProps.put(Constants.SERVICE_ID, 1L);
    final ServiceRegistration<?> reg = context.registerService(Runnable.class.getName(), service, serviceProps);
    final ServiceReference<?> reference = reg.getReference();
    handler.register(reference, service);
    JobKey jobKey = JobKey.jobKey(schedulerName + "." + reference.getProperty(Constants.SERVICE_ID));
    assertNotNull(quartzScheduler.getSchedulers().get("testName").getScheduler().getJobDetail(jobKey));
}
Also used : JobKey(org.quartz.JobKey) Hashtable(java.util.Hashtable) Test(org.junit.Test)

Example 64 with JobKey

use of org.quartz.JobKey in project cdap by caskdata.

the class DatasetBasedTimeScheduleStoreTest method testPausedTriggersAcrossRestarts.

@Test
public void testPausedTriggersAcrossRestarts() throws SchedulerException, UnsupportedTypeException {
    JobKey jobKey = scheduleJobWithTrigger(true);
    List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
    // pause triggers for the job
    for (Trigger trigger : triggers) {
        scheduler.pauseTrigger(trigger.getKey());
    }
    //Shutdown scheduler.
    schedulerTearDown();
    //restart scheduler.
    schedulerSetup(true);
    verifyJobAndTriggers(jobKey, 1, Trigger.TriggerState.PAUSED);
    // remove job and check if the associated trigger gets removed too
    Assert.assertTrue("Failed to delete the job", scheduler.deleteJob(jobKey));
    Assert.assertFalse("Trigger for the deleted job still exists", scheduler.checkExists(triggers.get(0).getKey()));
    // check for trigger to not exist in the datastore too from which scheduler will get initialized across restart
    //Shutdown scheduler.
    schedulerTearDown();
    //restart scheduler.
    schedulerSetup(true);
    Assert.assertFalse("Trigger for the deleted job still exists", scheduler.checkExists(triggers.get(0).getKey()));
    schedulerTearDown();
}
Also used : JobKey(org.quartz.JobKey) Trigger(org.quartz.Trigger) Test(org.junit.Test)

Example 65 with JobKey

use of org.quartz.JobKey in project cdap by caskdata.

the class TimeScheduler method initNewPausedTriggersGroup.

/**
   * Creates a paused group TimeScheduler#PAUSED_NEW_TRIGGERS_GROUP by adding a dummy job to it if it does not exists
   * already. This is needed so that we can add new triggers to this paused group and they will be paused too.
   *
   * @throws org.quartz.SchedulerException
   */
private void initNewPausedTriggersGroup() throws org.quartz.SchedulerException {
    // which will create the TimeScheduler#PAUSED_NEW_TRIGGERS_GROUP
    if (!scheduler.checkExists(new JobKey(EmptyJob.class.getSimpleName(), PAUSED_NEW_TRIGGERS_GROUP))) {
        JobDetail job = JobBuilder.newJob(EmptyJob.class).withIdentity(EmptyJob.class.getSimpleName(), PAUSED_NEW_TRIGGERS_GROUP).storeDurably(true).build();
        scheduler.addJob(job, true);
    }
    // call pause on this group this ensures that all the new triggers added to this group will also be paused
    scheduler.pauseTriggers(GroupMatcher.triggerGroupEquals(PAUSED_NEW_TRIGGERS_GROUP));
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail)

Aggregations

JobKey (org.quartz.JobKey)65 SchedulerException (org.quartz.SchedulerException)33 JobDetail (org.quartz.JobDetail)22 Trigger (org.quartz.Trigger)19 Scheduler (org.quartz.Scheduler)17 TriggerKey (org.quartz.TriggerKey)15 Test (org.junit.Test)13 List (java.util.List)6 CronTrigger (org.quartz.CronTrigger)6 JobDataMap (org.quartz.JobDataMap)6 ArrayList (java.util.ArrayList)5 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)5 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 Hashtable (java.util.Hashtable)2 Scheduled (org.apache.deltaspike.scheduler.api.Scheduled)2 CronScheduleBuilder (org.quartz.CronScheduleBuilder)2