Search in sources :

Example 1 with JobBuilder

use of org.quartz.JobBuilder in project xwiki-platform by xwiki.

the class SchedulerPlugin method scheduleJob.

public boolean scheduleJob(BaseObject object, XWikiContext context) throws SchedulerPluginException {
    boolean scheduled = true;
    try {
        // compute the job unique Id
        String xjob = getObjectUniqueId(object, context);
        // Load the job class.
        // Note: Remember to always use the current thread's class loader and not the container's
        // (Class.forName(...)) since otherwise we will not be able to load classes installed with EM.
        ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
        String jobClassName = object.getStringValue("jobClass");
        Class<Job> jobClass = (Class<Job>) Class.forName(jobClassName, true, currentThreadClassLoader);
        // Build the new job.
        JobBuilder jobBuilder = JobBuilder.newJob(jobClass);
        jobBuilder.withIdentity(xjob);
        jobBuilder.storeDurably();
        JobDataMap data = new JobDataMap();
        // Let's prepare an execution context...
        XWikiContext stubContext = prepareJobStubContext(object, context);
        data.put("context", stubContext);
        data.put("xcontext", stubContext);
        data.put("xwiki", new com.xpn.xwiki.api.XWiki(context.getWiki(), stubContext));
        data.put("xjob", object);
        data.put("services", Utils.getComponent(ScriptServiceManager.class));
        jobBuilder.setJobData(data);
        getScheduler().addJob(jobBuilder.build(), true);
        TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
        triggerBuilder.withIdentity(xjob);
        triggerBuilder.forJob(xjob);
        triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(object.getStringValue("cron")));
        Trigger trigger = triggerBuilder.build();
        JobState status = getJobStatus(object, context);
        switch(status.getQuartzState()) {
            case PAUSED:
                // a paused job must be resumed, not scheduled
                break;
            case NORMAL:
                if (getTrigger(object, context).compareTo(trigger) != 0) {
                    LOGGER.debug("Reschedule Job: [{}]", object.getStringValue("jobName"));
                }
                getScheduler().rescheduleJob(trigger.getKey(), trigger);
                break;
            case NONE:
                LOGGER.debug("Schedule Job: [{}]", object.getStringValue("jobName"));
                getScheduler().scheduleJob(trigger);
                LOGGER.info("XWiki Job Status: [{}]", object.getStringValue("status"));
                if (object.getStringValue("status").equals("Paused")) {
                    getScheduler().pauseJob(new JobKey(xjob));
                    saveStatus("Paused", object, context);
                } else {
                    saveStatus("Normal", object, context);
                }
                break;
            default:
                LOGGER.debug("Schedule Job: [{}]", object.getStringValue("jobName"));
                getScheduler().scheduleJob(trigger);
                saveStatus("Normal", object, context);
                break;
        }
    } catch (SchedulerException e) {
        throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_SCHEDULE_JOB, "Error while scheduling job " + object.getStringValue("jobName"), e);
    } catch (ClassNotFoundException e) {
        throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while loading job class for job : " + object.getStringValue("jobName"), e);
    } catch (XWikiException e) {
        throw new SchedulerPluginException(SchedulerPluginException.ERROR_SCHEDULERPLUGIN_JOB_XCLASS_NOT_FOUND, "Error while saving job status for job : " + object.getStringValue("jobName"), e);
    }
    return scheduled;
}
Also used : JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) JobBuilder(org.quartz.JobBuilder) XWikiContext(com.xpn.xwiki.XWikiContext) ScriptServiceManager(org.xwiki.script.service.ScriptServiceManager) JobKey(org.quartz.JobKey) Trigger(org.quartz.Trigger) Job(org.quartz.Job) XWikiException(com.xpn.xwiki.XWikiException)

Example 2 with JobBuilder

use of org.quartz.JobBuilder in project syncope by apache.

the class SchedulingPullActions method schedule.

protected <T extends Job> void schedule(final Class<T> reference, final Map<String, Object> jobMap) throws JobExecutionException {
    @SuppressWarnings("unchecked") T jobInstance = (T) ApplicationContextProvider.getBeanFactory().createBean(reference, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false);
    String jobName = getClass().getName() + UUID_GENERATOR.generate();
    jobMap.put(JobManager.DOMAIN_KEY, AuthContextUtils.getDomain());
    ApplicationContextProvider.getBeanFactory().registerSingleton(jobName, jobInstance);
    JobBuilder jobDetailBuilder = JobBuilder.newJob(reference).withIdentity(jobName).usingJobData(new JobDataMap(jobMap));
    TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger().withIdentity(JobNamer.getTriggerName(jobName)).startNow();
    try {
        scheduler.getScheduler().scheduleJob(jobDetailBuilder.build(), triggerBuilder.build());
    } catch (SchedulerException e) {
        throw new JobExecutionException("Could not schedule, aborting", e);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException) JobExecutionException(org.quartz.JobExecutionException) JobBuilder(org.quartz.JobBuilder)

Example 3 with JobBuilder

use of org.quartz.JobBuilder in project syncope by apache.

the class JobManagerImpl method registerJob.

private void registerJob(final String jobName, final Job jobInstance, final String cronExpression, final Date startAt, final Map<String, Object> jobMap) throws SchedulerException {
    if (isRunning(new JobKey(jobName, Scheduler.DEFAULT_GROUP))) {
        LOG.debug("Job {} already running, cancel", jobName);
        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());
    }
}
Also used : JobKey(org.quartz.JobKey) JobDataMap(org.quartz.JobDataMap) JobBuilder(org.quartz.JobBuilder)

Example 4 with JobBuilder

use of org.quartz.JobBuilder in project serverless by bluenimble.

the class SchedulerPlugin method startJobs.

private void startJobs(ApiSpace space) throws PluginRegistryException {
    JsonObject schedulerFeature = Json.getObject(space.getFeatures(), feature);
    if (schedulerFeature == null || schedulerFeature.isEmpty()) {
        return;
    }
    Iterator<String> keys = schedulerFeature.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        JsonObject source = Json.getObject(schedulerFeature, key);
        if (!this.getName().equalsIgnoreCase(Json.getString(source, ApiSpace.Features.Provider))) {
            continue;
        }
        JsonObject spec = Json.getObject(source, ApiSpace.Features.Spec);
        if (spec == null) {
            continue;
        }
        String expression = Json.getString(source, Spec.Expression);
        if (Lang.isNullOrEmpty(expression)) {
            continue;
        }
        String api = Json.getString(source, Spec.Api);
        if (Lang.isNullOrEmpty(api)) {
            continue;
        }
        String service = Json.getString(source, Spec.Service);
        if (Lang.isNullOrEmpty(service)) {
            continue;
        }
        String id = space.getNamespace() + Lang.DASH + key;
        JobBuilder builder = newJob(ServiceJob.class).withIdentity(Prefix.Job + id, Prefix.Group + id);
        builder.usingJobData(_Space, space.getNamespace());
        builder.usingJobData(_Api, api);
        builder.usingJobData(_Service, service);
        JsonObject data = Json.getObject(spec, Spec.Data);
        if (!Json.isNullOrEmpty(data)) {
            Iterator<String> dataKeys = data.keys();
            while (dataKeys.hasNext()) {
                String dataKey = dataKeys.next();
                builder.usingJobData(dataKey, String.valueOf(data.get(dataKey)));
            }
        }
        JobDetail job = builder.build();
        String timeZone = Json.getString(source, Spec.TimeZone);
        CronScheduleBuilder csb = cronSchedule(Json.getString(source, Spec.Expression));
        if (!Lang.isNullOrEmpty(timeZone)) {
            csb.inTimeZone(TimeZone.getTimeZone(timeZone));
        }
        Trigger trigger = newTrigger().withIdentity(Prefix.Trigger + id, Prefix.Group + id).withSchedule(csb).forJob(job.getKey()).build();
        try {
            oScheduler.scheduleJob(trigger);
        } catch (SchedulerException e) {
            throw new PluginRegistryException(e.getMessage(), e);
        }
    }
}
Also used : JobDetail(org.quartz.JobDetail) CronScheduleBuilder(org.quartz.CronScheduleBuilder) Trigger(org.quartz.Trigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) SchedulerException(org.quartz.SchedulerException) JobBuilder(org.quartz.JobBuilder) JsonObject(com.bluenimble.platform.json.JsonObject) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 5 with JobBuilder

use of org.quartz.JobBuilder in project kernel by exoplatform.

the class JobSchedulerServiceImpl method addCronJob.

public void addCronJob(JobInfo jinfo, String exp, JobDataMap jdatamap) throws Exception {
    JobInfo jobinfo = getJobInfo(jinfo);
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).forJob(jobinfo.getJobName(), jobinfo.getGroupName()).withSchedule(CronScheduleBuilder.cronSchedule(exp)).build();
    @SuppressWarnings("unchecked") JobBuilder jb = JobBuilder.newJob(jobinfo.getJob()).withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).withDescription(jinfo.getDescription());
    JobDetail job = jdatamap == null ? jb.build() : jb.usingJobData(jdatamap).build();
    scheduleJob(job, trigger);
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) JobInfo(org.exoplatform.services.scheduler.JobInfo) JobBuilder(org.quartz.JobBuilder)

Aggregations

JobBuilder (org.quartz.JobBuilder)14 JobDetail (org.quartz.JobDetail)10 Trigger (org.quartz.Trigger)10 JobDataMap (org.quartz.JobDataMap)6 SchedulerException (org.quartz.SchedulerException)5 SimpleTrigger (org.quartz.SimpleTrigger)4 CronScheduleBuilder (org.quartz.CronScheduleBuilder)3 Job (org.quartz.Job)3 Date (java.util.Date)2 JobInfo (org.exoplatform.services.scheduler.JobInfo)2 JobKey (org.quartz.JobKey)2 Scheduler (org.quartz.Scheduler)2 SimpleScheduleBuilder (org.quartz.SimpleScheduleBuilder)2 JsonObject (com.bluenimble.platform.json.JsonObject)1 PluginRegistryException (com.bluenimble.platform.plugins.PluginRegistryException)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 ParseException (java.text.ParseException)1 CronTrigger (org.quartz.CronTrigger)1 JobExecutionException (org.quartz.JobExecutionException)1