Search in sources :

Example 11 with InternalTaskService

use of org.kie.internal.task.api.InternalTaskService in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManager method getRuntimeEngine.

@Override
public RuntimeEngine getRuntimeEngine(Context<?> context) {
    if (isClosed()) {
        throw new IllegalStateException("Runtime manager " + identifier + " is already closed");
    }
    checkPermission();
    RuntimeEngine runtime = null;
    Object contextId = context.getContextId();
    if (!(context instanceof ProcessInstanceIdContext || context instanceof CorrelationKeyContext)) {
        logger.warn("ProcessInstanceIdContext or CorrelationKeyContext shall be used when interacting with PerProcessInstance runtime manager");
    }
    if (engineInitEager) {
        KieSession ksession = null;
        Long ksessionId = null;
        if (contextId == null || context instanceof EmptyContext) {
            ksession = factory.newKieSession();
            ksessionId = ksession.getIdentifier();
        } else {
            RuntimeEngine localRuntime = findLocalRuntime(contextId);
            if (localRuntime != null) {
                return localRuntime;
            }
            ksessionId = mapper.findMapping(context, this.identifier);
            if (ksessionId == null) {
                throw new SessionNotFoundException("No session found for context " + context.getContextId());
            }
            ksession = factory.findKieSessionById(ksessionId);
        }
        InternalTaskService internalTaskService = newTaskService(taskServiceFactory);
        runtime = new RuntimeEngineImpl(ksession, internalTaskService);
        ((RuntimeEngineImpl) runtime).setManager(this);
        ((RuntimeEngineImpl) runtime).setContext(context);
        configureRuntimeOnTaskService(internalTaskService, runtime);
        registerDisposeCallback(runtime, new DisposeSessionTransactionSynchronization(this, runtime), ksession.getEnvironment());
        registerItems(runtime);
        attachManager(runtime);
        ksession.addEventListener(new MaintainMappingListener(ksessionId, runtime, this.identifier));
    } else {
        RuntimeEngine localRuntime = findLocalRuntime(contextId);
        if (localRuntime != null) {
            return localRuntime;
        }
        // lazy initialization of ksession and task service
        runtime = new RuntimeEngineImpl(context, new PerProcessInstanceInitializer());
        ((RuntimeEngineImpl) runtime).setManager(this);
    }
    createLockOnGetEngine(context, runtime);
    saveLocalRuntime(contextId, runtime);
    return runtime;
}
Also used : EmptyContext(org.kie.internal.runtime.manager.context.EmptyContext) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) InternalTaskService(org.kie.internal.task.api.InternalTaskService) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext) CorrelationKeyContext(org.kie.internal.runtime.manager.context.CorrelationKeyContext) DisposeSessionTransactionSynchronization(org.jbpm.runtime.manager.impl.tx.DisposeSessionTransactionSynchronization) KieSession(org.kie.api.runtime.KieSession) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException)

Example 12 with InternalTaskService

use of org.kie.internal.task.api.InternalTaskService in project jbpm by kiegroup.

the class PerRequestRuntimeManager method getRuntimeEngine.

@Override
public RuntimeEngine getRuntimeEngine(Context<?> context) {
    if (isClosed()) {
        throw new IllegalStateException("Runtime manager " + identifier + " is already closed");
    }
    checkPermission();
    RuntimeEngine runtime = null;
    if (local.get().get(identifier) != null) {
        RuntimeEngine engine = local.get().get(identifier);
        // check if engine is not already disposed as afterCompletion might be issued from another thread
        if (engine != null && ((RuntimeEngineImpl) engine).isDisposed()) {
            return null;
        }
        return engine;
    }
    if (engineInitEager) {
        InternalTaskService internalTaskService = newTaskService(taskServiceFactory);
        runtime = new RuntimeEngineImpl(factory.newKieSession(), internalTaskService);
        ((RuntimeEngineImpl) runtime).setManager(this);
        configureRuntimeOnTaskService(internalTaskService, runtime);
        registerDisposeCallback(runtime, new DisposeSessionTransactionSynchronization(this, runtime), runtime.getKieSession().getEnvironment());
        registerDisposeCallback(runtime, new DestroySessionTransactionSynchronization(runtime.getKieSession()), runtime.getKieSession().getEnvironment());
        registerItems(runtime);
        attachManager(runtime);
    } else {
        runtime = new RuntimeEngineImpl(context, new PerRequestInitializer());
        ((RuntimeEngineImpl) runtime).setManager(this);
    }
    local.get().put(identifier, runtime);
    return runtime;
}
Also used : DestroySessionTransactionSynchronization(org.jbpm.runtime.manager.impl.tx.DestroySessionTransactionSynchronization) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) DisposeSessionTransactionSynchronization(org.jbpm.runtime.manager.impl.tx.DisposeSessionTransactionSynchronization) InternalTaskService(org.kie.internal.task.api.InternalTaskService)

Example 13 with InternalTaskService

use of org.kie.internal.task.api.InternalTaskService in project jbpm by kiegroup.

the class AbstractRuntimeManager method newTaskService.

protected InternalTaskService newTaskService(TaskServiceFactory factory) {
    InternalTaskService internalTaskService = (InternalTaskService) factory.newTaskService();
    if (internalTaskService instanceof CommandBasedTaskService) {
        ((CommandBasedTaskService) internalTaskService).getEnvironment().set(EnvironmentName.DEPLOYMENT_ID, this.getIdentifier());
        ((CommandBasedTaskService) internalTaskService).getEnvironment().set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, ((SimpleRuntimeEnvironment) environment).getEnvironmentTemplate().get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES));
    }
    return internalTaskService;
}
Also used : InternalTaskService(org.kie.internal.task.api.InternalTaskService) CommandBasedTaskService(org.jbpm.services.task.impl.command.CommandBasedTaskService)

Example 14 with InternalTaskService

use of org.kie.internal.task.api.InternalTaskService in project jbpm by kiegroup.

the class CallActivitiesWithUserTasksProcessTest method testCallActivitiesWith2ndUserTaskInSub.

@Test
public void testCallActivitiesWith2ndUserTaskInSub() throws Exception {
    TaskCleanUpProcessEventListener taskListener = new TaskCleanUpProcessEventListener(null);
    addWorkItemHandler("Sysout", new SystemOutWorkItemHandler());
    addProcessEventListener(taskListener);
    InitialContext context = new InitialContext();
    UserTransaction ut = (UserTransaction) context.lookup(JtaTransactionManager.DEFAULT_USER_TRANSACTION_NAME);
    RuntimeManager manager = createRuntimeManager(strategy, (String) null, "org/jbpm/test/functional/subprocess/CallActivityWithTaskInSub-Main.bpmn2", "org/jbpm/test/functional/subprocess/CallActivityWithTaskInSub-Sub.bpmn2");
    RuntimeEngine runtimeEngine;
    if (Strategy.SINGLETON.equals(strategy)) {
        runtimeEngine = getRuntimeEngine();
    } else if (Strategy.PROCESS_INSTANCE.equals(strategy)) {
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get());
    } else {
        throw new IllegalStateException("Not possible!");
    }
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    // set created task service on listener
    taskListener.setTaskService((InternalTaskService) taskService);
    if (userManagedTx) {
        ut.begin();
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("pActorId", "john");
    ProcessInstance processInstance = ksession.startProcess("PolicyValueAnalysis", params);
    if (userManagedTx) {
        ut.commit();
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
        ut.begin();
    } else {
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
    }
    List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    Long taskId = tasks.get(0).getId();
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    if (userManagedTx) {
        ut.commit();
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
        ut.begin();
    } else {
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
    }
    // sub process task
    tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    taskId = tasks.get(0).getId();
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    if (userManagedTx) {
        ut.commit();
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
        ut.begin();
    } else {
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
    }
    tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    taskId = tasks.get(0).getId();
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    if (userManagedTx) {
        ut.commit();
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
        ut.begin();
    } else {
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
    }
    // sub process task
    tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    taskId = tasks.get(0).getId();
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    if (userManagedTx) {
        // since we are checking in same transaction it will already see it as completed
        assertProcessInstanceCompleted(processInstance.getId());
        ut.commit();
    }
    assertProcessInstanceCompleted(processInstance.getId());
}
Also used : UserTransaction(javax.transaction.UserTransaction) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) InternalTaskService(org.kie.internal.task.api.InternalTaskService) TaskService(org.kie.api.task.TaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) TaskCleanUpProcessEventListener(org.jbpm.services.task.admin.listener.TaskCleanUpProcessEventListener) InitialContext(javax.naming.InitialContext) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test)

Example 15 with InternalTaskService

use of org.kie.internal.task.api.InternalTaskService in project jbpm by kiegroup.

the class CallActivitiesWithUserTasksProcessTest method testCallActivitiesWithUserTasks.

@Test
public void testCallActivitiesWithUserTasks() throws Exception {
    TaskCleanUpProcessEventListener taskListener = new TaskCleanUpProcessEventListener(null);
    addWorkItemHandler("Sysout", new SystemOutWorkItemHandler());
    addProcessEventListener(taskListener);
    InitialContext context = new InitialContext();
    UserTransaction ut = (UserTransaction) context.lookup(JtaTransactionManager.DEFAULT_USER_TRANSACTION_NAME);
    RuntimeManager manager = createRuntimeManager(strategy, (String) null, "org/jbpm/test/functional/subprocess/CallActivityWithTask-Main.bpmn2", "org/jbpm/test/functional/subprocess/CallActivityWithTask-Sub.bpmn2");
    RuntimeEngine runtimeEngine;
    if (Strategy.SINGLETON.equals(strategy)) {
        runtimeEngine = getRuntimeEngine();
    } else if (Strategy.PROCESS_INSTANCE.equals(strategy)) {
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get());
    } else {
        throw new IllegalStateException("Not possible!");
    }
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    // set created task service on listener
    taskListener.setTaskService((InternalTaskService) taskService);
    if (userManagedTx) {
        ut.begin();
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("pActorId", "john");
    ProcessInstance processInstance = ksession.startProcess("PolicyValueAnalysis", params);
    if (userManagedTx) {
        ut.commit();
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
        ut.begin();
    } else {
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
    }
    List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    Long taskId = tasks.get(0).getId();
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    if (userManagedTx) {
        ut.commit();
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
        ut.begin();
    } else {
        manager.disposeRuntimeEngine(runtimeEngine);
        runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
        ksession = runtimeEngine.getKieSession();
        taskService = runtimeEngine.getTaskService();
        taskListener.setTaskService((InternalTaskService) taskService);
    }
    tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    taskId = tasks.get(0).getId();
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    if (userManagedTx) {
        ut.commit();
        ut.begin();
    }
    assertProcessInstanceCompleted(processInstance.getId());
    if (userManagedTx) {
        ut.commit();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) InternalTaskService(org.kie.internal.task.api.InternalTaskService) TaskService(org.kie.api.task.TaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) TaskCleanUpProcessEventListener(org.jbpm.services.task.admin.listener.TaskCleanUpProcessEventListener) InitialContext(javax.naming.InitialContext) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test)

Aggregations

InternalTaskService (org.kie.internal.task.api.InternalTaskService)44 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)35 TaskService (org.kie.api.task.TaskService)27 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)25 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)20 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)18 UserTaskService (org.jbpm.services.api.UserTaskService)17 TaskSummary (org.kie.api.task.model.TaskSummary)13 Test (org.junit.Test)12 KieSession (org.kie.api.runtime.KieSession)11 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)9 Task (org.kie.api.task.model.Task)5 TaskCleanUpProcessEventListener (org.jbpm.services.task.admin.listener.TaskCleanUpProcessEventListener)4 AuditService (org.kie.api.runtime.manager.audit.AuditService)4 I18NText (org.kie.api.task.model.I18NText)4 Date (java.util.Date)3 EntityManager (javax.persistence.EntityManager)3 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)3