Search in sources :

Example 16 with JobExecutionContext

use of org.quartz.JobExecutionContext in project kernel by exoplatform.

the class JobSchedulerServiceImpl method stop.

public void stop() {
    try {
        List<JobExecutionContext> jobs = getAllExcutingJobs();
        for (JobExecutionContext ctx : jobs) {
            Job job = ctx.getJobInstance();
            if (job instanceof InterruptableJob) {
                ((InterruptableJob) job).interrupt();
            }
        }
        scheduler_.shutdown(true);
    } catch (Exception ex) {
        LOG.warn("Could not interrupt all the current jobs properly", ex);
    }
}
Also used : InterruptableJob(org.quartz.InterruptableJob) JobExecutionContext(org.quartz.JobExecutionContext) Job(org.quartz.Job) InterruptableJob(org.quartz.InterruptableJob) PeriodJob(org.exoplatform.services.scheduler.PeriodJob) CronJob(org.exoplatform.services.scheduler.CronJob) SchedulerException(org.quartz.SchedulerException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Example 17 with JobExecutionContext

use of org.quartz.JobExecutionContext in project ddf by codice.

the class CommandJobTest method createMockJobExecutionContext.

private JobExecutionContext createMockJobExecutionContext(String command) {
    JobDataMap jobDataMap = new JobDataMap();
    jobDataMap.put(CommandJob.COMMAND_KEY, command);
    JobExecutionContext context = mock(JobExecutionContext.class);
    when(context.getMergedJobDataMap()).thenReturn(jobDataMap);
    return context;
}
Also used : JobDataMap(org.quartz.JobDataMap) JobExecutionContext(org.quartz.JobExecutionContext)

Example 18 with JobExecutionContext

use of org.quartz.JobExecutionContext in project ddf by codice.

the class CommandJobTest method testNullMergedJobDataMap.

/**
 * Tests that there is no exception when the {@link JobDataMap} of the {@link JobExecutionContext}
 * is null
 */
@Test
public void testNullMergedJobDataMap() {
    // given
    CommandJob commandJob = new CommandJob(new Security());
    JobExecutionContext jobExecutionContext = mock(JobExecutionContext.class);
    when(jobExecutionContext.getMergedJobDataMap()).thenReturn(null);
    // when
    commandJob.execute(jobExecutionContext);
}
Also used : JobExecutionContext(org.quartz.JobExecutionContext) Security(org.codice.ddf.security.impl.Security) Test(org.junit.Test)

Example 19 with JobExecutionContext

use of org.quartz.JobExecutionContext in project jaffa-framework by jaffa-projects.

the class TransactionInvokerJob method execute.

/**
 * Called by the <code>{@link org.quartz.Scheduler}</code> when a
 * <code>{@link org.quartz.Trigger}</code> fires that is associated with
 * the <code>Job</code>.
 *
 * @param context The Quartz JobExecutionContext structure used to retrieve job specific information
 * @throws JobExecutionException if there is an exception while executing the job.
 */
public void execute(JobExecutionContext context) throws JobExecutionException {
    String scheduledTaskId = null;
    String userId = null;
    Object businessObject = null;
    UserContextWrapper ucw = null;
    try {
        List<JobExecutionContext> jobs = context.getScheduler().getCurrentlyExecutingJobs();
        if (log.isDebugEnabled())
            log.debug("Job Size:- " + jobs.size());
        if (log.isDebugEnabled())
            log.debug("Starting Checking Scheduled Tasks");
        for (JobExecutionContext job : jobs) {
            if (log.isDebugEnabled()) {
                log.debug("Job Trigger: " + job.getTrigger());
                log.debug("Context Trigger: " + context.getTrigger());
            }
            if (job.getTrigger().equals(context.getTrigger()) && !job.getJobInstance().equals(this)) {
                if (log.isDebugEnabled())
                    log.debug("There's another instance running, so leaving" + this);
                return;
            }
        }
        // Obtain the Task from the Job details
        ScheduledTask task = QuartzSchedulerHelper.jobDataMapToTask(context.getJobDetail().getJobDataMap());
        if (log.isDebugEnabled())
            log.debug("Executing " + task);
        scheduledTaskId = task.getScheduledTaskId();
        userId = task.getRunAs();
        businessObject = task.getBusinessObject();
        // Switch the thread to use the context the "RunAs" is set to, so this propergates when creating the JaffaTransaction/JMS records to process
        if (log.isDebugEnabled())
            log.debug("Set up use context. RunAs = " + userId);
        ucw = UserContextWrapperFactory.instance(userId);
        // Sets Log4J's MDC to enable BusinessEventLogging
        MDC.put(BusinessEventLogMeta.SCHEDULED_TASK_ID, scheduledTaskId);
        MDC.put(BusinessEventLogMeta.LOGGED_BY, userId);
        // Set up some Thread Level variables for the Jaffa Transaction. This will help distinguish between a Schedule invoke transaction and any others
        ContextManagerFactory.instance().setProperty(CONTEXT_SCHEDULED_TASK_ID, scheduledTaskId);
        // Send a Jaffa Transaction message
        if (log.isInfoEnabled())
            log.info(MessageHelper.findMessage("label.Jaffa.Scheduler.JaffaTransactionInvokerJob.start", new Object[] { businessObject, userId, scheduledTaskId }));
        UOWHelper.addMessage(businessObject);
        if (log.isInfoEnabled())
            log.info(MessageHelper.findMessage("label.Jaffa.Scheduler.JaffaTransactionInvokerJob.success", new Object[] { businessObject, userId, scheduledTaskId }));
    } catch (Exception e) {
        log.error(MessageHelper.findMessage("error.Jaffa.Scheduler.JaffaTransactionInvokerJob.error", new Object[] { businessObject, userId, scheduledTaskId }), e);
        throw new JobExecutionException(e);
    } finally {
        // Unset the Logging context
        MDC.remove(BusinessEventLogMeta.SCHEDULED_TASK_ID);
        MDC.remove(BusinessEventLogMeta.LOGGED_BY);
        // Clear context for this user
        if (ucw != null) {
            if (log.isDebugEnabled())
                log.debug("Unset user context");
            ucw.unsetContext();
        }
    }
}
Also used : JobExecutionException(org.quartz.JobExecutionException) UserContextWrapper(org.jaffa.modules.user.services.UserContextWrapper) JobExecutionContext(org.quartz.JobExecutionContext) ScheduledTask(org.jaffa.modules.scheduler.services.ScheduledTask) JobExecutionException(org.quartz.JobExecutionException)

Example 20 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)

Aggregations

JobExecutionContext (org.quartz.JobExecutionContext)47 Test (org.junit.Test)20 JobDetail (org.quartz.JobDetail)20 JobDataMap (org.quartz.JobDataMap)13 Set (java.util.Set)10 JobExecutionException (org.quartz.JobExecutionException)10 List (java.util.List)8 SchedulerException (org.quartz.SchedulerException)8 VirtConsumerMap (org.candlepin.model.VirtConsumerMap)7 Job (org.quartz.Job)6 ArrayList (java.util.ArrayList)5 UnableToInterruptJobException (org.quartz.UnableToInterruptJobException)5 Consumer (org.candlepin.model.Consumer)4 InterruptableJob (org.quartz.InterruptableJob)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