Search in sources :

Example 41 with JobDetail

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

the class QuartzScheduler method removeJob.

/**
     * @see org.apache.sling.commons.scheduler.Scheduler#removeJob(java.lang.String)
     */
public void removeJob(final Long bundleId, final String jobName) throws NoSuchElementException {
    // as this method might be called from unbind and during
    // unbind a deactivate could happen, we check the scheduler first
    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 JobKey key = JobKey.jobKey(jobName);
                final JobDetail jobdetail = proxy.getScheduler().getJobDetail(key);
                if (jobdetail != null) {
                    proxy.getScheduler().deleteJob(key);
                    this.logger.debug("Unscheduling job with name {}", jobName);
                    return;
                }
            } catch (final SchedulerException ignored) {
            // ignore
            }
        }
    }
    if (this.active) {
        throw new NoSuchElementException("No job found with name " + jobName);
    }
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException) NoSuchElementException(java.util.NoSuchElementException)

Example 42 with JobDetail

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

the class QuartzJobExecutorTest method testJobExecutedWithTwoRunOnParams.

@Test
public void testJobExecutedWithTwoRunOnParams() throws SchedulerException {
    Job job = new SimpleJob();
    String jobName = "testName";
    Map<String, Serializable> jobConfig = new HashMap<String, Serializable>();
    //Adding a job just to receive a JobDetail object which is needed for testing
    quartzScheduler.addJob(1L, 1L, jobName, job, jobConfig, "0 * * * * ?", true);
    JobDetail jobDetail = quartzScheduler.getSchedulers().get("testName").getScheduler().getJobDetail(JobKey.jobKey(jobName));
    when(executionContext.getJobDetail()).thenReturn(jobDetail);
    //Job with this config should not be executed
    jobDetail.getJobDataMap().put(QuartzScheduler.DATA_MAP_RUN_ON, new String[] { VALUE_RUN_ON_LEADER, VALUE_RUN_ON_SINGLE });
    //In this case, when SLING_ID is equal to one of values above
    //Job should be executed
    QuartzJobExecutor.SLING_ID = VALUE_RUN_ON_SINGLE;
    isRunnablePseudoJobCompleted = false;
    jobExecutor.execute(executionContext);
    assertTrue(isRunnablePseudoJobCompleted);
}
Also used : Serializable(java.io.Serializable) JobDetail(org.quartz.JobDetail) HashMap(java.util.HashMap) Job(org.apache.sling.commons.scheduler.Job) Test(org.junit.Test)

Example 43 with JobDetail

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

the class AbstractQuartzScheduler method isExecutingJob.

@Override
public boolean isExecutingJob(Class<? extends T> jobClass) {
    try {
        JobKey jobKey = createJobKey(jobClass);
        JobDetail jobDetail = this.scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            return false;
        }
        for (JobExecutionContext jobExecutionContext : this.scheduler.getCurrentlyExecutingJobs()) {
            if (jobKey.equals(jobExecutionContext.getJobDetail().getKey())) {
                return true;
            }
        }
        return false;
    } catch (SchedulerException e) {
        throw ExceptionUtils.throwAsRuntimeException(e);
    }
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException) JobExecutionContext(org.quartz.JobExecutionContext)

Example 44 with JobDetail

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

the class AbstractQuartzScheduler method registerNewJob.

@Override
public void registerNewJob(Class<? extends T> jobClass) {
    JobKey jobKey = createJobKey(jobClass);
    try {
        Scheduled scheduled = jobClass.getAnnotation(Scheduled.class);
        String description = scheduled.description();
        if ("".equals(scheduled.description())) {
            description = jobClass.getName();
        }
        JobDetail jobDetail = this.scheduler.getJobDetail(jobKey);
        Trigger trigger;
        if (jobDetail == null) {
            Class<? extends Job> jobClassToAdd = createFinalJobClass(jobClass);
            jobDetail = JobBuilder.newJob(jobClassToAdd).withDescription(description).withIdentity(jobKey).build();
            scheduleNewJob(scheduled, jobKey, jobDetail);
        } else if (scheduled.overrideOnStartup()) {
            List<? extends Trigger> existingTriggers = this.scheduler.getTriggersOfJob(jobKey);
            if (existingTriggers == null || existingTriggers.isEmpty()) {
                scheduleNewJob(scheduled, jobKey, jobDetail);
                return;
            }
            if (existingTriggers.size() > 1) {
                throw new IllegalStateException("multiple triggers found for " + jobKey + " ('" + jobDetail + "')" + ", but aren't supported by @" + Scheduled.class.getName() + "#overrideOnStartup");
            }
            trigger = existingTriggers.iterator().next();
            if (scheduled.cronExpression().startsWith("{") && scheduled.cronExpression().endsWith("}")) {
                this.scheduler.unscheduleJobs(Arrays.asList(trigger.getKey()));
                scheduleNewJob(scheduled, jobKey, jobDetail);
            } else {
                trigger = TriggerBuilder.newTrigger().withIdentity(trigger.getKey()).withSchedule(CronScheduleBuilder.cronSchedule(scheduled.cronExpression())).build();
                this.scheduler.rescheduleJob(trigger.getKey(), trigger);
            }
        } else {
            Logger.getLogger(AbstractQuartzScheduler.class.getName()).info(jobKey + " exists already and will be ignored.");
        }
    } catch (SchedulerException e) {
        throw ExceptionUtils.throwAsRuntimeException(e);
    }
}
Also used : Scheduled(org.apache.deltaspike.scheduler.api.Scheduled) JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException) List(java.util.List)

Example 45 with JobDetail

use of org.quartz.JobDetail in project engine by craftercms.

the class SchedulingUtils method createJobContext.

public static JobContext createJobContext(SiteContext siteContext, String scriptUrl, String cronExpression, ServletContext servletContext) {
    String jobName = siteContext.getSiteName() + ":" + scriptUrl;
    JobDetail detail = SchedulingUtils.createScriptJob(siteContext, jobName, scriptUrl, servletContext);
    Trigger trigger = SchedulingUtils.createCronTrigger("trigger for " + jobName, cronExpression);
    String description = "Job{url='" + scriptUrl + "', cron='" + cronExpression + "'}";
    return new JobContext(detail, trigger, description);
}
Also used : JobDetail(org.quartz.JobDetail) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Trigger(org.quartz.Trigger) JobContext(org.craftercms.engine.util.quartz.JobContext)

Aggregations

JobDetail (org.quartz.JobDetail)131 SchedulerException (org.quartz.SchedulerException)59 Trigger (org.quartz.Trigger)51 Scheduler (org.quartz.Scheduler)34 Test (org.junit.Test)30 CronTrigger (org.quartz.CronTrigger)26 JobKey (org.quartz.JobKey)22 SimpleTrigger (org.quartz.SimpleTrigger)22 JobDataMap (org.quartz.JobDataMap)20 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 TriggerKey (org.quartz.TriggerKey)10 HashMap (java.util.HashMap)8 Date (java.util.Date)7 ArrayList (java.util.ArrayList)5 Command (org.openhab.core.types.Command)5 IOException (java.io.IOException)4 Serializable (java.io.Serializable)4 InetSocketAddress (java.net.InetSocketAddress)4 SocketChannel (java.nio.channels.SocketChannel)4