Search in sources :

Example 6 with TimerJobInstance

use of org.drools.core.time.impl.TimerJobInstance in project jbpm by kiegroup.

the class EJBTimerScheduler method executeTimerJob.

@SuppressWarnings("unchecked")
@Timeout
public void executeTimerJob(Timer timer) {
    EjbTimerJob timerJob = (EjbTimerJob) timer.getInfo();
    logger.debug("About to execute timer for job {}", timerJob);
    TimerJobInstance timerJobInstance = timerJob.getTimerJobInstance();
    String timerServiceId = ((EjbGlobalJobHandle) timerJobInstance.getJobHandle()).getDeploymentId();
    // handle overdue timers as ejb timer service might start before all deployments are ready
    long time = 0;
    while (TimerServiceRegistry.getInstance().get(timerServiceId) == null) {
        logger.debug("waiting for timer service to be available, elapsed time {} ms", time);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        time += 500;
        if (time > OVERDUE_WAIT_TIME) {
            logger.debug("No timer service found after waiting {} ms", time);
            break;
        }
    }
    try {
        ((Callable<Void>) timerJobInstance).call();
    } catch (Exception e) {
        logger.warn("Execution of time failed due to {}", e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
Also used : TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) Callable(java.util.concurrent.Callable) NoSuchObjectLocalException(javax.ejb.NoSuchObjectLocalException) Timeout(javax.ejb.Timeout)

Example 7 with TimerJobInstance

use of org.drools.core.time.impl.TimerJobInstance in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeTimers.

private static ProtobufMessages.Timers writeTimers(Collection<TimerJobInstance> timers, MarshallerWriteContext outCtx) {
    if (!timers.isEmpty()) {
        List<TimerJobInstance> sortedTimers = new ArrayList<TimerJobInstance>(timers);
        Collections.sort(sortedTimers, new Comparator<TimerJobInstance>() {

            public int compare(TimerJobInstance o1, TimerJobInstance o2) {
                return (int) (o1.getJobHandle().getId() - o2.getJobHandle().getId());
            }
        });
        ProtobufMessages.Timers.Builder _timers = ProtobufMessages.Timers.newBuilder();
        for (TimerJobInstance timer : sortedTimers) {
            JobContext jctx = ((SelfRemovalJobContext) timer.getJobContext()).getJobContext();
            if (jctx instanceof ObjectTypeNode.ExpireJobContext && !((ObjectTypeNode.ExpireJobContext) jctx).getExpireAction().getFactHandle().isValid()) {
                continue;
            }
            TimersOutputMarshaller writer = outCtx.writersByClass.get(jctx.getClass());
            Timer _timer = writer.serialize(jctx, outCtx);
            if (_timer != null) {
                _timers.addTimer(_timer);
            }
        }
        return _timers.build();
    }
    return null;
}
Also used : TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) SelfRemovalJobContext(org.drools.core.time.SelfRemovalJobContext) Timer(org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer) JobContext(org.drools.core.time.JobContext) SelfRemovalJobContext(org.drools.core.time.SelfRemovalJobContext) Timers(org.drools.core.marshalling.impl.ProtobufMessages.Timers)

Example 8 with TimerJobInstance

use of org.drools.core.time.impl.TimerJobInstance in project drools by kiegroup.

the class ProtobufOutputMarshaller method writeTimers.

private static ProtobufMessages.Timers writeTimers(Collection<TimerJobInstance> timers, MarshallerWriteContext outCtx) {
    if (!timers.isEmpty()) {
        List<TimerJobInstance> sortedTimers = new ArrayList<TimerJobInstance>(timers);
        Collections.sort(sortedTimers, new Comparator<TimerJobInstance>() {

            public int compare(TimerJobInstance o1, TimerJobInstance o2) {
                return (int) (o1.getJobHandle().getId() - o2.getJobHandle().getId());
            }
        });
        ProtobufMessages.Timers.Builder _timers = ProtobufMessages.Timers.newBuilder();
        for (TimerJobInstance timer : sortedTimers) {
            JobContext jctx = timer.getJobContext();
            if (jctx instanceof SelfRemovalJobContext) {
                jctx = ((SelfRemovalJobContext) jctx).getJobContext();
            }
            if (jctx instanceof ObjectTypeNode.ExpireJobContext && !((ObjectTypeNode.ExpireJobContext) jctx).getExpireAction().getFactHandle().isValid()) {
                continue;
            }
            TimersOutputMarshaller writer = (TimersOutputMarshaller) outCtx.getWriterForClass(jctx.getClass());
            Timer _timer = writer.serialize(jctx, outCtx);
            if (_timer != null) {
                _timers.addTimer(_timer);
            }
        }
        return _timers.build();
    }
    return null;
}
Also used : TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) SelfRemovalJobContext(org.drools.core.time.SelfRemovalJobContext) Timer(org.drools.serialization.protobuf.ProtobufMessages.Timers.Timer) JobContext(org.drools.core.time.JobContext) SelfRemovalJobContext(org.drools.core.time.SelfRemovalJobContext) Timers(org.drools.serialization.protobuf.ProtobufMessages.Timers)

Example 9 with TimerJobInstance

use of org.drools.core.time.impl.TimerJobInstance in project jbpm by kiegroup.

the class QuartzSchedulerService method scheduleJob.

@Override
public JobHandle scheduleJob(Job job, JobContext ctx, Trigger trigger) {
    Long id = idCounter.getAndIncrement();
    String jobname = null;
    String groupName = "jbpm";
    if (ctx instanceof ProcessJobContext) {
        ProcessJobContext processCtx = (ProcessJobContext) ctx;
        jobname = processCtx.getSessionId() + "-" + processCtx.getProcessInstanceId() + "-" + processCtx.getTimer().getId();
        if (processCtx instanceof StartProcessJobContext) {
            jobname = "StartProcess-" + ((StartProcessJobContext) processCtx).getProcessId() + "-" + processCtx.getTimer().getId();
        }
        String deploymentId = (String) processCtx.getKnowledgeRuntime().getEnvironment().get(EnvironmentName.DEPLOYMENT_ID);
        if (deploymentId != null) {
            groupName = deploymentId;
        }
    } else if (ctx instanceof NamedJobContext) {
        jobname = ((NamedJobContext) ctx).getJobName();
        String deploymentId = ((NamedJobContext) ctx).getDeploymentId();
        if (deploymentId != null) {
            groupName = deploymentId;
        }
    } else {
        jobname = "Timer-" + ctx.getClass().getSimpleName() + "-" + id;
    }
    logger.debug("Scheduling timer with name " + jobname);
    // check if this scheduler already has such job registered if so there is no need to schedule it again
    try {
        JobDetail jobDetail = scheduler.getJobDetail(jobKey(jobname, groupName));
        if (jobDetail != null) {
            TimerJobInstance timerJobInstance = (TimerJobInstance) jobDetail.getJobDataMap().get("timerJobInstance");
            return timerJobInstance.getJobHandle();
        }
    } catch (SchedulerException e) {
    }
    GlobalQuartzJobHandle quartzJobHandle = new GlobalQuartzJobHandle(id, jobname, groupName);
    TimerJobInstance jobInstance = globalTimerService.getTimerJobFactoryManager().createTimerJobInstance(job, ctx, trigger, quartzJobHandle, (InternalSchedulerService) globalTimerService);
    quartzJobHandle.setTimerJobInstance((TimerJobInstance) jobInstance);
    interceptor.internalSchedule(jobInstance);
    return quartzJobHandle;
}
Also used : StartProcessJobContext(org.jbpm.process.instance.timer.TimerManager.StartProcessJobContext) JobDetail(org.quartz.JobDetail) TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) SchedulerException(org.quartz.SchedulerException) ProcessJobContext(org.jbpm.process.instance.timer.TimerManager.ProcessJobContext) StartProcessJobContext(org.jbpm.process.instance.timer.TimerManager.StartProcessJobContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) NamedJobContext(org.jbpm.process.core.timer.NamedJobContext)

Example 10 with TimerJobInstance

use of org.drools.core.time.impl.TimerJobInstance in project jbpm by kiegroup.

the class GlobalTimerServiceBaseTest method testInterediateTimerWithHTBeforeWithGlobalTestServiceRollback.

@Test(timeout = 20000)
public void testInterediateTimerWithHTBeforeWithGlobalTestServiceRollback() throws Exception {
    // prepare listener to assert results
    Properties properties = new Properties();
    properties.setProperty("mary", "HR");
    properties.setProperty("john", "HR");
    UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl(properties);
    environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycleWithHT2.bpmn2"), ResourceType.BPMN2).schedulerService(globalScheduler).userGroupCallback(userGroupCallback).get();
    manager = getManager(environment, true);
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    long ksessionId = ksession.getIdentifier();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "R3/PT1S");
    ProcessInstance processInstance = ksession.startProcess("IntermediateCatchEvent", params);
    assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
    // get tasks
    List<Status> statuses = new ArrayList<Status>();
    statuses.add(Status.Reserved);
    List<TaskSummary> tasks = runtime.getTaskService().getTasksAssignedAsPotentialOwnerByStatus("john", statuses, "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    TaskSummary task = tasks.get(0);
    runtime.getTaskService().start(task.getId(), "john");
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    try {
        ut.begin();
        runtime.getTaskService().complete(task.getId(), "john", null);
    } finally {
        ut.rollback();
    }
    processInstance = ksession.getProcessInstance(processInstance.getId());
    Collection<NodeInstance> activeNodes = ((WorkflowProcessInstance) processInstance).getNodeInstances();
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    assertTrue(activeNodes.iterator().next() instanceof HumanTaskNodeInstance);
    TimerService timerService = TimerServiceRegistry.getInstance().get(manager.getIdentifier() + TimerServiceRegistry.TIMER_SERVICE_SUFFIX);
    Collection<TimerJobInstance> timerInstances = timerService.getTimerJobInstances(ksessionId);
    assertNotNull(timerInstances);
    assertEquals(0, timerInstances.size());
    // clean up
    ksession.abortProcessInstance(processInstance.getId());
    manager.disposeRuntimeEngine(runtime);
}
Also used : Status(org.kie.api.task.model.Status) UserTransaction(javax.transaction.UserTransaction) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) TimerJobInstance(org.drools.core.time.impl.TimerJobInstance) HashMap(java.util.HashMap) JBossUserGroupCallbackImpl(org.jbpm.services.task.identity.JBossUserGroupCallbackImpl) ArrayList(java.util.ArrayList) Properties(java.util.Properties) UserGroupCallback(org.kie.api.task.UserGroupCallback) TimerService(org.drools.core.time.TimerService) HumanTaskNodeInstance(org.jbpm.workflow.instance.node.HumanTaskNodeInstance) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) HumanTaskNodeInstance(org.jbpm.workflow.instance.node.HumanTaskNodeInstance) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) Test(org.junit.Test)

Aggregations

TimerJobInstance (org.drools.core.time.impl.TimerJobInstance)15 SelfRemovalJobContext (org.drools.core.time.SelfRemovalJobContext)5 ProcessJobContext (org.jbpm.process.instance.timer.TimerManager.ProcessJobContext)5 ArrayList (java.util.ArrayList)4 JobContext (org.drools.core.time.JobContext)4 TimerService (org.drools.core.time.TimerService)4 Test (org.junit.Test)4 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)4 UserTransaction (javax.transaction.UserTransaction)3 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)3 KieSession (org.kie.api.runtime.KieSession)3 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)3 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 Callable (java.util.concurrent.Callable)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 NoSuchObjectLocalException (javax.ejb.NoSuchObjectLocalException)2 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)2 StartProcessJobContext (org.jbpm.process.instance.timer.TimerManager.StartProcessJobContext)2