Search in sources :

Example 1 with JobExecutionContext

use of org.quartz.JobExecutionContext 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 2 with JobExecutionContext

use of org.quartz.JobExecutionContext in project opennms by OpenNMS.

the class ImportSchedulerIT method createJobAndVerifyImportJobFactoryIsRegistered.

@Test
public void createJobAndVerifyImportJobFactoryIsRegistered() throws SchedulerException, InterruptedException {
    RequisitionDef def = m_dao.getDefs().get(0);
    JobDetail detail = new JobDetailImpl("test", ImportScheduler.JOB_GROUP, ImportJob.class, false, false);
    detail.getJobDataMap().put(ImportJob.URL, def.getImportUrlResource().orElse(null));
    detail.getJobDataMap().put(ImportJob.RESCAN_EXISTING, def.getRescanExisting());
    class MyBoolWrapper {

        volatile Boolean m_called = false;

        public Boolean getCalled() {
            return m_called;
        }

        public void setCalled(Boolean called) {
            m_called = called;
        }
    }
    final MyBoolWrapper callTracker = new MyBoolWrapper();
    m_importScheduler.getScheduler().getListenerManager().addTriggerListener(new TriggerListener() {

        @Override
        public String getName() {
            return "TestTriggerListener";
        }

        @Override
        public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
            LOG.info("triggerComplete called on trigger listener");
            callTracker.setCalled(true);
        }

        @Override
        public void triggerFired(Trigger trigger, JobExecutionContext context) {
            LOG.info("triggerFired called on trigger listener");
            Job jobInstance = context.getJobInstance();
            if (jobInstance instanceof ImportJob) {
                Assert.assertNotNull(((ImportJob) jobInstance).getProvisioner());
                Assert.assertTrue(context.getJobDetail().getJobDataMap().containsKey(ImportJob.URL));
                Assert.assertEquals("dns://localhost/localhost", context.getJobDetail().getJobDataMap().get(ImportJob.URL));
                Assert.assertTrue(context.getJobDetail().getJobDataMap().containsKey(ImportJob.RESCAN_EXISTING));
                Assert.assertEquals("dbonly", context.getJobDetail().getJobDataMap().get(ImportJob.RESCAN_EXISTING));
            }
            callTracker.setCalled(true);
        }

        @Override
        public void triggerMisfired(Trigger trigger) {
            LOG.info("triggerMisFired called on trigger listener");
            callTracker.setCalled(true);
        }

        @Override
        public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
            LOG.info("vetoJobExecution called on trigger listener");
            callTracker.setCalled(true);
            return false;
        }
    });
    Calendar testCal = Calendar.getInstance();
    testCal.add(Calendar.SECOND, 5);
    SimpleTriggerImpl trigger = new SimpleTriggerImpl("test", ImportScheduler.JOB_GROUP, testCal.getTime());
    m_importScheduler.getScheduler().scheduleJob(detail, trigger);
    m_importScheduler.start();
    int callCheck = 0;
    while (!callTracker.getCalled() && callCheck++ < 2) {
        Thread.sleep(5000);
    }
// TODO: need to fix the interrupted exception that occurs in the provisioner
}
Also used : SimpleTriggerImpl(org.quartz.impl.triggers.SimpleTriggerImpl) Calendar(java.util.Calendar) TriggerListener(org.quartz.TriggerListener) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) JobDetailImpl(org.quartz.impl.JobDetailImpl) JobExecutionContext(org.quartz.JobExecutionContext) RequisitionDef(org.opennms.netmgt.config.provisiond.RequisitionDef) Job(org.quartz.Job) Test(org.junit.Test)

Example 3 with JobExecutionContext

use of org.quartz.JobExecutionContext in project wso2-synapse by wso2.

the class QuartzTaskManager method isTaskRunning.

@Override
public boolean isTaskRunning(Object taskKey) {
    if (!(taskKey instanceof JobKey)) {
        return false;
    }
    try {
        List<JobExecutionContext> currentJobs;
        synchronized (lock) {
            currentJobs = this.scheduler.getCurrentlyExecutingJobs();
        }
        JobKey currentJobKey;
        for (JobExecutionContext jobCtx : currentJobs) {
            currentJobKey = jobCtx.getJobDetail().getKey();
            if (currentJobKey.compareTo((JobKey) taskKey) == 0) {
                logger.warn("the job is already running");
                return true;
            }
        }
    } catch (SchedulerException e) {
        return false;
    }
    return false;
}
Also used : JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException) JobExecutionContext(org.quartz.JobExecutionContext)

Example 4 with JobExecutionContext

use of org.quartz.JobExecutionContext in project xwiki-platform by xwiki.

the class AbstractJob method execute.

@Override
public final void execute(JobExecutionContext jobContext) throws JobExecutionException {
    JobDataMap data = jobContext.getJobDetail().getJobDataMap();
    // The XWiki context was saved in the Job execution data map. Get it as we'll retrieve
    // the script to execute from it.
    this.xcontext = (XWikiContext) data.get("context");
    // Clone the XWikiContex to have a new one for each run
    this.xcontext = this.xcontext.clone();
    // Init execution context
    Execution execution;
    try {
        ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
        execution = Utils.getComponent(Execution.class);
        ExecutionContext context = new ExecutionContext();
        // Bridge with old XWiki Context, required for old code
        this.xcontext.declareInExecutionContext(context);
        ecim.initialize(context);
    } catch (ExecutionContextException e) {
        throw new JobExecutionException("Fail to initialize execution context", e);
    }
    try {
        // Execute the job
        executeJob(jobContext);
    } finally {
        // We must ensure we clean the ThreadLocal variables located in the Execution
        // component as otherwise we will have a potential memory leak.
        execution.removeContext();
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Execution(org.xwiki.context.Execution) JobExecutionContext(org.quartz.JobExecutionContext) ExecutionContext(org.xwiki.context.ExecutionContext) JobExecutionException(org.quartz.JobExecutionException) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 5 with JobExecutionContext

use of org.quartz.JobExecutionContext in project incubator-gobblin by apache.

the class JobScheduler method shutDown.

@Override
protected void shutDown() throws Exception {
    LOG.info("Stopping the job scheduler");
    closer.close();
    cancelRequested = true;
    List<JobExecutionContext> currentExecutions = this.scheduler.getScheduler().getCurrentlyExecutingJobs();
    for (JobExecutionContext jobExecutionContext : currentExecutions) {
        try {
            this.scheduler.getScheduler().interrupt(jobExecutionContext.getFireInstanceId());
        } catch (UnableToInterruptJobException e) {
            LOG.error("Failed to cancel job " + jobExecutionContext.getJobDetail().getKey(), e);
        }
    }
    ExecutorsUtils.shutdownExecutorService(this.jobExecutor, Optional.of(LOG));
}
Also used : UnableToInterruptJobException(org.quartz.UnableToInterruptJobException) JobExecutionContext(org.quartz.JobExecutionContext)

Aggregations

JobExecutionContext (org.quartz.JobExecutionContext)41 Test (org.junit.Test)18 JobDetail (org.quartz.JobDetail)17 JobDataMap (org.quartz.JobDataMap)11 Set (java.util.Set)10 JobExecutionException (org.quartz.JobExecutionException)8 VirtConsumerMap (org.candlepin.model.VirtConsumerMap)7 SchedulerException (org.quartz.SchedulerException)7 List (java.util.List)6 ArrayList (java.util.ArrayList)4 Consumer (org.candlepin.model.Consumer)4 Job (org.quartz.Job)4 Scheduler (org.quartz.Scheduler)4 Date (java.util.Date)3 Map (java.util.Map)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 LoggerWrapper (com.searchcode.app.util.LoggerWrapper)2 IOException (java.io.IOException)2