use of org.jbpm.process.audit.NodeInstanceLog in project jbpm by kiegroup.
the class AuditCommandsTest method testVarAndNodeInstanceCommands.
@Test
public void testVarAndNodeInstanceCommands() throws Exception {
KieBase kbase = createKnowledgeBase("BPMN2-SubProcessUserTask.bpmn2");
KieSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
ProcessInstance processInstance = ksession.startProcess("SubProcess");
assertProcessInstanceActive(processInstance);
Command<?> cmd = new FindNodeInstancesCommand(processInstance.getId());
Object result = ksession.execute(cmd);
assertNotNull("Command result is empty!", result);
assertTrue(result instanceof List);
List<NodeInstanceLog> nodeLogList = (List<NodeInstanceLog>) result;
assertEquals("Log list size is incorrect.", 8, nodeLogList.size());
cmd = new FindNodeInstancesCommand(processInstance.getId(), "UserTask_1");
result = ksession.execute(cmd);
assertNotNull("Command result is empty!", result);
assertTrue(result instanceof List);
nodeLogList = (List<NodeInstanceLog>) result;
assertEquals("Log list size is incorrect.", 1, nodeLogList.size());
cmd = new FindVariableInstancesCommand(processInstance.getId(), "2:x");
result = ksession.execute(cmd);
assertNotNull("Command result is empty!", result);
assertTrue(result instanceof List);
List<VariableInstanceLog> varLogList = (List<VariableInstanceLog>) result;
assertEquals("Log list size is incorrect.", 1, varLogList.size());
WorkItem workItem = workItemHandler.getWorkItem();
assertNotNull(workItem);
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
}
use of org.jbpm.process.audit.NodeInstanceLog in project jbpm by kiegroup.
the class SLATrackingCommand method execute.
@Override
public ExecutionResults execute(CommandContext ctx) throws Exception {
ExecutionResults executionResults = new ExecutionResults();
String emfName = (String) ctx.getData("EmfName");
if (emfName == null) {
emfName = "org.jbpm.domain";
}
String singleRun = (String) ctx.getData("SingleRun");
if ("true".equalsIgnoreCase(singleRun)) {
// disable rescheduling
this.nextScheduleTimeAdd = -1;
}
String nextRun = (String) ctx.getData("NextRun");
if (nextRun != null) {
nextScheduleTimeAdd = DateTimeUtils.parseDateAsDuration(nextRun);
}
// get hold of persistence and create instance of audit service
EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(emfName);
// collect parameters
String forDeployment = (String) ctx.getData("ForDeployment");
// SLA Violations on nodes
Map<String, Object> parameters = new HashMap<>();
parameters.put("now", new Date());
StringBuilder lookupQuery = new StringBuilder();
lookupQuery.append("select log from NodeInstanceLog log where ");
lookupQuery.append("log.nodeInstanceId in ( select nil.nodeInstanceId from NodeInstanceLog nil where nil.slaDueDate < :now and nil.slaCompliance = 1 ");
lookupQuery.append("GROUP BY nil.nodeInstanceId ");
lookupQuery.append("HAVING sum(nil.type) = 0) ");
lookupQuery.append("and log.type = 0 ");
if (forDeployment != null && !forDeployment.isEmpty()) {
lookupQuery.append(" and log.externalId = :forDeployment");
parameters.put("forDeployment", forDeployment);
}
TransactionalCommandService commandService = new TransactionalCommandService(emf);
List<NodeInstanceLog> nodeInstancesViolations = commandService.execute(new QueryStringCommand<List<NodeInstanceLog>>(lookupQuery.toString(), parameters));
logger.debug("Number of node instances with violated SLA {}", nodeInstancesViolations.size());
if (!nodeInstancesViolations.isEmpty()) {
logger.debug("Signaling process instances that have SLA violations on nodes");
int nodeSignals = 0;
for (NodeInstanceLog niLog : nodeInstancesViolations) {
RuntimeManager runtimeManager = RuntimeManagerRegistry.get().getManager(niLog.getExternalId());
if (runtimeManager == null) {
logger.debug("No runtime manager found for {}, not able to send SLA violation signal", niLog.getExternalId());
continue;
}
RuntimeEngine engine = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get(niLog.getProcessInstanceId()));
try {
engine.getKieSession().signalEvent("slaViolation:" + niLog.getNodeInstanceId(), null, niLog.getProcessInstanceId());
nodeSignals++;
} catch (Exception e) {
logger.warn("Unexpected error when signalig process instance {} about SLA violation {}", niLog.getProcessInstanceId(), e.getMessage(), e);
} finally {
runtimeManager.disposeRuntimeEngine(engine);
}
}
logger.info("SLA Violations JOB :: Number of nodes successfully signaled is {}", nodeSignals);
executionResults.setData("NodeSLASignals", nodeSignals);
}
// SLA Violations on process instances
parameters = new HashMap<>();
parameters.put("now", new Date());
lookupQuery = new StringBuilder();
lookupQuery.append("select log from ProcessInstanceLog log where log.slaDueDate < :now and log.slaCompliance = 1 ");
if (forDeployment != null && !forDeployment.isEmpty()) {
lookupQuery.append(" and log.externalId = :forDeployment");
parameters.put("forDeployment", forDeployment);
}
List<ProcessInstanceLog> processInstancesViolations = commandService.execute(new QueryStringCommand<List<ProcessInstanceLog>>(lookupQuery.toString(), parameters));
logger.debug("Number of node instances with violated SLA {}", nodeInstancesViolations.size());
if (!processInstancesViolations.isEmpty()) {
logger.debug("Signaling process instances that have SLA violations");
int processSignals = 0;
for (ProcessInstanceLog piLog : processInstancesViolations) {
RuntimeManager runtimeManager = RuntimeManagerRegistry.get().getManager(piLog.getExternalId());
if (runtimeManager == null) {
logger.debug("No runtime manager found for {}, not able to send SLA violation signal", piLog.getExternalId());
continue;
}
RuntimeEngine engine = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get(piLog.getProcessInstanceId()));
try {
engine.getKieSession().signalEvent("slaViolation", null, piLog.getProcessInstanceId());
processSignals++;
} catch (Exception e) {
logger.warn("Unexpected error when signalig process instance {} about SLA violation {}", piLog.getProcessInstanceId(), e.getMessage(), e);
} finally {
runtimeManager.disposeRuntimeEngine(engine);
}
}
logger.info("SLA Violations JOB :: Number of process instances successfully signaled is {}", processSignals);
executionResults.setData("ProcessSLASignals", processSignals);
}
return executionResults;
}
use of org.jbpm.process.audit.NodeInstanceLog in project jbpm by kiegroup.
the class JbpmJUnitTestCase method assertNodeTriggered.
public void assertNodeTriggered(long processInstanceId, String... nodeNames) {
List<String> names = new ArrayList<String>();
for (String nodeName : nodeNames) {
names.add(nodeName);
}
if (sessionPersistence) {
List<NodeInstanceLog> logs = logService.findNodeInstances(processInstanceId);
if (logs != null) {
for (NodeInstanceLog l : logs) {
String nodeName = l.getNodeName();
if ((l.getType() == NodeInstanceLog.TYPE_ENTER || l.getType() == NodeInstanceLog.TYPE_EXIT) && names.contains(nodeName)) {
names.remove(nodeName);
}
}
}
} else {
for (LogEvent event : logger.getLogEvents()) {
if (event instanceof RuleFlowNodeLogEvent) {
String nodeName = ((RuleFlowNodeLogEvent) event).getNodeName();
if (names.contains(nodeName)) {
names.remove(nodeName);
}
}
}
}
if (!names.isEmpty()) {
String s = names.get(0);
for (int i = 1; i < names.size(); i++) {
s += ", " + names.get(i);
}
fail("Node(s) not executed: " + s);
}
}
use of org.jbpm.process.audit.NodeInstanceLog in project jbpm by kiegroup.
the class ManagedAuditEventBuilderImpl method buildEvent.
@Override
public AuditEvent buildEvent(ProcessNodeLeftEvent pnle, Object log) {
NodeInstanceLog nodeInstanceLog = (NodeInstanceLog) super.buildEvent(pnle, log);
nodeInstanceLog.setExternalId(ownerId);
return nodeInstanceLog;
}
use of org.jbpm.process.audit.NodeInstanceLog in project jbpm by kiegroup.
the class ServicesAwareAuditEventBuilder method buildEvent.
@Override
public AuditEvent buildEvent(ProcessNodeLeftEvent pnle, Object log) {
NodeInstanceLog nodeInstanceLog = (NodeInstanceLog) super.buildEvent(pnle, log);
nodeInstanceLog.setExternalId(deploymentUnitId);
return nodeInstanceLog;
}
Aggregations