Search in sources :

Example 6 with CronTrigger

use of org.quartz.CronTrigger in project pinot by linkedin.

the class AlertJobSchedulerV2 method run.

public void run() {
    try {
        // read all alert configs
        LOG.info("Reading all alert configs..");
        List<AlertConfigDTO> alertConfigs = alertConfigDAO.findAll();
        // get active jobs
        List<String> scheduledJobs = getScheduledJobs();
        LOG.info("Scheduled jobs {}", scheduledJobs);
        for (AlertConfigDTO alertConfig : alertConfigs) {
            Long id = alertConfig.getId();
            String jobKey = getJobKey(id);
            boolean isActive = alertConfig.isActive();
            boolean isScheduled = scheduledJobs.contains(jobKey);
            if (isActive) {
                if (isScheduled) {
                    String cronInDatabase = alertConfig.getCronExpression();
                    List<Trigger> triggers = (List<Trigger>) quartzScheduler.getTriggersOfJob(JobKey.jobKey(jobKey));
                    CronTrigger cronTrigger = (CronTrigger) triggers.get(0);
                    String cronInSchedule = cronTrigger.getCronExpression();
                    // cron expression has been updated, restart this job
                    if (!cronInDatabase.equals(cronInSchedule)) {
                        LOG.info("Cron expression for config {} with jobKey {} has been changed from {}  to {}. " + "Restarting schedule", id, jobKey, cronInSchedule, cronInDatabase);
                        stopJob(jobKey);
                        startJob(alertConfig, jobKey);
                    }
                } else {
                    LOG.info("Found active but not scheduled {}", id);
                    startJob(alertConfig, jobKey);
                }
            } else {
                if (isScheduled) {
                    LOG.info("Found inactive but scheduled {}", id);
                    stopJob(jobKey);
                }
            // for all jobs with not isActive, and not isScheduled, no change required
            }
        }
        // stop the schedule, as function has been deleted
        for (String scheduledJobKey : scheduledJobs) {
            Long configId = getIdFromJobKey(scheduledJobKey);
            AlertConfigDTO alertConfigSpec = alertConfigDAO.findById(configId);
            if (alertConfigSpec == null) {
                LOG.info("Found scheduled, but not in database {}", configId);
                stopJob(scheduledJobKey);
            }
        }
    } catch (SchedulerException e) {
        LOG.error("Exception in reading active jobs", e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) ArrayList(java.util.ArrayList) List(java.util.List) AlertConfigDTO(com.linkedin.thirdeye.datalayer.dto.AlertConfigDTO)

Example 7 with CronTrigger

use of org.quartz.CronTrigger in project head by mifos.

the class MifosBatchJob method computeMissedJobLaunches.

@SuppressWarnings("unchecked")
public List<Date> computeMissedJobLaunches(Date from, Date to, Trigger trigger, boolean onDemandRun) throws Exception {
    List<Date> missedLaunches = new LinkedList<Date>();
    if (trigger instanceof CronTrigger) {
        CronTrigger cronTrigger = new CronTrigger();
        cronTrigger.setStartTime(from);
        cronTrigger.setNextFireTime(from);
        String cronExpression = ((CronTrigger) trigger).getCronExpression();
        try {
            cronTrigger.setCronExpression(cronExpression);
        } catch (ParseException pe) {
            throw new Exception(pe);
        }
        List<Date> computationOutcome = TriggerUtils.computeFireTimesBetween(cronTrigger, null, from, to);
        missedLaunches.addAll(computationOutcome);
        missedLaunches.remove(0);
        if (!onDemandRun && missedLaunches.size() > 0) {
            missedLaunches.remove(missedLaunches.size() - 1);
        }
    } else if (trigger instanceof SimpleTrigger) {
        SimpleTrigger simpleTrigger = new SimpleTrigger();
        simpleTrigger.setStartTime(from);
        simpleTrigger.setNextFireTime(from);
        simpleTrigger.setRepeatInterval(((SimpleTrigger) trigger).getRepeatInterval());
        simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
        List<Date> computationOutcome = TriggerUtils.computeFireTimesBetween(simpleTrigger, null, from, to);
        missedLaunches.addAll(computationOutcome);
        missedLaunches.remove(0);
        if (!onDemandRun && missedLaunches.size() > 0) {
            missedLaunches.remove(missedLaunches.size() - 1);
        }
    }
    return missedLaunches;
}
Also used : CronTrigger(org.quartz.CronTrigger) LinkedList(java.util.LinkedList) List(java.util.List) ParseException(java.text.ParseException) SimpleTrigger(org.quartz.SimpleTrigger) LinkedList(java.util.LinkedList) Date(java.util.Date) ParseException(java.text.ParseException) BatchJobException(org.mifos.framework.components.batchjobs.exceptions.BatchJobException) JobExecutionException(org.quartz.JobExecutionException) JobInstanceAlreadyCompleteException(org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException)

Example 8 with CronTrigger

use of org.quartz.CronTrigger in project head by mifos.

the class BatchjobsServiceFacadeWebTier method getBatchjobs.

@Override
public List<BatchjobsDto> getBatchjobs(ServletContext context) throws TaskSystemException, FileNotFoundException, IOException, SchedulerException {
    List<BatchjobsDto> batchjobs = new ArrayList<BatchjobsDto>();
    MifosScheduler mifosScheduler = (MifosScheduler) context.getAttribute(MifosScheduler.class.getName());
    Scheduler scheduler = mifosScheduler.getScheduler();
    for (String groupName : scheduler.getJobGroupNames()) {
        for (String jobName : scheduler.getJobNames(groupName)) {
            Trigger[] triggers = scheduler.getTriggersOfJob(jobName, groupName);
            Trigger trigger = triggers.length > 0 ? triggers[0] : null;
            JobDetail jobDetail = scheduler.getJobDetail(jobName, groupName);
            if (trigger != null && jobDetail != null) {
                Date nextFire = trigger.getNextFireTime() != null ? trigger.getNextFireTime() : new Date(0);
                Date lastFire = mifosScheduler.getJobsPreviousRunTime(jobName);
                Date lastSuccessfulRun = mifosScheduler.getJobsLastSuccessfulRunTime(jobName);
                int priority = trigger.getPriority();
                String frequency = "";
                String taskType = "";
                if (trigger instanceof CronTrigger) {
                    frequency = ((CronTrigger) trigger).getCronExpression();
                    taskType = CRON_TRIGGER;
                }
                if (trigger instanceof SimpleTrigger) {
                    frequency = Long.toString(((SimpleTrigger) trigger).getRepeatInterval());
                    taskType = SIMPLE_TRIGGER;
                }
                String previousRunStatus = mifosScheduler.getJobsPreviousRunStatus(jobName);
                int triggerState = scheduler.getTriggerState(trigger.getName(), groupName);
                String failDescription = mifosScheduler.getJobFailDescription(jobName);
                batchjobs.add(new BatchjobsDto(jobName, frequency, taskType, priority, previousRunStatus, lastFire, lastSuccessfulRun, nextFire, triggerState, failDescription));
            }
        }
    }
    return batchjobs;
}
Also used : CronTrigger(org.quartz.CronTrigger) Scheduler(org.quartz.Scheduler) MifosScheduler(org.mifos.framework.components.batchjobs.MifosScheduler) ArrayList(java.util.ArrayList) BatchjobsDto(org.mifos.application.admin.servicefacade.BatchjobsDto) MifosScheduler(org.mifos.framework.components.batchjobs.MifosScheduler) Date(java.util.Date) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) SimpleTrigger(org.quartz.SimpleTrigger)

Example 9 with CronTrigger

use of org.quartz.CronTrigger in project head by mifos.

the class BatchJobCatchUpIntegrationTest method testIncompleteTaskDelay.

@Test
public void testIncompleteTaskDelay() throws Exception {
    mifosScheduler = getMifosScheduler("org/mifos/framework/components/batchjobs/catchUpTask.xml");
    Scheduler scheduler = mifosScheduler.getScheduler();
    ProductStatus productStatusTask = new ProductStatus();
    productStatusTask.setJobExplorer(mifosScheduler.getBatchJobExplorer());
    productStatusTask.setJobLauncher(mifosScheduler.getBatchJobLauncher());
    productStatusTask.setJobLocator(mifosScheduler.getBatchJobLocator());
    productStatusTask.setJobRepository(mifosScheduler.getBatchJobRepository());
    String quartzJobName = "ProductStatusJob";
    String quartzTriggerName = "ProductStatusTrigger2";
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 10);
    Date previousFireTime = calendar.getTime();
    calendar.set(Calendar.SECOND, 21);
    Date quartzFireTime = calendar.getTime();
    calendar.set(Calendar.SECOND, 22);
    Date quartzNextFireTime = calendar.getTime();
    calendar.set(Calendar.SECOND, 20);
    Date quartzPrevFireTime = calendar.getTime();
    JobDetail jobDetail = scheduler.getJobDetail(quartzJobName, Scheduler.DEFAULT_GROUP);
    jobDetail.setJobDataMap(new JobDataMap());
    CronTrigger trigger = new CronTrigger(quartzTriggerName, Scheduler.DEFAULT_GROUP, quartzJobName, Scheduler.DEFAULT_GROUP, "* * * * * ?");
    trigger.setJobDataMap(new JobDataMap());
    TriggerFiredBundle triggerFiredBundle = new TriggerFiredBundle(jobDetail, trigger, new BaseCalendar(), false, quartzFireTime, quartzFireTime, quartzPrevFireTime, quartzNextFireTime);
    JobExecutionContext jobExecutionContext = new JobExecutionContext(scheduler, triggerFiredBundle, productStatusTask);
    JobLauncher jobLauncher = mifosScheduler.getBatchJobLauncher();
    JobLocator jobLocator = mifosScheduler.getBatchJobLocator();
    jobLauncher.run(jobLocator.getJob(jobName), MifosBatchJob.createJobParameters(previousFireTime.getTime()));
    Thread.sleep(1500);
    productStatusTask.catchUpMissedLaunches(jobLocator.getJob(jobName), jobExecutionContext);
    JobExplorer explorer = mifosScheduler.getBatchJobExplorer();
    List<JobInstance> jobInstances = explorer.getJobInstances(jobName, 0, 20);
    Assert.assertEquals(11, jobInstances.size());
    for (JobInstance jobInstance : jobInstances) {
        List<JobExecution> jobExecutions = explorer.getJobExecutions(jobInstance);
        Assert.assertEquals(BatchStatus.COMPLETED, jobExecutions.get(0).getStatus());
        Assert.assertEquals(calendar.getTimeInMillis(), jobInstance.getJobParameters().getLong(MifosBatchJob.JOB_EXECUTION_TIME_KEY));
        calendar.roll(Calendar.SECOND, false);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) CronTrigger(org.quartz.CronTrigger) JobLauncher(org.springframework.batch.core.launch.JobLauncher) BaseCalendar(org.quartz.impl.calendar.BaseCalendar) JobInstance(org.springframework.batch.core.JobInstance) Scheduler(org.quartz.Scheduler) Calendar(java.util.Calendar) BaseCalendar(org.quartz.impl.calendar.BaseCalendar) JobLocator(org.springframework.batch.core.configuration.JobLocator) ProductStatus(org.mifos.framework.components.batchjobs.helpers.ProductStatus) TriggerFiredBundle(org.quartz.spi.TriggerFiredBundle) Date(java.util.Date) JobExecution(org.springframework.batch.core.JobExecution) JobDetail(org.quartz.JobDetail) JobExecutionContext(org.quartz.JobExecutionContext) JobExplorer(org.springframework.batch.core.explore.JobExplorer) Test(org.junit.Test)

Example 10 with CronTrigger

use of org.quartz.CronTrigger in project openhab1-addons by openhab.

the class FHTBinding method scheduleJob.

/**
     * The user may configure this binding to update the internal clock of
     * FHT80b devices via rf command. The method takes care of scheduling this
     * job.
     */
private JobKey scheduleJob(Class<? extends Job> jobClass, String cronExpression) {
    JobKey jobKey = null;
    try {
        Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
        JobDetail detail = JobBuilder.newJob(jobClass).withIdentity("FHT " + jobClass.getSimpleName(), "cul").build();
        detail.getJobDataMap().put(FHTBinding.class.getName(), this);
        CronTrigger trigger = TriggerBuilder.newTrigger().forJob(detail).withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
        jobKey = detail.getKey();
        sched.scheduleJob(detail, trigger);
    } catch (SchedulerException e) {
        logger.error("Can't schedule time update job", e);
    }
    return jobKey;
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler)

Aggregations

CronTrigger (org.quartz.CronTrigger)42 JobDetail (org.quartz.JobDetail)20 Test (org.junit.Test)17 Trigger (org.quartz.Trigger)14 SchedulerException (org.quartz.SchedulerException)13 Scheduler (org.quartz.Scheduler)12 SimpleTrigger (org.quartz.SimpleTrigger)11 TriggerKey (org.quartz.TriggerKey)7 JobKey (org.quartz.JobKey)5 Date (java.util.Date)4 CamelContext (org.apache.camel.CamelContext)4 JobDataMap (org.quartz.JobDataMap)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 JobContext (org.craftercms.engine.util.quartz.JobContext)2 CronScheduleBuilder (org.quartz.CronScheduleBuilder)2 JobExecutionException (org.quartz.JobExecutionException)2