Search in sources :

Example 1 with Scheduled

use of org.apache.deltaspike.scheduler.api.Scheduled 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 2 with Scheduled

use of org.apache.deltaspike.scheduler.api.Scheduled in project deltaspike by apache.

the class AbstractQuartzScheduler method createJobKey.

private JobKey createJobKey(Class<?> jobClass) {
    Scheduled scheduled = jobClass.getAnnotation(Scheduled.class);
    if (scheduled == null) {
        throw new IllegalStateException("@" + Scheduled.class.getName() + " is missing on " + jobClass.getName());
    }
    String groupName = scheduled.group().getSimpleName();
    String jobName = getJobName(jobClass);
    if (!Scheduled.class.getSimpleName().equals(groupName)) {
        return new JobKey(jobName, groupName);
    }
    return new JobKey(jobName);
}
Also used : Scheduled(org.apache.deltaspike.scheduler.api.Scheduled) JobKey(org.quartz.JobKey)

Example 3 with Scheduled

use of org.apache.deltaspike.scheduler.api.Scheduled in project deltaspike by apache.

the class SchedulerExtension method findScheduledJobs.

public <X> void findScheduledJobs(@Observes ProcessAnnotatedType<X> pat, BeanManager beanManager) {
    if (!this.isActivated) {
        return;
    }
    Class<X> beanClass = pat.getAnnotatedType().getJavaClass();
    //see SchedulerProducer
    if (Scheduler.class.isAssignableFrom(beanClass) || isInternalUnmanagedClass(beanClass)) {
        pat.veto();
        return;
    }
    if (!jobClass.isAssignableFrom(beanClass) && !Runnable.class.isAssignableFrom(beanClass)) {
        return;
    }
    Scheduled scheduled = pat.getAnnotatedType().getAnnotation(Scheduled.class);
    if (scheduled != null && scheduled.onStartup()) {
        this.foundManagedJobClasses.add(beanClass);
    }
}
Also used : Scheduled(org.apache.deltaspike.scheduler.api.Scheduled) Scheduler(org.apache.deltaspike.scheduler.spi.Scheduler)

Aggregations

Scheduled (org.apache.deltaspike.scheduler.api.Scheduled)3 JobKey (org.quartz.JobKey)2 List (java.util.List)1 Scheduler (org.apache.deltaspike.scheduler.spi.Scheduler)1 JobDetail (org.quartz.JobDetail)1 SchedulerException (org.quartz.SchedulerException)1 Trigger (org.quartz.Trigger)1