Search in sources :

Example 1 with JobDetailFactoryBean

use of org.springframework.scheduling.quartz.JobDetailFactoryBean 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 2 with JobDetailFactoryBean

use of org.springframework.scheduling.quartz.JobDetailFactoryBean in project series-rest-api by 52North.

the class JobUtils method createJobDetail.

default JobDetailFactoryBean createJobDetail(Class<? extends Job> jobClass, String jobName, String group) {
    getLogger().debug("createJobDetail(jobClass={}, jobName={}, groupName={})", jobClass.getName(), jobName, group);
    JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
    factoryBean.setName(jobName);
    factoryBean.setJobClass(jobClass);
    factoryBean.setGroup(group);
    // job has to be durable to be stored in DB:
    factoryBean.setDurability(true);
    factoryBean.afterPropertiesSet();
    return factoryBean;
}
Also used : JobDetailFactoryBean(org.springframework.scheduling.quartz.JobDetailFactoryBean)

Example 3 with JobDetailFactoryBean

use of org.springframework.scheduling.quartz.JobDetailFactoryBean in project OpenClinica by OpenClinica.

the class QuartzConfiguration method getJobDetailFactoryBean.

@Bean
@Scope("prototype")
@Lazy
public JobDetailFactoryBean getJobDetailFactoryBean(SimpleTrigger simpleTrigger, String triggerGroupName) {
    JobDetailFactoryBean jobDetailFactoryBean;
    jobDetailFactoryBean = new JobDetailFactoryBean();
    jobDetailFactoryBean.setGroup(triggerGroupName);
    jobDetailFactoryBean.setName(simpleTrigger.getKey().getName() + System.currentTimeMillis());
    jobDetailFactoryBean.setJobClass(org.akaza.openclinica.job.XsltStatefulJob.class);
    jobDetailFactoryBean.setJobDataMap(simpleTrigger.getJobDataMap());
    // need durability? YES - we will want to see if it's finished
    jobDetailFactoryBean.setDurability(true);
    return jobDetailFactoryBean;
}
Also used : JobDetailFactoryBean(org.springframework.scheduling.quartz.JobDetailFactoryBean) MethodInvokingJobDetailFactoryBean(org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean) JobDetailFactoryBean(org.springframework.scheduling.quartz.JobDetailFactoryBean) UserAccountBean(org.akaza.openclinica.bean.login.UserAccountBean) SimpleTriggerFactoryBean(org.springframework.scheduling.quartz.SimpleTriggerFactoryBean) MethodInvokingJobDetailFactoryBean(org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean) ExtractPropertyBean(org.akaza.openclinica.bean.extract.ExtractPropertyBean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) CronTriggerFactoryBean(org.springframework.scheduling.quartz.CronTriggerFactoryBean)

Example 4 with JobDetailFactoryBean

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

the class QuartzReportScheduler method execute.

@Override
public UUID execute(UUID jobId, int userId, UUID reportUuid, Map<String, Object> parameters) {
    if (jobId == null) {
        jobId = UUID.randomUUID();
    }
    log.debug("New job " + jobId + " for report " + reportUuid);
    String groupName = reportUuid.toString();
    JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
    jobDetailFactory.setName(jobId.toString());
    jobDetailFactory.setGroup(groupName);
    jobDetailFactory.setJobClass(GeneratorJob.class);
    jobDetailFactory.afterPropertiesSet();
    JobDetail jobDetail = jobDetailFactory.getObject();
    final JobDataMap dataMap = jobDetail.getJobDataMap();
    dataMap.put("jobId", jobId);
    dataMap.put("reportId", reportUuid);
    dataMap.put("parameters", parameters);
    dataMap.put("userId", userId);
    SimpleTriggerFactoryBean triggerFactoryBean = new SimpleTriggerFactoryBean();
    triggerFactoryBean.setName(jobId.toString() + "_trigger");
    triggerFactoryBean.setGroup(groupName);
    triggerFactoryBean.setJobDetail(jobDetail);
    triggerFactoryBean.setRepeatCount(0);
    triggerFactoryBean.afterPropertiesSet();
    Trigger trigger = triggerFactoryBean.getObject();
    try {
        scheduler.scheduleJob(jobDetail, trigger);
    } catch (SchedulerException e) {
        log.error("Can't schedule job", e);
        jobId = null;
    }
    return jobId;
}
Also used : SimpleTriggerFactoryBean(org.springframework.scheduling.quartz.SimpleTriggerFactoryBean) JobDetailFactoryBean(org.springframework.scheduling.quartz.JobDetailFactoryBean)

Example 5 with JobDetailFactoryBean

use of org.springframework.scheduling.quartz.JobDetailFactoryBean in project tutorials by eugenp.

the class SpringQrtzScheduler method jobDetail.

@Bean
public JobDetailFactoryBean jobDetail() {
    JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
    jobDetailFactory.setJobClass(SampleJob.class);
    jobDetailFactory.setName("Qrtz_Job_Detail");
    jobDetailFactory.setDescription("Invoke Sample Job service...");
    jobDetailFactory.setDurability(true);
    return jobDetailFactory;
}
Also used : JobDetailFactoryBean(org.springframework.scheduling.quartz.JobDetailFactoryBean) JobDetailFactoryBean(org.springframework.scheduling.quartz.JobDetailFactoryBean) SimpleTriggerFactoryBean(org.springframework.scheduling.quartz.SimpleTriggerFactoryBean) Bean(org.springframework.context.annotation.Bean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean)

Aggregations

JobDetailFactoryBean (org.springframework.scheduling.quartz.JobDetailFactoryBean)5 SimpleTriggerFactoryBean (org.springframework.scheduling.quartz.SimpleTriggerFactoryBean)4 CronTriggerFactoryBean (org.springframework.scheduling.quartz.CronTriggerFactoryBean)2 SchedulerFactoryBean (org.springframework.scheduling.quartz.SchedulerFactoryBean)2 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 ExtractPropertyBean (org.akaza.openclinica.bean.extract.ExtractPropertyBean)1 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)1 Bean (org.springframework.context.annotation.Bean)1 MethodInvokingJobDetailFactoryBean (org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean)1