Search in sources :

Example 6 with SimpleScheduleBuilder

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

the class QuartzBaseSchedulers method addTask.

@Override
public boolean addTask(T task) throws Exception {
    if (!checkTask(task)) {
        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 = StringUtils.getSplit(cycleContent, ",");
        if (cycles == null || cycles.length == 0) {
            throw new NullPointerException("simple trigger cycle time is empty !!! content : " + cycleContent);
        }
        if (cycles.length != 2) {
            throw new NullPointerException("simple trigger cycle time is error !!! content : " + cycleContent);
        }
        long interval = Long.valueOf(cycles[0]);
        int repeateCount = Integer.valueOf(cycles[1]);
        SimpleScheduleBuilder sSched = SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(interval).withRepeatCount(repeateCount);
        trigger = TriggerBuilder.newTrigger().withIdentity(taskName, taskGroup).startNow().withSchedule(sSched).build();
    }
    if (trigger == null || jobDetail == null) {
        return false;
    }
    Scheduler scheduler = this.ssf.getScheduler(this.instanceName);
    scheduler.scheduleJob(jobDetail, trigger);
    return true;
}
Also used : JobDataMap(org.quartz.JobDataMap) CronScheduleBuilder(org.quartz.CronScheduleBuilder) Scheduler(org.quartz.Scheduler) JobBuilder(org.quartz.JobBuilder) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) Job(org.quartz.Job)

Example 7 with SimpleScheduleBuilder

use of org.quartz.SimpleScheduleBuilder in project motech by motech.

the class MotechSchedulerServiceImpl method scheduleRepeatingJob.

private void scheduleRepeatingJob(RepeatingSchedulableJob job, boolean update) {
    logObjectIfNotNull(job);
    validateRepeatingSchedulableJob(job);
    MotechEvent motechEvent = job.getMotechEvent();
    DateTime jobStartTime = job.getStartDate();
    DateTime jobEndTime = job.getEndDate();
    Integer repeatIntervalInSeconds = job.getRepeatIntervalInSeconds();
    Integer jobRepeatCount = job.getRepeatCount();
    if (null == jobRepeatCount) {
        jobRepeatCount = MAX_REPEAT_COUNT;
    }
    JobId jobId = new RepeatingJobId(motechEvent);
    JobDetail jobDetail = newJob(MotechScheduledJob.class).withIdentity(jobKey(jobId.value(), JOB_GROUP_NAME)).build();
    putMotechEventDataToJobDataMap(jobDetail.getJobDataMap(), motechEvent);
    jobDetail.getJobDataMap().put(EVENT_METADATA, createMetadataForMisfireSchedulableJob(job, motechEvent));
    try {
        if (scheduler.getTrigger(triggerKey(jobId.value(), JOB_GROUP_NAME)) != null) {
            unscheduleJob(jobId);
        }
    } catch (SchedulerException e) {
        throw new MotechSchedulerException(format("Schedule or reschedule the job: %s.\n%s", jobId, e.getMessage()), "scheduler.error.cantRescheduleJob", Arrays.asList(jobId.value(), e.getMessage()), e);
    }
    ScheduleBuilder scheduleBuilder;
    if (!job.isUseOriginalFireTimeAfterMisfire()) {
        SimpleScheduleBuilder simpleSchedule = simpleSchedule().withIntervalInSeconds(repeatIntervalInSeconds).withRepeatCount(jobRepeatCount);
        simpleSchedule = setMisfirePolicyForSimpleTrigger(simpleSchedule, schedulerSettings.getProperty("scheduler.repeating.trigger.misfire.policy"));
        scheduleBuilder = simpleSchedule;
    } else {
        if (job.getRepeatCount() != null) {
            final double half = 0.5;
            jobEndTime = new DateTime((long) (job.getStartDate().getMillis() + repeatIntervalInSeconds * MILLISECOND * (job.getRepeatCount() + half)));
        }
        scheduleBuilder = CalendarIntervalScheduleBuilder.calendarIntervalSchedule().withIntervalInSeconds(repeatIntervalInSeconds).withMisfireHandlingInstructionFireAndProceed();
    }
    Trigger trigger = buildJobDetail(job, DateUtil.toDate(jobStartTime), DateUtil.toDate(jobEndTime), jobId, jobDetail, scheduleBuilder);
    scheduleJob(jobDetail, trigger, update);
}
Also used : JobDetail(org.quartz.JobDetail) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) SchedulerException(org.quartz.SchedulerException) CronTrigger(org.quartz.CronTrigger) OperableTrigger(org.quartz.spi.OperableTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) PeriodIntervalScheduleBuilder(org.motechproject.scheduler.trigger.PeriodIntervalScheduleBuilder) ScheduleBuilder(org.quartz.ScheduleBuilder) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) CalendarIntervalScheduleBuilder(org.quartz.CalendarIntervalScheduleBuilder) CronScheduleBuilder(org.quartz.CronScheduleBuilder) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) MotechSchedulerException(org.motechproject.scheduler.exception.MotechSchedulerException) MotechEvent(org.motechproject.event.MotechEvent) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder) DateUtil.newDateTime(org.motechproject.commons.date.util.DateUtil.newDateTime) DateTime(org.joda.time.DateTime) RunOnceJobId(org.motechproject.scheduler.contract.RunOnceJobId) JobId(org.motechproject.scheduler.contract.JobId) RepeatingJobId(org.motechproject.scheduler.contract.RepeatingJobId) RepeatingPeriodJobId(org.motechproject.scheduler.contract.RepeatingPeriodJobId) CronJobId(org.motechproject.scheduler.contract.CronJobId)

Example 8 with SimpleScheduleBuilder

use of org.quartz.SimpleScheduleBuilder 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 9 with SimpleScheduleBuilder

use of org.quartz.SimpleScheduleBuilder in project pancm_project by xuwujing.

the class QuartzJdbcTest method startSchedule.

/**
 * 开始一个simpleSchedule()调度
 */
public static void startSchedule() {
    try {
        // 1、创建一个JobDetail实例,指定Quartz
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("job1_1", "jGroup1").build();
        // 触发器类型
        SimpleScheduleBuilder builder = SimpleScheduleBuilder.repeatSecondlyForTotalCount(5);
        // CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
        // 2、创建Trigger
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1_1", "tGroup1").startNow().withSchedule(builder).build();
        // 3、创建Scheduler
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();
        // 4、调度执行
        scheduler.scheduleJob(jobDetail, trigger);
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 关闭调度器
        scheduler.shutdown();
    } catch (SchedulerException e) {
        e.printStackTrace();
    }
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) SimpleScheduleBuilder(org.quartz.SimpleScheduleBuilder)

Aggregations

SimpleScheduleBuilder (org.quartz.SimpleScheduleBuilder)9 Trigger (org.quartz.Trigger)9 CronTrigger (org.quartz.CronTrigger)5 JobDetail (org.quartz.JobDetail)5 SimpleTrigger (org.quartz.SimpleTrigger)5 CronScheduleBuilder (org.quartz.CronScheduleBuilder)3 Scheduler (org.quartz.Scheduler)3 SchedulerException (org.quartz.SchedulerException)3 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)3 OperableTrigger (org.quartz.spi.OperableTrigger)3 Date (java.util.Date)2 DateTime (org.joda.time.DateTime)2 DateUtil.newDateTime (org.motechproject.commons.date.util.DateUtil.newDateTime)2 MotechEvent (org.motechproject.event.MotechEvent)2 CronJobId (org.motechproject.scheduler.contract.CronJobId)2 JobId (org.motechproject.scheduler.contract.JobId)2 RepeatingJobId (org.motechproject.scheduler.contract.RepeatingJobId)2 RepeatingPeriodJobId (org.motechproject.scheduler.contract.RepeatingPeriodJobId)2 RunOnceJobId (org.motechproject.scheduler.contract.RunOnceJobId)2 Job (org.quartz.Job)2