public void signalEvent(String type, Object event) { if ("timerTriggered".equals(type)) { TimerInstance timer = (TimerInstance) event; if (timer.getId() == timerId) { triggerCompleted(timer.getPeriod() == 0); } } }
public void signalEvent(String type, Object event) { if ("timerTriggered".equals(type)) { TimerInstance timer = (TimerInstance) event; logger.info("Timer {} triggered", timer.getId()); counter++; } } };
public void configureSLA() { String slaDueDateExpression = (String) getProcess().getMetaData().get("customSLADueDate"); if (slaDueDateExpression != null) { TimerInstance timer = configureSLATimer(slaDueDateExpression); if (timer != null) { this.slaTimerId = timer.getId(); this.slaDueDate = new Date(System.currentTimeMillis() + timer.getDelay()); this.slaCompliance = SLA_PENDING; logger.debug("SLA for process instance {} is PENDING with due date {}", this.getId(), this.slaDueDate); } } }
protected void configureSla() { String slaDueDateExpression = (String) getNode().getMetaData().get("customSLADueDate"); if (slaDueDateExpression != null) { TimerInstance timer = ((WorkflowProcessInstanceImpl)getProcessInstance()).configureSLATimer(slaDueDateExpression); if (timer != null) { this.slaTimerId = timer.getId(); this.slaDueDate = new Date(System.currentTimeMillis() + timer.getDelay()); this.slaCompliance = ProcessInstance.SLA_PENDING; logger.debug("SLA for node instance {} is PENDING with due date {}", this.getId(), this.slaDueDate); } } }
protected void configureSla() { String slaDueDateExpression = (String) getNode().getMetaData().get("customSLADueDate"); if (slaDueDateExpression != null) { TimerInstance timer = ((WorkflowProcessInstanceImpl)getProcessInstance()).configureSLATimer(slaDueDateExpression); if (timer != null) { this.slaTimerId = timer.getId(); this.slaDueDate = new Date(System.currentTimeMillis() + timer.getDelay()); this.slaCompliance = ProcessInstance.SLA_PENDING; logger.debug("SLA for node instance {} is PENDING with due date {}", this.getId(), this.slaDueDate); addTimerListener(); } } }
public long getTimerId() { JobContext ctx = this.getTimerJobInstance().getJobContext(); if (ctx instanceof SelfRemovalJobContext) { ctx = ((SelfRemovalJobContext) ctx).getJobContext(); } return ((ProcessJobContext)ctx).getTimer().getId(); }
@Override public JobHandle scheduleJob(Job job, JobContext ctx, Trigger trigger) { if (ctx instanceof ProcessJobContext) { ProcessJobContext processCtx = (ProcessJobContext) ctx; List<GlobalJobHandle> jobHandles = timerJobsPerSession.get(processCtx.getSessionId()); if (jobHandles == null) { jobHandles = new CopyOnWriteArrayList<GlobalJobHandle>(); timerJobsPerSession.put(processCtx.getSessionId(), jobHandles); } else { // check if the given job is already scheduled for (GlobalJobHandle handle : jobHandles) { long timerId = handle.getTimerId(); if (timerId == processCtx.getTimer().getId()) { // this timer job is already registered return handle; } } } GlobalJobHandle jobHandle = (GlobalJobHandle) this.schedulerService.scheduleJob(job, ctx, trigger); if (jobHandle != null) { jobHandles.add(jobHandle); } return jobHandle; } GlobalJobHandle jobHandle = (GlobalJobHandle) this.schedulerService.scheduleJob(job, ctx, trigger); return jobHandle; }
@Override public Map<Long, List<TimerInstance>> execute(Context context) { Map<Long, List<TimerInstance>> result = new LinkedHashMap<>(); KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class); TimerManager timerManager = getTimerManager(kieSession); WorkflowProcessInstanceImpl processInstance = (WorkflowProcessInstanceImpl) kieSession.getProcessInstance(migrationSpec.getProcessInstanceId()); Collection<org.jbpm.workflow.instance.NodeInstance> activeInstances = processInstance.getNodeInstances(true); for (org.jbpm.workflow.instance.NodeInstance active : activeInstances) { if (active instanceof TimerNodeInstance) { TimerInstance timerInstance = timerManager.getTimerMap().get(((TimerNodeInstance) active).getTimerId()); timerManager.cancelTimer(timerInstance.getId()); result.put(active.getId(), Arrays.asList(timerInstance)); } else if (active instanceof StateBasedNodeInstance) { List<Long> timers = ((StateBasedNodeInstance) active).getTimerInstances(); if (timers != null && !timers.isEmpty()) { List<TimerInstance> collected = new ArrayList<>(); for (Long timerId : timers) { TimerInstance timerInstance = timerManager.getTimerMap().get(timerId); timerManager.cancelTimer(timerInstance.getId()); collected.add(timerInstance); } result.put(active.getId(), collected); } } } return result; } });
@Override public void signalEvent(String type, Object event) { if ("timerTriggered".equals(type)) { TimerInstance timerInstance = (TimerInstance) event; if (timerInstances != null && timerInstances.contains(timerInstance.getId())) { triggerTimer(timerInstance); } else if (timerInstance.getId() == slaTimerId) { handleSLAViolation(); } } else if (("slaViolation:" + getId()).equals(type)) { handleSLAViolation(); } else if (type.equals(getActivationType())) { if (event instanceof MatchCreatedEvent) { String name = ((MatchCreatedEvent)event).getMatch().getRule().getName(); if (checkProcessInstance((Activation) ((MatchCreatedEvent)event).getMatch())) { ((MatchCreatedEvent)event).getKieRuntime().signalEvent(name, null); } } } }
public void internalAddTimer(final TimerInstance timer) { long delay; Date lastTriggered = timer.getLastTriggered(); if (lastTriggered == null) { Date activated = timer.getActivated(); Date now = new Date(); long timespan = now.getTime() - activated.getTime(); delay = timer.getDelay() - timespan; if (delay < 0) { delay = 0; } } else { Date now = new Date(); long timespan = now.getTime() - lastTriggered.getTime(); delay = timespan - timer.getPeriod(); if (delay < 0) { delay = 0; } } Trigger trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, -1, delay, timer.getPeriod(), null, null); ProcessJobContext ctx = new ProcessJobContext(timer, trigger, timer.getProcessInstanceId(), this.kruntime); JobHandle jobHandle = this.timerService.scheduleJob(processJob, ctx, trigger); timer.setJobHandle(jobHandle); timers.put(timer.getId(), timer); }
public void internalTrigger(NodeInstance from, String type) { if (!org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) { throw new IllegalArgumentException( "A TimerNode only accepts default incoming connections!"); } InternalKnowledgeRuntime kruntime = getProcessInstance().getKnowledgeRuntime(); timerInstance = createTimerInstance(kruntime); if (getTimerInstances() == null) { addTimerListener(); } ((InternalProcessRuntime)kruntime.getProcessRuntime()) .getTimerManager().registerTimer(timerInstance, (ProcessInstance) getProcessInstance()); timerId = timerInstance.getId(); }
public void clearProcessInstancesState() { try { // at this point only timers are considered as state that needs to be cleared TimerManager timerManager = ((InternalProcessRuntime)kruntime.getProcessRuntime()).getTimerManager(); for (ProcessInstance processInstance: new ArrayList<ProcessInstance>(processInstances.values())) { WorkflowProcessInstance pi = ((WorkflowProcessInstance) processInstance); for (org.kie.api.runtime.process.NodeInstance nodeInstance : pi.getNodeInstances()) { if (nodeInstance instanceof TimerNodeInstance){ if (((TimerNodeInstance)nodeInstance).getTimerInstance() != null) { timerManager.cancelTimer(((TimerNodeInstance)nodeInstance).getTimerInstance().getId()); } } else if (nodeInstance instanceof StateBasedNodeInstance) { List<Long> timerIds = ((StateBasedNodeInstance) nodeInstance).getTimerInstances(); if (timerIds != null) { for (Long id: timerIds) { timerManager.cancelTimer(id); } } } } } } catch (Exception e) { // catch everything here to make sure it will not break any following // logic to allow complete clean up } }
public void signalEvent(String type, Object event) { if ("timerTriggered".equals(type)) { TimerInstance timerInstance = (TimerInstance) event; if (timerInstance.getId() == slaTimerId) { handleSLAViolation(); } } else if (("slaViolation:" + getId()).equals(type)) { handleSLAViolation(); } else { String variableName = getEventNode().getVariableName(); if (variableName != null) { VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, variableName); if (variableScopeInstance == null) { throw new IllegalArgumentException( "Could not find variable for event node: " + variableName); } EventTransformer transformer = getEventNode().getEventTransformer(); if (transformer != null) { event = transformer.transformEvent(event); } variableScopeInstance.setVariable(variableName, event); } triggerCompleted(); } }
@Override public boolean removeJob(JobHandle jobHandle) { if (jobHandle == null) { return false; } jobHandle.setCancel( true ); JobContext jobContext = ((GlobalJDKJobHandle) jobHandle).getTimerJobInstance().getJobContext(); try { ProcessJobContext processCtx = null; if (jobContext instanceof SelfRemovalJobContext) { processCtx = (ProcessJobContext) ((SelfRemovalJobContext) jobContext).getJobContext(); } else { processCtx = (ProcessJobContext) jobContext; } String jobname = processCtx.getSessionId() + "-" + processCtx.getProcessInstanceId() + "-" + processCtx.getTimer().getId(); if (processCtx instanceof StartProcessJobContext) { jobname = "StartProcess-"+((StartProcessJobContext) processCtx).getProcessId()+ "-" + processCtx.getTimer().getId(); } activeTimer.remove(jobname); globalTimerService.getTimerJobFactoryManager().removeTimerJobInstance( ((GlobalJDKJobHandle) jobHandle).getTimerJobInstance() ); } catch (ClassCastException e) { // do nothing in case ProcessJobContext was not given } boolean removed = this.scheduler.remove( (Runnable) ((GlobalJDKJobHandle) jobHandle).getFuture() ); return removed; }
public void deserialize(MarshallerReaderContext inCtx, Timer timer) throws ClassNotFoundException { JBPMMessages.ProcessTimer ptimer = timer.getExtension(JBPMMessages.procTimer); TimerService ts = inCtx.wm.getTimerService(); long processInstanceId = ptimer.getTimer().getProcessInstanceId(); Trigger trigger = ProtobufInputMarshaller.readTrigger(inCtx, ptimer.getTrigger()); TimerInstance timerInstance = ProtobufProcessMarshaller.readTimer(inCtx, ptimer.getTimer()); TimerManager tm = ((InternalProcessRuntime) inCtx.wm.getProcessRuntime()).getTimerManager(); // check if the timer instance is not already registered to avoid duplicated timers if (!tm.getTimerMap().containsKey(timerInstance.getId())) { ProcessJobContext pctx = new ProcessJobContext(timerInstance, trigger, processInstanceId, inCtx.wm.getKnowledgeRuntime(), false); Date date = trigger.hasNextFireTime(); if (date != null) { long then = date.getTime(); long now = pctx.getKnowledgeRuntime().getSessionClock().getCurrentTime(); // overdue timer if (then < now) { trigger = new OverdueTrigger(trigger, pctx.getKnowledgeRuntime()); } } JobHandle jobHandle = ts.scheduleJob(processJob, pctx, trigger); timerInstance.setJobHandle(jobHandle); pctx.setJobHandle(jobHandle); tm.getTimerMap().put(timerInstance.getId(), timerInstance); } } }
public static JBPMMessages.ProcessTimer.TimerInstance writeTimer(MarshallerWriteContext context, TimerInstance timer) { Builder _timer = JBPMMessages.ProcessTimer.TimerInstance.newBuilder() .setId( timer.getId() ) .setTimerId( timer.getTimerId() ) .setSessionId( timer.getSessionId() ) .setDelay( timer.getDelay() ) .setPeriod( timer.getPeriod() ) .setProcessInstanceId( timer.getProcessInstanceId() ) .setActivatedTime( timer.getActivated().getTime() ) .setRepeatLimit(timer.getRepeatLimit()); Date lastTriggered = timer.getLastTriggered(); if ( lastTriggered != null ) { _timer.setLastTriggered( lastTriggered.getTime() ); } return _timer.build(); }
public void registerTimer(final TimerInstance timer, String processId, Map<String, Object> params) { try { kruntime.startOperation(); timer.setId(++timerId); timer.setProcessInstanceId(-1l); timer.setSessionId(((StatefulKnowledgeSession) kruntime).getIdentifier()); timer.setActivated(new Date()); Trigger trigger = null; if (timer.getCronExpression() != null) { Date startTime = new Date(timerService.getCurrentTime() + 1000); trigger = new CronTrigger(timerService.getCurrentTime(), startTime, null, -1, timer.getCronExpression(), null, null); // cron timers are by nature repeatable timer.setPeriod(1); } else { trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, timer.getRepeatLimit(), timer.getDelay(), timer.getPeriod(), null, null); } StartProcessJobContext ctx = new StartProcessJobContext(timer, trigger, processId, params, this.kruntime); JobHandle jobHandle = this.timerService.scheduleJob(startProcessJob, ctx, trigger); timer.setJobHandle(jobHandle); timers.put(timer.getId(), timer); } finally { kruntime.endOperation(); } }
public void registerTimer(final TimerInstance timer, ProcessInstance processInstance) { try { kruntime.startOperation(); timer.setId(++timerId); timer.setProcessInstanceId(processInstance.getId()); timer.setSessionId(((KieSession) kruntime).getIdentifier()); timer.setActivated(new Date()); Trigger trigger = null; if (timer.getCronExpression() != null) { Date startTime = new Date(timerService.getCurrentTime() + 1000); trigger = new CronTrigger(timerService.getCurrentTime(), startTime, null, -1, timer.getCronExpression(), null, null); // cron timers are by nature repeatable timer.setPeriod(1); } else { trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, timer.getRepeatLimit(), timer.getDelay(), timer.getPeriod(), null, null); } ProcessJobContext ctx = new ProcessJobContext(timer, trigger, processInstance.getId(), this.kruntime); JobHandle jobHandle = this.timerService.scheduleJob(processJob, ctx, trigger); timer.setJobHandle(jobHandle); timers.put(timer.getId(), timer); } finally { kruntime.endOperation(); } }
TimerInstance timerInstance = createTimerInstance(timer); timerManager.registerTimer(timerInstance, (ProcessInstance) getProcessInstance()); timerInstances.add(timerInstance.getId());