use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.
the class WorkflowProcessInstanceImpl method configureSLA.
@Override
public void configureSLA() {
String slaDueDateExpression = (String) getProcess().getMetaData().get(CUSTOM_SLA_DUE_DATE);
if (slaDueDateExpression != null) {
TimerInstance timer = configureSLATimer(slaDueDateExpression);
if (timer != null) {
this.slaTimerId = timer.getId();
this.slaDueDate = new Date(System.currentTimeMillis() + timer.getDelay());
this.slaCompliance = KogitoProcessInstance.SLA_PENDING;
logger.debug("SLA for process instance {} is PENDING with due date {}", this.getStringId(), this.slaDueDate);
}
}
}
use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.
the class EventNodeInstance method configureSla.
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.getStringId(), this.slaDueDate);
}
}
}
use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.
the class StateBasedNodeInstance method configureSla.
@Override
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 = KogitoProcessInstance.SLA_PENDING;
logger.debug("SLA for node instance {} is PENDING with due date {}", this.getStringId(), this.slaDueDate);
addTimerListener();
}
}
}
use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.
the class TimerTest method testTimer.
@Test
@Disabled
public void testTimer() {
KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance() {
private static final long serialVersionUID = 510l;
public void signalEvent(String type, Object event) {
if ("timerTriggered".equals(type)) {
TimerInstance timer = (TimerInstance) event;
logger.info("Timer {} triggered", timer.getId());
counter++;
}
}
};
processInstance.setKnowledgeRuntime(((InternalWorkingMemory) kruntime.getKieSession()).getKnowledgeRuntime());
processInstance.setId("1234");
InternalProcessRuntime processRuntime = ((InternalProcessRuntime) ((InternalWorkingMemory) kruntime.getKieSession()).getProcessRuntime());
processRuntime.getProcessInstanceManager().internalAddProcessInstance(processInstance);
new Thread(() -> kruntime.getKieSession().fireUntilHalt()).start();
JobsService jobService = new LegacyInMemoryJobService(kruntime, new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
ProcessInstanceJobDescription desc = ProcessInstanceJobDescription.of(ExactExpirationTime.now(), processInstance.getStringId(), "test");
String jobId = jobService.scheduleProcessInstanceJob(desc);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
assertEquals(1, counter);
counter = 0;
desc = ProcessInstanceJobDescription.of(DurationExpirationTime.after(500), processInstance.getStringId(), "test");
jobId = jobService.scheduleProcessInstanceJob(desc);
assertEquals(0, counter);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
assertEquals(1, counter);
counter = 0;
desc = ProcessInstanceJobDescription.of(DurationExpirationTime.repeat(500, 300L), processInstance.getStringId(), "test");
jobId = jobService.scheduleProcessInstanceJob(desc);
assertEquals(0, counter);
try {
Thread.sleep(700);
} catch (InterruptedException e) {
// do nothing
}
assertEquals(1, counter);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
// we can't know exactly how many times this will fire as timers are not precise, but should be at least 4
assertTrue(counter >= 4);
jobService.cancelJob(jobId);
int lastCount = counter;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// do nothing
}
assertEquals(lastCount, counter);
}
use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.
the class WorkflowProcessInstanceImpl method signalEvent.
@Override
@SuppressWarnings("unchecked")
public void signalEvent(String type, Object event) {
logger.debug("Signal {} received with data {} in process instance {}", type, event, getStringId());
synchronized (this) {
if (getState() != KogitoProcessInstance.STATE_ACTIVE) {
return;
}
if ("timerTriggered".equals(type)) {
TimerInstance timer = (TimerInstance) event;
if (timer.getId().equals(slaTimerId)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
}
if ("slaViolation".equals(type)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
List<NodeInstance> currentView = new ArrayList<>(this.nodeInstances);
try {
this.activatingNodeIds = new ArrayList<>();
List<KogitoEventListener> listeners = eventListeners.get(type);
if (listeners != null) {
for (KogitoEventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
listeners = externalEventListeners.get(type);
if (listeners != null) {
for (KogitoEventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
for (org.kie.api.definition.process.Node node : getWorkflowProcess().getNodes()) {
if (node instanceof EventNodeInterface && ((EventNodeInterface) node).acceptsEvent(type, event, getResolver(node, currentView))) {
if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
EventNodeInstance eventNodeInstance = (EventNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event, getResolver(node, currentView));
} else {
if (node instanceof EventSubProcessNode && (resolveVariables(((EventSubProcessNode) node).getEvents()).contains(type))) {
EventSubProcessNodeInstance eventNodeInstance = (EventSubProcessNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView);
if (nodeInstances != null && !nodeInstances.isEmpty()) {
for (NodeInstance nodeInstance : nodeInstances) {
((EventNodeInstanceInterface) nodeInstance).signalEvent(type, event, getResolver(node, currentView));
}
}
}
}
}
}
if (((org.jbpm.workflow.core.WorkflowProcess) getWorkflowProcess()).isDynamic()) {
for (org.kie.api.definition.process.Node node : getWorkflowProcess().getNodes()) {
if (type.equals(node.getName()) && node.getIncomingConnections().isEmpty()) {
NodeInstance nodeInstance = getNodeInstance(node);
if (event != null) {
Map<String, Object> dynamicParams = new HashMap<>(getVariables());
if (event instanceof Map) {
dynamicParams.putAll((Map<String, Object>) event);
} else {
dynamicParams.put("Data", event);
}
nodeInstance.setDynamicParameters(dynamicParams);
}
nodeInstance.trigger(null, Node.CONNECTION_DEFAULT_TYPE);
} else if (node instanceof CompositeNode) {
Optional<NodeInstance> instance = this.nodeInstances.stream().filter(ni -> ni.getNodeId() == node.getId()).findFirst();
instance.ifPresent(n -> ((CompositeNodeInstance) n).signalEvent(type, event));
}
}
}
} finally {
if (this.activatingNodeIds != null) {
this.activatingNodeIds.clear();
this.activatingNodeIds = null;
}
}
}
}
Aggregations