Search in sources :

Example 76 with ProcessEventListener

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;
}
Also used : InputStream(java.io.InputStream) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) IOException(java.io.IOException) Properties(java.util.Properties) AbstractAuditLogger(org.jbpm.process.audit.AbstractAuditLogger) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with ProcessEventListener

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);
    }
}
Also used : ProcessEventListener(org.kie.api.event.process.ProcessEventListener) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) RegisterableItemsFactory(org.kie.api.runtime.manager.RegisterableItemsFactory) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener)

Example 78 with ProcessEventListener

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);
}
Also used : Status(org.kie.api.task.model.Status) TaskService(org.kie.api.task.TaskService) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DebugProcessEventListener(org.drools.core.event.DebugProcessEventListener) ArrayList(java.util.ArrayList) PerProcessInstanceRuntimeManager(org.jbpm.runtime.manager.impl.PerProcessInstanceRuntimeManager) EventService(org.kie.internal.task.api.EventService) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) PerProcessInstance(org.kie.internal.runtime.manager.cdi.qualifier.PerProcessInstance)

Example 79 with ProcessEventListener

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();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 80 with ProcessEventListener

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();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AuditService(org.kie.api.runtime.manager.audit.AuditService) HashSet(java.util.HashSet) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

ProcessEventListener (org.kie.api.event.process.ProcessEventListener)88 Test (org.junit.Test)77 KieSession (org.kie.api.runtime.KieSession)64 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)64 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)61 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)58 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)49 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)44 ArrayList (java.util.ArrayList)41 DefaultRegisterableItemsFactory (org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory)33 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)30 HashMap (java.util.HashMap)29 AbstractExecutorBaseTest (org.jbpm.test.util.AbstractExecutorBaseTest)29 ProcessNodeLeftEvent (org.kie.api.event.process.ProcessNodeLeftEvent)27 NodeTriggeredCountDownProcessEventListener (org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener)20 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)18 KieBase (org.kie.api.KieBase)18 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)17 ProcessStartedEvent (org.kie.api.event.process.ProcessStartedEvent)17 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)14