Search in sources :

Example 6 with TaskServiceFactory

use of org.kie.internal.runtime.manager.TaskServiceFactory in project jbpm-work-items by kiegroup.

the class JPAWorkItemHandlerTest method startJPAWIHProcess.

private void startJPAWIHProcess(String action, Product prod) throws Exception {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
    RuntimeEnvironment env = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).addEnvironmentEntry("org.kie.internal.runtime.manager.TaskServiceFactory", new TaskServiceFactory() {

        @Override
        public TaskService newTaskService() {
            return null;
        }

        @Override
        public void close() {
        }
    }).addAsset(ResourceFactory.newClassPathResource("JPAWIH.bpmn2"), ResourceType.BPMN2).get();
    RuntimeManager manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(env);
    RuntimeEngine engine = manager.getRuntimeEngine(EmptyContext.get());
    KieSession kSession = engine.getKieSession();
    JPAWorkItemHandler jpaWih = new JPAWorkItemHandler(P_UNIT, this.getClass().getClassLoader());
    kSession.getWorkItemManager().registerWorkItemHandler("JPAWIH", jpaWih);
    HashMap<String, Object> params = new HashMap<>();
    params.put("action", action);
    params.put("obj", prod);
    params.put("id", prod.getId());
    kSession.startProcess("org.jbpm.JPA_WIH", params);
    manager.disposeRuntimeEngine(engine);
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) TaskServiceFactory(org.kie.internal.runtime.manager.TaskServiceFactory) EntityManagerFactory(javax.persistence.EntityManagerFactory) KieSession(org.kie.api.runtime.KieSession)

Example 7 with TaskServiceFactory

use of org.kie.internal.runtime.manager.TaskServiceFactory in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManagerTest method testCreationOfSessionCustomTaskServiceFactory.

@Test
public void testCreationOfSessionCustomTaskServiceFactory() {
    final AtomicBoolean customTaskServiceUsed = new AtomicBoolean(false);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().userGroupCallback(userGroupCallback).entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).addEnvironmentEntry("org.kie.internal.runtime.manager.TaskServiceFactory", new TaskServiceFactory() {

        private EntityManagerFactory emf;

        public EntityManagerFactory produceEntityManagerFactory() {
            if (this.emf == null) {
                this.emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
            }
            return this.emf;
        }

        @Override
        public TaskService newTaskService() {
            customTaskServiceUsed.set(true);
            return HumanTaskServiceFactory.newTaskServiceConfigurator().entityManagerFactory(produceEntityManagerFactory()).listener(new JPATaskLifeCycleEventListener(true)).getTaskService();
        }

        @Override
        public void close() {
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(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(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long ksession1Id = ksession.getIdentifier();
    assertTrue(ksession1Id == 1);
    // FIXME quick hack to overcome problems with same pi ids when not using persistence
    ksession.startProcess("ScriptTask");
    // ksession for process instance #2
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession2 = runtime2.getKieSession();
    assertNotNull(ksession2);
    long ksession2Id = ksession2.getIdentifier();
    assertTrue(ksession2Id == 2);
    ProcessInstance pi1 = ksession.startProcess("UserTask");
    ProcessInstance pi2 = ksession2.startProcess("UserTask");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    assertEquals(ProcessInstance.STATE_ACTIVE, pi2.getState());
    runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId()));
    ksession = runtime.getKieSession();
    assertEquals(ksession1Id, ksession.getIdentifier());
    runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi2.getId()));
    ksession2 = runtime2.getKieSession();
    assertEquals(ksession2Id, ksession2.getIdentifier());
    manager.close();
    // check if our custom task service factory was used
    assertTrue(customTaskServiceUsed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TaskServiceFactory(org.kie.internal.runtime.manager.TaskServiceFactory) HumanTaskServiceFactory(org.jbpm.services.task.HumanTaskServiceFactory) JPATaskLifeCycleEventListener(org.jbpm.services.task.audit.JPATaskLifeCycleEventListener) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) EntityManagerFactory(javax.persistence.EntityManagerFactory) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 8 with TaskServiceFactory

use of org.kie.internal.runtime.manager.TaskServiceFactory in project jbpm by kiegroup.

the class RuntimeManagerFactoryImpl method newSingletonRuntimeManager.

@Override
public RuntimeManager newSingletonRuntimeManager(RuntimeEnvironment environment, String identifier) {
    SessionFactory factory = getSessionFactory(environment, identifier);
    TaskServiceFactory taskServiceFactory = getTaskServiceFactory(environment);
    RuntimeManager manager = new SingletonRuntimeManager(environment, factory, taskServiceFactory, identifier);
    initTimerService(environment, manager);
    ((AbstractRuntimeManager) manager).init();
    return manager;
}
Also used : InMemorySessionFactory(org.jbpm.runtime.manager.impl.factory.InMemorySessionFactory) SessionFactory(org.kie.internal.runtime.manager.SessionFactory) JPASessionFactory(org.jbpm.runtime.manager.impl.factory.JPASessionFactory) LocalTaskServiceFactory(org.jbpm.runtime.manager.impl.factory.LocalTaskServiceFactory) TaskServiceFactory(org.kie.internal.runtime.manager.TaskServiceFactory) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager)

Aggregations

TaskServiceFactory (org.kie.internal.runtime.manager.TaskServiceFactory)8 LocalTaskServiceFactory (org.jbpm.runtime.manager.impl.factory.LocalTaskServiceFactory)6 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)5 InMemorySessionFactory (org.jbpm.runtime.manager.impl.factory.InMemorySessionFactory)4 JPASessionFactory (org.jbpm.runtime.manager.impl.factory.JPASessionFactory)4 SessionFactory (org.kie.internal.runtime.manager.SessionFactory)4 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 KieSession (org.kie.api.runtime.KieSession)2 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)2 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)2 HashMap (java.util.HashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SimpleRuntimeEnvironment (org.jbpm.runtime.manager.impl.SimpleRuntimeEnvironment)1 HumanTaskServiceFactory (org.jbpm.services.task.HumanTaskServiceFactory)1 JPATaskLifeCycleEventListener (org.jbpm.services.task.audit.JPATaskLifeCycleEventListener)1 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)1 Test (org.junit.Test)1 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)1 TaskService (org.kie.api.task.TaskService)1