Search in sources :

Example 1 with JobInstance

use of org.springframework.batch.core.JobInstance in project head by mifos.

the class MifosBatchJob method catchUpMissedLaunches.

public void catchUpMissedLaunches(Job job, JobExecutionContext context) throws Exception {
    List<JobInstance> jobInstances = jobExplorer.getJobInstances(job.getName(), 0, 1);
    if (jobInstances.size() > 0) {
        JobInstance jobInstance = jobInstances.get(0);
        Date previousFireTime = new Date(jobInstance.getJobParameters().getLong(JOB_EXECUTION_TIME_KEY));
        Date scheduledFireTime = context.getScheduledFireTime();
        Trigger trigger = context.getTrigger();
        boolean onDemandRun = false;
        if (Scheduler.DEFAULT_MANUAL_TRIGGERS.equals(trigger.getGroup())) {
            // this is a manual run
            trigger = context.getScheduler().getTrigger(job.getName(), Scheduler.DEFAULT_GROUP);
            scheduledFireTime = new DateTimeService().getCurrentDateTime().toDate();
            onDemandRun = true;
        }
        List<Date> missedLaunches = computeMissedJobLaunches(previousFireTime, scheduledFireTime, trigger, onDemandRun);
        for (Date missedLaunch : missedLaunches) {
            JobParameters jobParameters = createJobParameters(missedLaunch.getTime());
            launchJob(job, jobParameters);
        }
    }
}
Also used : Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) JobInstance(org.springframework.batch.core.JobInstance) JobParameters(org.springframework.batch.core.JobParameters) DateTimeService(org.mifos.framework.util.DateTimeService) Date(java.util.Date)

Example 2 with JobInstance

use of org.springframework.batch.core.JobInstance in project head by mifos.

the class MifosScheduler method getJobsPreviousRunTime.

public Date getJobsPreviousRunTime(String jobName) {
    JobExplorer explorer = getBatchJobExplorer();
    List<JobInstance> jobInstances = explorer.getJobInstances(jobName, 0, 1);
    if (jobInstances.size() == 0) {
        return new Date(0);
    }
    JobInstance jobInstance = jobInstances.get(0);
    long executionTimeInMillis = jobInstance.getJobParameters().getLong(MifosBatchJob.JOB_EXECUTION_TIME_KEY);
    return new Date(executionTimeInMillis);
}
Also used : JobInstance(org.springframework.batch.core.JobInstance) JobExplorer(org.springframework.batch.core.explore.JobExplorer) Date(java.util.Date)

Example 3 with JobInstance

use of org.springframework.batch.core.JobInstance in project head by mifos.

the class MifosScheduler method getJobFailDescription.

public String getJobFailDescription(String jobName) {
    String failDescription = null;
    JobExplorer explorer = getBatchJobExplorer();
    List<JobInstance> jobInstances = explorer.getJobInstances(jobName, 0, 1);
    if (jobInstances.size() > 0) {
        List<JobExecution> jobExecutions = explorer.getJobExecutions(jobInstances.get(0));
        if (jobExecutions.size() > 0) {
            Collection<StepExecution> steps = jobExecutions.get(0).getStepExecutions();
            if (steps.size() > 0) {
                StepExecution step = steps.iterator().next();
                if (!step.getExitStatus().getExitDescription().isEmpty()) {
                    failDescription = step.getExitStatus().getExitDescription();
                }
            }
        }
    }
    return failDescription;
}
Also used : JobExecution(org.springframework.batch.core.JobExecution) JobInstance(org.springframework.batch.core.JobInstance) JobExplorer(org.springframework.batch.core.explore.JobExplorer) StepExecution(org.springframework.batch.core.StepExecution)

Example 4 with JobInstance

use of org.springframework.batch.core.JobInstance in project head by mifos.

the class MifosScheduler method getJobsLastSuccessfulRunTime.

public Date getJobsLastSuccessfulRunTime(String jobName) {
    JobExplorer explorer = getBatchJobExplorer();
    List<JobInstance> jobInstances = explorer.getJobInstances(jobName, 0, 100);
    for (JobInstance job : jobInstances) {
        for (JobExecution execution : explorer.getJobExecutions(job)) {
            if (BatchStatus.COMPLETED.equals(execution.getStatus())) {
                long executionTimeInMillis = job.getJobParameters().getLong(MifosBatchJob.JOB_EXECUTION_TIME_KEY);
                return new Date(executionTimeInMillis);
            }
        }
    }
    return new Date(0);
}
Also used : JobExecution(org.springframework.batch.core.JobExecution) JobInstance(org.springframework.batch.core.JobInstance) JobExplorer(org.springframework.batch.core.explore.JobExplorer) Date(java.util.Date)

Example 5 with JobInstance

use of org.springframework.batch.core.JobInstance 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)

Aggregations

JobInstance (org.springframework.batch.core.JobInstance)15 JobExecution (org.springframework.batch.core.JobExecution)13 JobExplorer (org.springframework.batch.core.explore.JobExplorer)12 Test (org.junit.Test)8 Date (java.util.Date)6 StepExecution (org.springframework.batch.core.StepExecution)3 JobLocator (org.springframework.batch.core.configuration.JobLocator)3 JobLauncher (org.springframework.batch.core.launch.JobLauncher)3 Ignore (org.junit.Ignore)2 CronTrigger (org.quartz.CronTrigger)2 JobParameters (org.springframework.batch.core.JobParameters)2 Calendar (java.util.Calendar)1 ProductStatus (org.mifos.framework.components.batchjobs.helpers.ProductStatus)1 DateTimeService (org.mifos.framework.util.DateTimeService)1 JobDataMap (org.quartz.JobDataMap)1 JobDetail (org.quartz.JobDetail)1 JobExecutionContext (org.quartz.JobExecutionContext)1 Scheduler (org.quartz.Scheduler)1 SimpleTrigger (org.quartz.SimpleTrigger)1 Trigger (org.quartz.Trigger)1