Search in sources :

Example 1 with CronTriggerFactoryBean

use of org.springframework.scheduling.quartz.CronTriggerFactoryBean in project simba-os by cegeka.

the class TriggerConfiguration method createCronTrigger.

private CronTriggerFactoryBean createCronTrigger(JobDetail jobDetail, String cronExpression) {
    CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
    bean.setJobDetail(jobDetail);
    bean.setGroup(SIMBA_QUARTZ_GROUP);
    bean.setCronExpression(cronExpression);
    return bean;
}
Also used : CronTriggerFactoryBean(org.springframework.scheduling.quartz.CronTriggerFactoryBean)

Example 2 with CronTriggerFactoryBean

use of org.springframework.scheduling.quartz.CronTriggerFactoryBean in project opennms by OpenNMS.

the class Statsd method scheduleReport.

private void scheduleReport(ReportDefinition reportDef) throws ClassNotFoundException, NoSuchMethodException, ParseException, SchedulerException, Exception {
    // this is most likely reentrant since the method is private and called from start via plural version.
    synchronized (m_scheduler) {
        MethodInvokingJobDetailFactoryBean jobFactory = new MethodInvokingJobDetailFactoryBean();
        jobFactory.setTargetObject(this);
        jobFactory.setTargetMethod("runReport");
        jobFactory.setArguments(new Object[] { reportDef });
        jobFactory.setConcurrent(false);
        jobFactory.setBeanName(reportDef.getDescription());
        jobFactory.afterPropertiesSet();
        JobDetail jobDetail = (JobDetail) jobFactory.getObject();
        CronTriggerFactoryBean cronReportTrigger = new CronTriggerFactoryBean();
        cronReportTrigger.setBeanName(reportDef.getDescription());
        cronReportTrigger.setJobDetail(jobDetail);
        cronReportTrigger.setCronExpression(reportDef.getCronExpression());
        cronReportTrigger.afterPropertiesSet();
        m_scheduler.scheduleJob(jobDetail, cronReportTrigger.getObject());
        LOG.debug("Schedule report {}", cronReportTrigger);
    }
}
Also used : JobDetail(org.quartz.JobDetail) CronTriggerFactoryBean(org.springframework.scheduling.quartz.CronTriggerFactoryBean) MethodInvokingJobDetailFactoryBean(org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean)

Example 3 with CronTriggerFactoryBean

use of org.springframework.scheduling.quartz.CronTriggerFactoryBean in project netxms by netxms.

the class QuartzReportScheduler method addRecurrent.

@Override
public UUID addRecurrent(UUID jobId, UUID reportUuid, int jobType, int daysOfWeek, int daysOfMonth, Date startTime, Map<String, Object> parameters, int userId) {
    Calendar dateTime = Calendar.getInstance();
    dateTime.setTime(startTime);
    int hours = dateTime.get(Calendar.HOUR_OF_DAY);
    int minutes = dateTime.get(Calendar.MINUTE);
    log.debug(String.format("reportId=%1$s, hours=%2$d, minutes=%3$d, dow=%4$d, dom=%5$d", reportUuid, hours, minutes, daysOfWeek, daysOfMonth));
    if (jobId == null) {
        jobId = UUID.randomUUID();
    }
    // TODO: Remove old jobs and triggers
    // TODO: check for running jobs
    final String reportKey = reportUuid.toString();
    JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
    jobDetailFactory.setJobClass(GeneratorJob.class);
    jobDetailFactory.setName(jobId.toString());
    jobDetailFactory.setGroup(reportKey);
    jobDetailFactory.afterPropertiesSet();
    JobDetail job = jobDetailFactory.getObject();
    final JobDataMap dataMap = job.getJobDataMap();
    dataMap.put("jobId", jobId);
    dataMap.put("jobType", jobType);
    dataMap.put("reportId", reportUuid);
    dataMap.put("userId", userId);
    dataMap.put("parameters", parameters);
    dataMap.put("startDate", startTime);
    dataMap.put("daysOfWeek", daysOfWeek);
    dataMap.put("daysOfMonth", daysOfMonth);
    Trigger trigger;
    if (jobType == TYPE_ONCE) {
        SimpleTriggerFactoryBean triggerFactoryBean = new SimpleTriggerFactoryBean();
        triggerFactoryBean.setName(jobId.toString() + "_trigger");
        triggerFactoryBean.setGroup(reportKey);
        triggerFactoryBean.setJobDetail(job);
        triggerFactoryBean.setRepeatCount(0);
        triggerFactoryBean.setStartTime(startTime);
        triggerFactoryBean.afterPropertiesSet();
        trigger = triggerFactoryBean.getObject();
    } else {
        String cronExpression = null;
        switch(jobType) {
            case TYPE_DAILY:
                cronExpression = "0 " + minutes + " " + hours + " * * ?";
                break;
            case TYPE_WEEKLY:
                String sDays = "";
                for (int i = 0; i < 7; i++) {
                    if (((daysOfWeek >> i) & 0x01) != 0) {
                        sDays += (sDays.length() > 0 ? "," : "") + dayOfWeekNames[(7 - i) - 1];
                    }
                }
                if (sDays.length() > 0) {
                    cronExpression = "0 " + minutes + " " + hours + " ? * " + sDays;
                }
                break;
            case TYPE_MONTHLY:
                String sDayOfM = "";
                for (int i = 0; i < 31; i++) {
                    if (((daysOfMonth >> i) & 0x01) != 0)
                        sDayOfM += (sDayOfM.length() > 0 ? "," : "") + String.valueOf(31 - i);
                }
                if (sDayOfM.length() > 0) {
                    cronExpression = "0 " + minutes + " " + hours + " " + sDayOfM + " * ?";
                }
                break;
        }
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(job);
        cronTriggerFactoryBean.setName(jobId.toString() + "_trigger");
        cronTriggerFactoryBean.setGroup(reportKey);
        cronTriggerFactoryBean.setCronExpression(cronExpression);
        try {
            cronTriggerFactoryBean.afterPropertiesSet();
        } catch (ParseException e) {
            log.error("Can't parse cron expression " + cronExpression, e);
        }
        trigger = cronTriggerFactoryBean.getObject();
    }
    try {
        scheduler.getListenerManager().addJobListener(new ReportingJobListener(jobId, jobType), KeyMatcher.keyEquals(job.getKey()));
        scheduler.scheduleJob(job, trigger);
    } catch (SchedulerException e) {
        log.error("Can't schedule job", e);
        jobId = null;
    }
    return jobId;
}
Also used : Calendar(java.util.Calendar) SimpleTriggerFactoryBean(org.springframework.scheduling.quartz.SimpleTriggerFactoryBean) CronTriggerFactoryBean(org.springframework.scheduling.quartz.CronTriggerFactoryBean) ParseException(java.text.ParseException) JobDetailFactoryBean(org.springframework.scheduling.quartz.JobDetailFactoryBean)

Example 4 with CronTriggerFactoryBean

use of org.springframework.scheduling.quartz.CronTriggerFactoryBean in project kylo by Teradata.

the class QuartzScheduler method scheduleJob.

public void scheduleJob(JobIdentifier jobIdentifier, TriggerIdentifier triggerIdentifier, Object obj, String targetMethod, String cronExpression, Map<String, Object> jobData) throws SchedulerException {
    MethodInvokingJobDetailFactoryBean jobDetailFactory = new MethodInvokingJobDetailFactoryBean();
    jobDetailFactory.setTargetObject(obj);
    jobDetailFactory.setTargetMethod(targetMethod);
    jobDetailFactory.setName(jobIdentifier.getName());
    jobDetailFactory.setGroup(jobIdentifier.getGroup());
    CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
    triggerFactoryBean.setCronExpression(cronExpression);
    triggerFactoryBean.setJobDetail(jobDetailFactory.getObject());
    triggerFactoryBean.setGroup(triggerIdentifier.getGroup());
    triggerFactoryBean.setName(triggerIdentifier.getName());
    scheduleJob(jobDetailFactory, triggerFactoryBean);
}
Also used : CronTriggerFactoryBean(org.springframework.scheduling.quartz.CronTriggerFactoryBean) MethodInvokingJobDetailFactoryBean(org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean)

Example 5 with CronTriggerFactoryBean

use of org.springframework.scheduling.quartz.CronTriggerFactoryBean in project weicoder by wdcode.

the class QuartzStarter method init.

/**
 * 初始化
 */
@PostConstruct
protected void init() {
    // 定时任务
    if (QuartzParams.SPRING) {
        // 声明定时对象
        List<Trigger> triggers = Lists.newList();
        // 循环设置
        for (Job job : context.getBeans(Job.class).values()) {
            // 设置任务
            for (Map.Entry<String, String> e : job.getTriggers().entrySet()) {
                // 声明方法执行bean
                MethodInvokingJobDetailFactoryBean method = new MethodInvokingJobDetailFactoryBean();
                // 设置任务对象
                method.setTargetObject(job);
                // 设置不可以并发
                method.setConcurrent(false);
                // // 设置执行方法
                method.setTargetMethod(e.getKey());
                // 设置group
                method.setGroup(job.getClass().getSimpleName());
                // 设置beanName
                method.setBeanName(e.getKey());
                try {
                    // 执行初始化
                    method.afterPropertiesSet();
                } catch (Exception ex) {
                }
                // 执行执行时间
                for (String trigger : e.getValue().split(StringConstants.COMMA)) {
                    // 执行时间对象
                    CronTriggerFactoryBean cron = new CronTriggerFactoryBean();
                    // 设置group
                    cron.setGroup(method.getTargetObject().getClass().getSimpleName());
                    // 设置beanName
                    cron.setBeanName(method.getTargetMethod());
                    // 设置任务对象
                    cron.setJobDetail(method.getObject());
                    // 设置时间
                    cron.setCronExpression(trigger);
                    // 执行初始化
                    try {
                        cron.afterPropertiesSet();
                    } catch (ParseException ex) {
                        Logs.error(ex);
                    }
                    // 添加到定时列表中
                    triggers.add(cron.getObject());
                }
            }
        }
        // 定时任务不为空
        if (!EmptyUtil.isEmpty(triggers)) {
            // 声明执行定时方法工厂
            SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
            // 设置执行时间
            scheduler.setTriggers(Lists.toArray(triggers));
            // scheduler.init();
            try {
                // 执行初始化
                scheduler.afterPropertiesSet();
            } catch (Exception e) {
            }
            // 执行
            scheduler.start();
        }
    }
}
Also used : Trigger(org.quartz.Trigger) CronTriggerFactoryBean(org.springframework.scheduling.quartz.CronTriggerFactoryBean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) ParseException(java.text.ParseException) Job(com.weicoder.frame.quartz.Job) Map(java.util.Map) MethodInvokingJobDetailFactoryBean(org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean) ParseException(java.text.ParseException) PostConstruct(javax.annotation.PostConstruct)

Aggregations

CronTriggerFactoryBean (org.springframework.scheduling.quartz.CronTriggerFactoryBean)7 MethodInvokingJobDetailFactoryBean (org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean)4 ParseException (java.text.ParseException)2 Calendar (java.util.Calendar)2 Job (com.weicoder.frame.quartz.Job)1 Map (java.util.Map)1 PostConstruct (javax.annotation.PostConstruct)1 JobDetail (org.quartz.JobDetail)1 Trigger (org.quartz.Trigger)1 JobDetailFactoryBean (org.springframework.scheduling.quartz.JobDetailFactoryBean)1 SchedulerFactoryBean (org.springframework.scheduling.quartz.SchedulerFactoryBean)1 SimpleTriggerFactoryBean (org.springframework.scheduling.quartz.SimpleTriggerFactoryBean)1