use of org.kie.api.event.process.ProcessEventListener in project jbpm by kiegroup.
the class DefaultRegisterableItemsFactory method getProcessEventListeners.
@Override
public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
List<ProcessEventListener> defaultListeners = new ArrayList<ProcessEventListener>();
DeploymentDescriptor descriptor = getRuntimeManager().getDeploymentDescriptor();
if (descriptor == null) {
// register JPAWorkingMemoryDBLogger
AbstractAuditLogger logger = AuditLoggerFactory.newJPAInstance(runtime.getKieSession().getEnvironment());
logger.setBuilder(getAuditBuilder(runtime));
defaultListeners.add(logger);
} else if (descriptor.getAuditMode() == AuditMode.JPA) {
// register JPAWorkingMemoryDBLogger
AbstractAuditLogger logger = null;
if (descriptor.getPersistenceUnit().equals(descriptor.getAuditPersistenceUnit())) {
logger = AuditLoggerFactory.newJPAInstance(runtime.getKieSession().getEnvironment());
} else {
Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, EntityManagerFactoryManager.get().getOrCreate(descriptor.getAuditPersistenceUnit()));
logger = AuditLoggerFactory.newJPAInstance(env);
}
logger.setBuilder(getAuditBuilder(runtime));
defaultListeners.add(logger);
} else if (descriptor.getAuditMode() == AuditMode.JMS) {
try {
if (jmsLogger == null) {
Properties properties = new Properties();
InputStream input = getRuntimeManager().getEnvironment().getClassLoader().getResourceAsStream("/jbpm.audit.jms.properties");
// required for junit test
if (input == null) {
input = getRuntimeManager().getEnvironment().getClassLoader().getResourceAsStream("jbpm.audit.jms.properties");
}
properties.load(input);
logger.debug("Creating AsyncAuditLogProducer {}", properties);
jmsLogger = AuditLoggerFactory.newJMSInstance((Map) properties);
jmsLogger.setBuilder(getAuditBuilder(runtime));
}
defaultListeners.add(jmsLogger);
} catch (IOException e) {
logger.error("Unable to load jms audit properties from {}", "/jbpm.audit.jms.properties", e);
}
}
// add any custom listeners
defaultListeners.addAll(super.getProcessEventListeners(runtime));
// add listeners from descriptor
defaultListeners.addAll(getEventListenerFromDescriptor(runtime, ProcessEventListener.class));
return defaultListeners;
}
use of org.kie.api.event.process.ProcessEventListener in project jbpm by kiegroup.
the class AbstractRuntimeManager method registerItems.
protected void registerItems(RuntimeEngine runtime) {
RegisterableItemsFactory factory = environment.getRegisterableItemsFactory();
// process handlers
Map<String, WorkItemHandler> handlers = factory.getWorkItemHandlers(runtime);
for (Entry<String, WorkItemHandler> entry : handlers.entrySet()) {
runtime.getKieSession().getWorkItemManager().registerWorkItemHandler(entry.getKey(), entry.getValue());
}
// register globals
Map<String, Object> globals = factory.getGlobals(runtime);
for (Entry<String, Object> entry : globals.entrySet()) {
runtime.getKieSession().setGlobal(entry.getKey(), entry.getValue());
}
// process listeners
List<ProcessEventListener> processListeners = factory.getProcessEventListeners(runtime);
for (ProcessEventListener listener : processListeners) {
runtime.getKieSession().addEventListener(listener);
}
// agenda listeners
List<AgendaEventListener> agendaListeners = factory.getAgendaEventListeners(runtime);
for (AgendaEventListener listener : agendaListeners) {
runtime.getKieSession().addEventListener(listener);
}
// working memory listeners
List<RuleRuntimeEventListener> wmListeners = factory.getRuleRuntimeEventListeners(runtime);
for (RuleRuntimeEventListener listener : wmListeners) {
runtime.getKieSession().addEventListener(listener);
}
}
use of org.kie.api.event.process.ProcessEventListener in project jbpm by kiegroup.
the class SingleRuntimeManagerWithListenersTest method testProcessStartOnManager.
private void testProcessStartOnManager(RuntimeEngine runtime) {
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
Collection<ProcessEventListener> pListeners = ksession.getProcessEventListeners();
assertNotNull(pListeners);
// prepare listeners class names for assertion
List<String> listenerCLassNames = new ArrayList<String>();
for (Object o : pListeners) {
listenerCLassNames.add(o.getClass().getName());
}
// DebugProcessEventListener was added by custom producer
assertTrue(listenerCLassNames.contains(DebugProcessEventListener.class.getName()));
// JPAWorkingMemoryDbLogger one is always added to deal with user tasks
assertTrue(listenerCLassNames.contains(JPAWorkingMemoryDbLogger.class.getName()));
if (((RuntimeEngineImpl) runtime).getManager() instanceof PerProcessInstanceRuntimeManager) {
assertEquals(3, pListeners.size());
} else {
assertEquals(2, pListeners.size());
}
TaskService taskService = runtime.getTaskService();
assertNotNull(taskService);
List<?> listeners = ((EventService<?>) taskService).getTaskEventListeners();
assertNotNull(listeners);
assertEquals(3, listeners.size());
// prepare listeners class names for assertion
listenerCLassNames = new ArrayList<String>();
for (Object o : listeners) {
System.out.println("###### " + o.getClass().getName());
listenerCLassNames.add(o.getClass().getName());
}
assertEquals(3, listenerCLassNames.size());
// JPATaskLifeCycleEventListener was added by custom producer
assertTrue(listenerCLassNames.contains(JPATaskLifeCycleEventListener.class.getName()));
// external one is always added to deal with user tasks
assertTrue(listenerCLassNames.contains(ExternalTaskEventListener.class.getName()));
// BAMTaskEventListener was added by custom producer
assertTrue(listenerCLassNames.contains(BAMTaskEventListener.class.getName()));
ProcessInstance processInstance = ksession.startProcess("UserTask");
assertNotNull(processInstance);
List<Status> statuses = new ArrayList<Status>();
statuses.add(Status.Reserved);
List<TaskSummary> tasks = taskService.getTasksOwnedByStatus("john", statuses, "en-UK");
assertNotNull(tasks);
assertEquals(1, tasks.size());
taskService.start(tasks.get(0).getId(), "john");
taskService.complete(tasks.get(0).getId(), "john", null);
processInstance = ksession.getProcessInstance(processInstance.getId());
assertNull(processInstance);
}
use of org.kie.api.event.process.ProcessEventListener in project jbpm by kiegroup.
the class PerCaseRuntimeManagerTest method testMultipleProcessesInSingleCaseCompletedInSequence.
@Test
public void testMultipleProcessesInSingleCaseCompletedInSequence() {
final Set<Long> ksessionUsed = new HashSet<Long>();
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {
@Override
public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
listeners.add(new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
ksessionUsed.add(((KieSession) event.getKieRuntime()).getIdentifier());
}
});
return listeners;
}
}).get();
manager = RuntimeManagerFactory.Factory.get().newPerCaseRuntimeManager(environment);
assertNotNull(manager);
// ksession for process instance #1
// since there is no process instance yet we need to get new session
RuntimeEngine runtime = manager.getRuntimeEngine(CaseContext.get("Case-1"));
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
long ksession1Id = ksession.getIdentifier();
assertTrue(ksession1Id > 0);
ProcessInstance pi1 = ksession.startProcess("ScriptTask");
assertEquals(ProcessInstance.STATE_COMPLETED, pi1.getState());
manager.disposeRuntimeEngine(runtime);
runtime = manager.getRuntimeEngine(CaseContext.get("Case-1"));
ksession = runtime.getKieSession();
ProcessInstance pi2 = ksession.startProcess("ScriptTask");
assertEquals(ProcessInstance.STATE_COMPLETED, pi2.getState());
// there should be only one ksession used
assertEquals(1, ksessionUsed.size());
assertEquals(ksession1Id, ksessionUsed.iterator().next().longValue());
manager.close();
}
use of org.kie.api.event.process.ProcessEventListener in project jbpm by kiegroup.
the class PerCaseRuntimeManagerTest method testExecuteReusableSubprocess.
@Test
public void testExecuteReusableSubprocess() {
final Set<Long> ksessionUsed = new HashSet<Long>();
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-CallActivity.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-CallActivitySubProcess.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {
@Override
public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
listeners.add(new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
ksessionUsed.add(((KieSession) event.getKieRuntime()).getIdentifier());
}
});
return listeners;
}
}).get();
manager = RuntimeManagerFactory.Factory.get().newPerCaseRuntimeManager(environment);
assertNotNull(manager);
// since there is no process instance yet we need to get new session
RuntimeEngine runtime = manager.getRuntimeEngine(CaseContext.get("Case-1"));
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
long ksession1Id = ksession.getIdentifier();
assertTrue(ksession1Id == 2);
ProcessInstance pi1 = ksession.startProcess("ParentProcess");
assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
manager.disposeRuntimeEngine(runtime);
runtime = manager.getRuntimeEngine(CaseContext.get("Case-1"));
ksession = runtime.getKieSession();
ksession.getWorkItemManager().completeWorkItem(1, null);
AuditService logService = runtime.getAuditService();
List<? extends ProcessInstanceLog> logs = logService.findActiveProcessInstances("ParentProcess");
assertNotNull(logs);
assertEquals(0, logs.size());
logs = logService.findActiveProcessInstances("SubProcess");
assertNotNull(logs);
assertEquals(0, logs.size());
logs = logService.findProcessInstances("ParentProcess");
assertNotNull(logs);
assertEquals(1, logs.size());
String externalId = logs.get(0).getExternalId();
assertEquals(manager.getIdentifier(), externalId);
logs = logService.findProcessInstances("SubProcess");
assertNotNull(logs);
assertEquals(1, logs.size());
externalId = logs.get(0).getExternalId();
assertEquals(manager.getIdentifier(), externalId);
assertEquals(1, ksessionUsed.size());
assertEquals(ksession1Id, ksessionUsed.iterator().next().longValue());
manager.disposeRuntimeEngine(runtime);
manager.close();
}
Aggregations