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);
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations