use of org.kie.api.event.process.SLAViolatedEvent in project jbpm by kiegroup.
the class EscalateToAdminSLAViolationListener method afterSLAViolated.
@Override
public void afterSLAViolated(SLAViolatedEvent event) {
CaseFileInstance caseFile = getCaseFile((KieSession) event.getKieRuntime());
if (caseFile != null) {
String caseId = ((WorkflowProcessInstanceImpl) event.getProcessInstance()).getCorrelationKey();
if (caseFile.getCaseId().equals(caseId)) {
try {
Collection<OrganizationalEntity> adminAssignments = ((CaseAssignment) caseFile).getAssignments("admin");
String users = adminAssignments.stream().filter(oe -> oe instanceof User).map(oe -> oe.getId()).collect(Collectors.joining(","));
String groups = adminAssignments.stream().filter(oe -> oe instanceof Group).map(oe -> oe.getId()).collect(Collectors.joining(","));
String taskName = "SLA violation for case " + caseId;
String taskDescription = "Service Level Agreement has been violated for case " + caseId;
if (event.getNodeInstance() != null) {
taskName += "Task (" + event.getNodeInstance().getNodeName() + ") SLA violation for case " + caseId;
taskDescription += " on task " + event.getNodeInstance().getNodeName();
}
logger.debug("Case instance {} has SLA violation, escalating to administrator", caseId);
CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE);
TaskSpecification taskSpec = caseService.newHumanTaskSpec(taskName, taskDescription, users, groups, null);
caseService.addDynamicTask(caseId, taskSpec);
} catch (IllegalArgumentException e) {
logger.debug("There is no admin role defined in case instance {}, unable to escalate SLA violation", caseId);
}
}
}
}
use of org.kie.api.event.process.SLAViolatedEvent in project jbpm by kiegroup.
the class SLAComplianceTest method testSLAonProcessViolatedWithExpression.
@Test
public void testSLAonProcessViolatedWithExpression() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
final ProcessEventListener listener = new DefaultProcessEventListener() {
@Override
public void afterSLAViolated(SLAViolatedEvent event) {
latch.countDown();
}
};
KieBase kbase = createKnowledgeBase("BPMN2-UserTaskWithSLAExpr.bpmn2");
KieSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
ksession.addEventListener(listener);
Map<String, Object> parameters = new HashMap<>();
parameters.put("s", "3s");
ProcessInstance processInstance = ksession.startProcess("UserTask", parameters);
assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
WorkItem workItem = workItemHandler.getWorkItem();
assertNotNull(workItem);
assertEquals("john", workItem.getParameter("ActorId"));
boolean slaViolated = latch.await(10, TimeUnit.SECONDS);
assertTrue("SLA was not violated while it is expected", slaViolated);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
slaCompliance = getSLAComplianceForProcessInstance(processInstance);
assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
ksession.dispose();
}
use of org.kie.api.event.process.SLAViolatedEvent in project jbpm by kiegroup.
the class SLAComplianceTest method testSLAonUserTaskViolatedExternalTracking.
@Test
public void testSLAonUserTaskViolatedExternalTracking() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
final ProcessEventListener listener = new DefaultProcessEventListener() {
@Override
public void afterSLAViolated(SLAViolatedEvent event) {
latch.countDown();
}
};
KieBase kbase = createKnowledgeBase("BPMN2-UserTaskWithSLAOnTask.bpmn2");
KieSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
ksession.addEventListener(listener);
ksession.getEnvironment().set("SLATimerMode", "false");
ProcessInstance processInstance = ksession.startProcess("UserTask");
assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
WorkItem workItem = workItemHandler.getWorkItem();
assertNotNull(workItem);
assertEquals("john", workItem.getParameter("ActorId"));
boolean slaViolated = latch.await(5, TimeUnit.SECONDS);
assertFalse("SLA should not violated by timer", slaViolated);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
Collection<NodeInstance> active = ((WorkflowProcessInstance) processInstance).getNodeInstances();
assertEquals(1, active.size());
NodeInstance userTaskNode = active.iterator().next();
// simulate external tracking of sla
ksession.signalEvent("slaViolation:" + userTaskNode.getId(), null, processInstance.getId());
slaViolated = latch.await(10, TimeUnit.SECONDS);
assertTrue("SLA was not violated while it is expected", slaViolated);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
int slaCompliance = getSLAComplianceForProcessInstance(processInstance);
assertEquals(ProcessInstance.SLA_NA, slaCompliance);
slaCompliance = getSLAComplianceForNodeInstance(processInstance.getId(), (org.jbpm.workflow.instance.NodeInstance) userTaskNode);
assertEquals(ProcessInstance.SLA_VIOLATED, slaCompliance);
ksession.dispose();
}
Aggregations