Search in sources :

Example 21 with JobExecutionContext

use of org.quartz.JobExecutionContext in project weicoder by wdcode.

the class ExecutingJobsManager method interrupt.

/**
 * Interrupt all instances of the identified InterruptableJob executing in
 * this Scheduler instance.
 *
 * <p>
 * This method is not cluster aware.  That is, it will only interrupt
 * instances of the identified InterruptableJob currently executing in this
 * Scheduler instance, not across the entire cluster.
 * </p>
 *
 * @see org.quartz.core.RemotableQuartzScheduler#interrupt(JobKey)
 */
public boolean interrupt(JobKey jobKey) throws UnableToInterruptJobException {
    List<JobExecutionContext> jobs = getCurrentlyExecutingJobs();
    JobDetail jobDetail = null;
    Job job = null;
    boolean interrupted = false;
    for (JobExecutionContext jec : jobs) {
        jobDetail = jec.getJobDetail();
        if (jobKey.equals(jobDetail.getKey())) {
            job = jec.getJobInstance();
            if (job instanceof InterruptableJob) {
                ((InterruptableJob) job).interrupt();
                interrupted = true;
            } else {
                throw new UnableToInterruptJobException("Job " + jobDetail.getKey() + " can not be interrupted, since it does not implement " + InterruptableJob.class.getName());
            }
        }
    }
    return interrupted;
}
Also used : UnableToInterruptJobException(org.quartz.UnableToInterruptJobException) JobDetail(org.quartz.JobDetail) InterruptableJob(org.quartz.InterruptableJob) JobExecutionContext(org.quartz.JobExecutionContext) Job(org.quartz.Job) InterruptableJob(org.quartz.InterruptableJob)

Example 22 with JobExecutionContext

use of org.quartz.JobExecutionContext in project midpoint by Evolveum.

the class TestQuartzTaskManagerContract method test105LightweightSubtasksSuspension.

@Test(enabled = true)
public void test105LightweightSubtasksSuspension() throws Exception {
    final String test = "105LightweightSubtasksSuspension";
    final OperationResult result = createResult(test);
    addObjectFromFile(taskFilename(test));
    Task task = taskManager.getTask(taskOid(test), result);
    System.out.println("After setup: " + task.debugDump());
    waitFor("Waiting for task manager to start the task", new Checker() {

        public boolean check() throws ObjectNotFoundException, SchemaException {
            Task task = taskManager.getTask(taskOid(test), result);
            IntegrationTestTools.display("Task while waiting for task manager to execute the task", task);
            return task.getLastRunStartTimestamp() != null && task.getLastRunStartTimestamp() != 0L;
        }

        @Override
        public void timeout() {
        }
    }, 15000, 500);
    task.refresh(result);
    System.out.println("After refresh (task was started; and it should run now): " + task.debugDump());
    AssertJUnit.assertEquals(TaskExecutionStatus.RUNNABLE, task.getExecutionStatus());
    // check the thread
    List<JobExecutionContext> jobExecutionContexts = taskManager.getExecutionManager().getQuartzScheduler().getCurrentlyExecutingJobs();
    JobExecutionContext found = null;
    for (JobExecutionContext jobExecutionContext : jobExecutionContexts) {
        if (task.getOid().equals(jobExecutionContext.getJobDetail().getKey().getName())) {
            found = jobExecutionContext;
            break;
        }
    }
    assertNotNull("Job for the task was not found", found);
    JobExecutor executor = (JobExecutor) found.getJobInstance();
    assertNotNull("No job executor", executor);
    Thread thread = executor.getExecutingThread();
    assertNotNull("No executing thread", thread);
    // now let us suspend it - the handler should stop, as well as the subtasks
    boolean stopped = taskManager.suspendTask(task, 10000L, result);
    task.refresh(result);
    AssertJUnit.assertTrue("Task is not stopped", stopped);
    AssertJUnit.assertEquals("Task is not suspended", TaskExecutionStatus.SUSPENDED, task.getExecutionStatus());
    Collection<? extends Task> subtasks = parallelTaskHandler.getLastTaskExecuted().getLightweightAsynchronousSubtasks();
    assertEquals("Wrong number of subtasks", MockParallelTaskHandler.NUM_SUBTASKS, subtasks.size());
    for (Task subtask : subtasks) {
        assertEquals("Wrong subtask state", TaskExecutionStatus.CLOSED, subtask.getExecutionStatus());
        MockParallelTaskHandler.MyLightweightTaskHandler handler = (MockParallelTaskHandler.MyLightweightTaskHandler) subtask.getLightweightTaskHandler();
        assertTrue("Handler has not run", handler.hasRun());
        assertTrue("Handler has not exited", handler.hasExited());
    }
}
Also used : Checker(com.evolveum.midpoint.test.Checker) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) JobExecutor(com.evolveum.midpoint.task.quartzimpl.execution.JobExecutor) JobExecutionContext(org.quartz.JobExecutionContext) Test(org.testng.annotations.Test)

Example 23 with JobExecutionContext

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

the class CommandJobTest method getJobExecutionContext.

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

Example 24 with JobExecutionContext

use of org.quartz.JobExecutionContext in project OpenOLAT by OpenOLAT.

the class NotificationsAdminWebService method getJobStatus.

private String getJobStatus() {
    try {
        Scheduler scheduler = CoreSpringFactory.getImpl(Scheduler.class);
        List<JobExecutionContext> jobs = scheduler.getCurrentlyExecutingJobs();
        for (JobExecutionContext job : jobs) {
            if ("org.olat.notifications.job.enabled".equals(job.getJobDetail().getKey().getName())) {
                return "running";
            }
        }
        return "stopped";
    } catch (SchedulerException e) {
        log.error("", e);
        return "error";
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) Scheduler(org.quartz.Scheduler) JobExecutionContext(org.quartz.JobExecutionContext)

Example 25 with JobExecutionContext

use of org.quartz.JobExecutionContext in project syncope by apache.

the class SetUMembershipsJob method execute.

@Override
public void execute(final JobExecutionContext context) throws JobExecutionException {
    try {
        AuthContextUtils.execWithAuthContext(context.getMergedJobDataMap().getString(JobManager.DOMAIN_KEY), () -> {
            @SuppressWarnings("unchecked") Map<String, Set<String>> memberships = (Map<String, Set<String>>) context.getMergedJobDataMap().get(MEMBERSHIPS_KEY);
            LOG.debug("About to set memberships (User -> Groups) {}", memberships);
            memberships.entrySet().stream().map(membership -> {
                UserPatch userPatch = new UserPatch();
                userPatch.setKey(membership.getKey());
                membership.getValue().forEach(groupKey -> {
                    userPatch.getMemberships().add(new MembershipPatch.Builder().operation(PatchOperation.ADD_REPLACE).group(groupKey).build());
                });
                return userPatch;
            }).filter(userPatch -> (!userPatch.isEmpty())).map((userPatch) -> {
                LOG.debug("About to update User {}", userPatch.getKey());
                return userPatch;
            }).forEachOrdered((userPatch) -> {
                userProvisioningManager.update(userPatch, true);
            });
            return null;
        });
    } catch (RuntimeException e) {
        LOG.error("While setting memberships", e);
        throw new JobExecutionException("While executing memberships", e);
    }
}
Also used : JobExecutionContext(org.quartz.JobExecutionContext) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Autowired(org.springframework.beans.factory.annotation.Autowired) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) JobManager(org.apache.syncope.core.provisioning.api.job.JobManager) JobExecutionException(org.quartz.JobExecutionException) MembershipPatch(org.apache.syncope.common.lib.patch.MembershipPatch) UserProvisioningManager(org.apache.syncope.core.provisioning.api.UserProvisioningManager) PatchOperation(org.apache.syncope.common.lib.types.PatchOperation) Map(java.util.Map) AuthContextUtils(org.apache.syncope.core.spring.security.AuthContextUtils) MembershipPatch(org.apache.syncope.common.lib.patch.MembershipPatch) Set(java.util.Set) JobExecutionException(org.quartz.JobExecutionException) Map(java.util.Map) UserPatch(org.apache.syncope.common.lib.patch.UserPatch)

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