Search in sources :

Example 1 with SimpleTriggerFactoryBean

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

the class XsltTriggerService method generateXsltTrigger.

public SimpleTrigger generateXsltTrigger(Scheduler scheduler, String xslFile, String xmlFile, String endFilePath, String endFile, int datasetId, ExtractPropertyBean epBean, UserAccountBean userAccountBean, String locale, int cnt, String xsltPath, String triggerGroupName) {
    //Date startDateTime = new Date(System.currentTimeMillis());
    String jobName = datasetId + "_" + epBean.getExportFileName()[0];
    if (triggerGroupName != null)
        TRIGGER_GROUP_NAME = triggerGroupName;
    //WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    ApplicationContext context = null;
    try {
        context = (ApplicationContext) scheduler.getContext().get("applicationContext");
    } catch (SchedulerException e) {
        e.printStackTrace();
    }
    SimpleTriggerFactoryBean triggerFactoryBean = context.getBean(SimpleTriggerFactoryBean.class, xslFile, xmlFile, endFilePath, endFile, datasetId, epBean, userAccountBean, locale, cnt, xsltPath);
    SimpleTrigger trigger = triggerFactoryBean.getObject();
    return trigger;
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SchedulerException(org.quartz.SchedulerException) SimpleTriggerFactoryBean(org.springframework.scheduling.quartz.SimpleTriggerFactoryBean) SimpleTrigger(org.quartz.SimpleTrigger)

Example 2 with SimpleTriggerFactoryBean

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

the class Poller method schedulePolls.

private void schedulePolls() throws Exception {
    LOG.debug("Enter schedulePolls");
    Collection<PolledService> polledServices = m_pollerFrontEnd.getPolledServices();
    if (polledServices == null || polledServices.size() == 0) {
        LOG.warn("No polling scheduled.");
        LOG.debug("Exit schedulePolls");
        return;
    }
    long startTime = System.currentTimeMillis();
    long scheduleSpacing = m_initialSpreadTime / polledServices.size();
    for (PolledService polledService : polledServices) {
        String jobName = polledService.toString();
        // remove any currently scheduled job
        if (m_scheduler.deleteJob(new JobKey(jobName, PollJobDetail.GROUP))) {
            LOG.debug("Job for {} already scheduled.  Rescheduling", polledService);
        } else {
            LOG.debug("Scheduling job for {}", polledService);
        }
        Date initialPollTime = new Date(startTime);
        m_pollerFrontEnd.setInitialPollTime(polledService.getServiceId(), initialPollTime);
        /*
            JobBuilder.newJob(PollJob.class)
                .withIdentity(jobName)
                .usingJobData(PollJobDetail.JOB_DATA_MAP_KEY_POLLEDSERVICE, polledService)
                .usingJobData(PollJobDetail.JOB_DATA_MAP_KEY_POLLERFRONTEND, m_pollerFrontEnd);
            */
        PollJobDetail jobDetail = new PollJobDetail(jobName, PollJob.class);
        jobDetail.setPolledService(polledService);
        jobDetail.setPollerFrontEnd(m_pollerFrontEnd);
        SimpleTriggerFactoryBean triggerFactory = new PolledServiceTrigger(jobDetail);
        triggerFactory.setStartTime(initialPollTime);
        Trigger pollTrigger = triggerFactory.getObject();
        m_scheduler.scheduleJob(jobDetail, pollTrigger);
        startTime += scheduleSpacing;
    }
    LOG.debug("Exit schedulePolls");
}
Also used : JobKey(org.quartz.JobKey) Trigger(org.quartz.Trigger) SimpleTriggerFactoryBean(org.springframework.scheduling.quartz.SimpleTriggerFactoryBean) Date(java.util.Date)

Example 3 with SimpleTriggerFactoryBean

use of org.springframework.scheduling.quartz.SimpleTriggerFactoryBean 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 SimpleTriggerFactoryBean

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

the class QuartzConfiguration method simpleTriggerFactoryBean.

@Bean
@Scope("prototype")
@Lazy
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(String xslFile, String xmlFile, String endFilePath, String endFile, int datasetId, ExtractPropertyBean epBean, UserAccountBean userAccountBean, String locale, int cnt, String xsltPath) {
    SimpleTriggerFactoryBean triggerFactoryBean = new SimpleTriggerFactoryBean();
    triggerFactoryBean.setBeanName("trigger1");
    triggerFactoryBean.setGroup("group1");
    triggerFactoryBean.setRepeatInterval(1);
    triggerFactoryBean.setRepeatCount(0);
    //triggerFactoryBean.setStartTime(startDateTime);
    triggerFactoryBean.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
    // set job data map
    JobDataMap jobDataMap = new JobDataMap();
    jobDataMap.put(XSL_FILE_PATH, xslFile);
    jobDataMap.put(XML_FILE_PATH, endFilePath);
    jobDataMap.put(POST_FILE_PATH, endFilePath);
    jobDataMap.put(POST_FILE_NAME, endFile);
    jobDataMap.put(EXTRACT_PROPERTY, epBean.getId());
    jobDataMap.put(USER_ID, userAccountBean.getId());
    jobDataMap.put(STUDY_ID, userAccountBean.getActiveStudyId());
    jobDataMap.put(LOCALE, locale);
    jobDataMap.put(DATASET_ID, datasetId);
    jobDataMap.put(EMAIL, userAccountBean.getEmail());
    jobDataMap.put(ZIPPED, epBean.getZipFormat());
    jobDataMap.put(DELETE_OLD, epBean.getDeleteOld());
    jobDataMap.put(SUCCESS_MESSAGE, epBean.getSuccessMessage());
    jobDataMap.put(FAILURE_MESSAGE, epBean.getFailureMessage());
    jobDataMap.put(POST_PROC_DELETE_OLD, epBean.getPostProcDeleteOld());
    jobDataMap.put(POST_PROC_ZIP, epBean.getPostProcZip());
    jobDataMap.put(POST_PROC_LOCATION, epBean.getPostProcLocation());
    jobDataMap.put(POST_PROC_EXPORT_NAME, epBean.getPostProcExportName());
    jobDataMap.put(COUNT, cnt);
    jobDataMap.put(XSLT_PATH, xsltPath);
    // jobDataMap.put(DIRECTORY, directory);
    // jobDataMap.put(ExampleSpringJob.LOCALE, locale);
    jobDataMap.put(EP_BEAN, epBean);
    triggerFactoryBean.setJobDataMap(jobDataMap);
    return triggerFactoryBean;
}
Also used : JobDataMap(org.quartz.JobDataMap) SimpleTriggerFactoryBean(org.springframework.scheduling.quartz.SimpleTriggerFactoryBean) 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 5 with SimpleTriggerFactoryBean

use of org.springframework.scheduling.quartz.SimpleTriggerFactoryBean in project OpenOLAT by OpenOLAT.

the class SchedulerTest method testSimpleTrigger.

@Test
public void testSimpleTrigger() throws SchedulerException, ParseException {
    JobDetail job = newJob(SchedulerTestJob.class).withIdentity("schedulerTestJobSimpleTrigger", Scheduler.DEFAULT_GROUP).build();
    SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
    trigger.setName("Test scheduler trigger");
    trigger.setStartDelay(0);
    trigger.setRepeatInterval(1000);
    trigger.setRepeatCount(10);
    trigger.setJobDetail(job);
    trigger.afterPropertiesSet();
    // Schedule job now
    scheduler.scheduleJob(job, trigger.getObject());
    // because of cal.add(Calendar.MILLISECOND, 11);
    sleep(20);
    // check number of calls to the job
    org.quartz.Calendar quartzCal = scheduler.getCalendar(trigger.getObject().getCalendarName());
    Calendar cal = Calendar.getInstance();
    Date start = cal.getTime();
    cal.add(Calendar.SECOND, 5);
    cal.add(Calendar.MILLISECOND, 11);
    Date end = cal.getTime();
    Assert.assertEquals(5, TriggerUtils.computeFireTimesBetween((OperableTrigger) trigger.getObject(), quartzCal, start, end).size());
}
Also used : JobDetail(org.quartz.JobDetail) Calendar(java.util.Calendar) SimpleTriggerFactoryBean(org.springframework.scheduling.quartz.SimpleTriggerFactoryBean) Date(java.util.Date) Test(org.junit.Test)

Aggregations

SimpleTriggerFactoryBean (org.springframework.scheduling.quartz.SimpleTriggerFactoryBean)9 JobDetailFactoryBean (org.springframework.scheduling.quartz.JobDetailFactoryBean)4 Calendar (java.util.Calendar)3 Date (java.util.Date)3 Test (org.junit.Test)2 JobDetail (org.quartz.JobDetail)2 CronTriggerFactoryBean (org.springframework.scheduling.quartz.CronTriggerFactoryBean)2 SchedulerFactoryBean (org.springframework.scheduling.quartz.SchedulerFactoryBean)2 ParseException (java.text.ParseException)1 ExtractPropertyBean (org.akaza.openclinica.bean.extract.ExtractPropertyBean)1 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)1 JobDataMap (org.quartz.JobDataMap)1 JobKey (org.quartz.JobKey)1 SchedulerException (org.quartz.SchedulerException)1 SimpleTrigger (org.quartz.SimpleTrigger)1 Trigger (org.quartz.Trigger)1 ApplicationContext (org.springframework.context.ApplicationContext)1 Bean (org.springframework.context.annotation.Bean)1 MethodInvokingJobDetailFactoryBean (org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1