Search in sources :

Example 11 with JobBuilder

use of org.quartz.JobBuilder in project BRFS by zhangnianli.

the class DefaultBaseSchedulers method addTask.

@Override
public boolean addTask(SumbitTaskInterface task) throws ParamsErrorException {
    // 1.检查任务的有效性
    checkTask(task);
    // 2.线程池处于暂停时,不提交任务
    if (this.pausePoolFlag) {
        LOG.warn("Thread pool is paused !!!");
        return false;
    }
    try {
        Scheduler scheduler = this.ssf.getScheduler(this.instanceName);
        // 当线程池不处于运行时,将不添加任务
        if (!isNormal()) {
            LOG.warn("Thread pool is not normal");
            return false;
        }
        // 当线程池满了也不会添加任务
        if (this.poolSize <= getSumbitTaskCount()) {
            LOG.warn("thread pool is full !!!");
            return false;
        }
        // 1.设置job的名称及执行的class
        Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(task.getClassInstanceName());
        String taskName = task.getTaskName();
        String taskGroup = task.getTaskGroupName();
        JobBuilder jobBuilder = JobBuilder.newJob(clazz).withIdentity(taskName, taskGroup);
        // 2.设置任务需要的数据
        Map<String, String> tmp = task.getTaskContent();
        if (tmp != null && !tmp.isEmpty()) {
            JobDataMap jobData = new JobDataMap();
            jobData.putAll(tmp);
            jobBuilder.usingJobData(jobData);
        }
        // 3.生成jobDetail
        JobDetail jobDetail = jobBuilder.build();
        // 4.判断触发器的类型 0 cron任务,1 simple任务
        int taskType = task.getTaskKind();
        String cycleContent = task.getCycleContent();
        Trigger trigger = null;
        if (taskType == 0) {
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cycleContent);
            trigger = TriggerBuilder.newTrigger().withIdentity(taskName, taskGroup).withSchedule(cronScheduleBuilder).build();
        } else if (taskType == 1) {
            String[] cycles = BrStringUtils.getSplit(cycleContent, ",");
            if (cycles == null || cycles.length == 0) {
                throw new NullPointerException("simple trigger cycle time is empty !!! content : " + cycleContent);
            }
            if (cycles.length != 5) {
                throw new NullPointerException("simple trigger cycle time is error !!! content : " + cycleContent);
            }
            long interval = Long.parseLong(cycles[0]);
            int repeateCount = Integer.parseInt(cycles[1]);
            repeateCount = repeateCount - 1;
            long delayTime = Long.parseLong(cycles[2]);
            boolean rightNow = Boolean.valueOf(cycles[3]);
            boolean cycleFlag = Boolean.valueOf(cycles[4]);
            SimpleScheduleBuilder builder = SimpleScheduleBuilder.simpleSchedule();
            builder.withIntervalInMilliseconds(interval);
            if (cycleFlag) {
                builder.repeatForever();
            } else if (repeateCount >= 0) {
                builder.withRepeatCount(repeateCount);
            } else {
                LOG.warn("repeated count is zero !!!!");
                return false;
            }
            TriggerBuilder trigBuilder = TriggerBuilder.newTrigger().withIdentity(taskName, taskGroup).withSchedule(builder);
            if (!rightNow && delayTime > 0) {
                long current = System.currentTimeMillis() + delayTime;
                Date date = new Date(current);
                trigBuilder.startAt(date);
            } else {
                trigBuilder.startNow();
            }
            trigger = trigBuilder.build();
        }
        if (trigger == null || jobDetail == null) {
            LOG.warn(" create task message is null");
            return false;
        }
        scheduler.scheduleJob(jobDetail, trigger);
        return true;
    } catch (NumberFormatException | ClassNotFoundException | ParseException | SchedulerException e) {
        LOG.error("add task {}", e);
    }
    return false;
}
Also used : JobDataMap(org.quartz.JobDataMap) CronScheduleBuilder(org.quartz.CronScheduleBuilder) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) JobBuilder(org.quartz.JobBuilder) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) TriggerBuilder(org.quartz.TriggerBuilder) Date(java.util.Date) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) ParseException(java.text.ParseException) Job(org.quartz.Job)

Example 12 with JobBuilder

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

the class JobSchedulerServiceImpl method addPeriodJob.

public void addPeriodJob(JobInfo jinfo, PeriodInfo pinfo, JobDataMap jdatamap) throws Exception {
    int repeat = pinfo.getRepeatCount();
    Date start = pinfo.getStartTime();
    JobInfo jobinfo = getJobInfo(jinfo);
    if (start == null)
        start = new Date();
    if (repeat <= 0)
        repeat = SimpleTrigger.REPEAT_INDEFINITELY;
    else
        repeat = repeat - 1;
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobinfo.getJobName(), jobinfo.getGroupName()).withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(repeat).withIntervalInMilliseconds(pinfo.getRepeatInterval())).startAt(start).endAt(pinfo.getEndTime()).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) Date(java.util.Date)

Example 13 with JobBuilder

use of org.quartz.JobBuilder in project Dempsy by Dempsy.

the class Container method startEvictionThread.

private void startEvictionThread() throws SchedulerException {
    // when done in parallel.
    synchronized (StdSchedulerFactory.class) {
        if (0 == evictionCycleTime || null == evictionTimeUnit) {
            LOGGER.warn("Eviction Thread cannot start with zero cycle time or null TimeUnit {} {}", evictionCycleTime, evictionTimeUnit);
            return;
        }
        if (prototype != null && prototype.isEvictionSupported()) {
            final JobBuilder jobBuilder = JobBuilder.newJob(EvictionCheckJob.class);
            final JobDetail jobDetail = jobBuilder.build();
            jobDetail.getJobDataMap().put(EVICTION_CHECK_JOB_NAME, this);
            final Trigger trigger = QuartzHelper.getSimpleTrigger(evictionTimeUnit, (int) evictionCycleTime, true);
            evictionScheduler = StdSchedulerFactory.getDefaultScheduler();
            evictionScheduler.scheduleJob(jobDetail, trigger);
            evictionScheduler.start();
        }
    }
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) JobBuilder(org.quartz.JobBuilder)

Example 14 with JobBuilder

use of org.quartz.JobBuilder in project Dempsy by Dempsy.

the class OutputQuartzHelper method getJobDetail.

/**
 * Gets the job detail.
 *
 * @param outputInvoker the output invoker
 * @return the job detail
 */
public JobDetail getJobDetail(final OutputInvoker outputInvoker) {
    final JobBuilder jobBuilder = JobBuilder.newJob(OutputJob.class);
    final JobDetail jobDetail = jobBuilder.build();
    jobDetail.getJobDataMap().put(OUTPUT_JOB_NAME, outputInvoker);
    return jobDetail;
}
Also used : JobDetail(org.quartz.JobDetail) 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