Search in sources :

Example 51 with TaskService

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

the class HumanTaskVariablesAccessTest method testHumanTaskWithListener.

private void testHumanTaskWithListener(TaskLifeCycleEventListener listener) {
    addTaskEventListener(listener);
    createRuntimeManager("org/jbpm/test/functional/task/HumanTaskWithVariables.bpmn2");
    RuntimeEngine runtimeEngine = getRuntimeEngine();
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("processHTInput", "Simple human task input");
    ProcessInstance processInstance = ksession.startProcess("humanTaskWithVariables", params);
    assertProcessInstanceActive(processInstance.getId(), ksession);
    assertNodeTriggered(processInstance.getId(), "Start", "Task");
    // let john execute Task
    List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    TaskSummary task = list.get(0);
    logger.info("John is executing task {}", task.getName());
    params.clear();
    params.put("humanTaskOutput", "Simple human task output");
    taskService.start(task.getId(), "john");
    taskService.complete(task.getId(), "john", params);
    assertNodeTriggered(processInstance.getId(), "End");
    assertProcessInstanceNotActive(processInstance.getId(), ksession);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 52 with TaskService

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

the class PessimisticLockTasksServiceTest method testPessimisticLockingOnTask.

@Test
public void testPessimisticLockingOnTask() throws Exception {
    final List<Exception> exceptions = new ArrayList<Exception>();
    addEnvironmentEntry(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
    createRuntimeManager("org/jbpm/test/functional/task/Evaluation2.bpmn");
    RuntimeEngine runtimeEngine = getRuntimeEngine();
    final KieSession ksession = runtimeEngine.getKieSession();
    final TaskService taskService = runtimeEngine.getTaskService();
    // setup another instance of task service to allow not synchronized access to cause pessimistic lock exception
    RuntimeEnvironment runtimeEnv = ((InternalRuntimeManager) manager).getEnvironment();
    HumanTaskConfigurator configurator = HumanTaskServiceFactory.newTaskServiceConfigurator().environment(runtimeEnv.getEnvironment()).entityManagerFactory((EntityManagerFactory) runtimeEnv.getEnvironment().get(EnvironmentName.ENTITY_MANAGER_FACTORY)).userGroupCallback(runtimeEnv.getUserGroupCallback());
    // register task listeners if any
    RegisterableItemsFactory itemsFactory = runtimeEnv.getRegisterableItemsFactory();
    for (TaskLifeCycleEventListener taskListener : itemsFactory.getTaskListeners()) {
        configurator.listener(taskListener);
    }
    final TaskService internalTaskService = configurator.getTaskService();
    logger.info("### Starting process ###");
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("employee", "salaboy");
    ProcessInstance process = ksession.startProcess("com.sample.evaluation", parameters);
    // The process is in the first Human Task waiting for its completion
    assertEquals(ProcessInstance.STATE_ACTIVE, process.getState());
    // gets salaboy's tasks
    List<TaskSummary> salaboysTasks = taskService.getTasksAssignedAsPotentialOwner("salaboy", "en-UK");
    assertEquals(1, salaboysTasks.size());
    final long taskId = salaboysTasks.get(0).getId();
    final CountDownLatch t2StartLockedTask = new CountDownLatch(1);
    final CountDownLatch t1Continue = new CountDownLatch(1);
    Thread t1 = new Thread() {

        @Override
        public void run() {
            try {
                UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
                try {
                    ut.begin();
                    logger.info("Attempting to lock task instance");
                    taskService.start(taskId, "salaboy");
                    t2StartLockedTask.countDown();
                    t1Continue.await();
                } finally {
                    ut.rollback();
                }
            } catch (Exception e) {
                logger.error("Error on thread ", e);
            }
        }
    };
    Thread t2 = new Thread() {

        @Override
        public void run() {
            try {
                UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
                try {
                    ut.begin();
                    t2StartLockedTask.await();
                    logger.info("Trying to start locked task instance");
                    try {
                        internalTaskService.start(taskId, "salaboy");
                    } catch (Exception e) {
                        logger.info("Abort failed with error {}", e.getMessage());
                        exceptions.add(e);
                    } finally {
                        t1Continue.countDown();
                    }
                } finally {
                    ut.rollback();
                }
            } catch (Exception e) {
                logger.error("Error on thread ", e);
            }
        }
    };
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    assertEquals(1, exceptions.size());
    assertEquals(PessimisticLockException.class.getName(), exceptions.get(0).getClass().getName());
    taskService.start(salaboysTasks.get(0).getId(), "salaboy");
    // complete task within user transaction to make sure no deadlock happens as both task service and ksession are under tx lock
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    try {
        ut.begin();
        taskService.complete(salaboysTasks.get(0).getId(), "salaboy", null);
        ut.commit();
    } catch (Exception ex) {
        ut.rollback();
        throw ex;
    }
    List<TaskSummary> pmsTasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertEquals(1, pmsTasks.size());
    List<TaskSummary> hrsTasks = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
    assertEquals(1, hrsTasks.size());
    ksession.abortProcessInstance(process.getId());
    assertProcessInstanceAborted(process.getId());
}
Also used : UserTransaction(javax.transaction.UserTransaction) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) HumanTaskConfigurator(org.jbpm.services.task.HumanTaskConfigurator) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) ArrayList(java.util.ArrayList) RegisterableItemsFactory(org.kie.api.runtime.manager.RegisterableItemsFactory) CountDownLatch(java.util.concurrent.CountDownLatch) PessimisticLockException(javax.persistence.PessimisticLockException) PessimisticLockException(javax.persistence.PessimisticLockException) EntityManagerFactory(javax.persistence.EntityManagerFactory) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) TaskLifeCycleEventListener(org.kie.api.task.TaskLifeCycleEventListener) Test(org.junit.Test)

Example 53 with TaskService

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

the class ProcessHumanTaskTest method testProcessWithCreatedBy.

@Test
public void testProcessWithCreatedBy() {
    createRuntimeManager("org/jbpm/test/functional/task/humantaskwithcreatedby.bpmn");
    RuntimeEngine runtimeEngine = getRuntimeEngine();
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("person", "krisv");
    ProcessInstance processInstance = ksession.startProcess("com.sample.bpmn.hello.createdby", params);
    assertProcessInstanceActive(processInstance.getId(), ksession);
    assertNodeTriggered(processInstance.getId(), "Start", "Task 1");
    // let john execute Task 1
    List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    TaskSummary task = list.get(0);
    assertEquals("mary", task.getCreatedById());
    logger.info("John is executing task {}", task.getName());
    taskService.start(task.getId(), "john");
    taskService.complete(task.getId(), "john", null);
    assertNodeTriggered(processInstance.getId(), "Task 2");
    // let mary execute Task 2
    list = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
    task = list.get(0);
    assertEquals("krisv", task.getCreatedById());
    logger.info("Mary is executing task {}", task.getName());
    taskService.start(task.getId(), "mary");
    taskService.complete(task.getId(), "mary", null);
    assertNodeTriggered(processInstance.getId(), "End");
    assertProcessInstanceNotActive(processInstance.getId(), ksession);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) 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 54 with TaskService

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

the class ProcessHumanTaskTest method testProcessRequestStrategy.

@Test
public void testProcessRequestStrategy() {
    createRuntimeManager(Strategy.REQUEST, "manager", "org/jbpm/test/functional/task/humantask.bpmn");
    RuntimeEngine runtimeEngine = getRuntimeEngine();
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    ProcessInstance processInstance = ksession.startProcess("com.sample.bpmn.hello");
    assertProcessInstanceActive(processInstance.getId(), ksession);
    assertNodeTriggered(processInstance.getId(), "Start", "Task 1");
    // let john execute Task 1
    List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    TaskSummary task = list.get(0);
    logger.info("John is executing task {}", task.getName());
    taskService.start(task.getId(), "john");
    taskService.complete(task.getId(), "john", null);
    assertNodeTriggered(processInstance.getId(), "Task 2");
    // let mary execute Task 2
    list = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
    task = list.get(0);
    logger.info("Mary is executing task {}", task.getName());
    taskService.start(task.getId(), "mary");
    taskService.complete(task.getId(), "mary", null);
    assertNodeTriggered(processInstance.getId(), "End");
    assertProcessInstanceNotActive(processInstance.getId(), ksession);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) TaskService(org.kie.api.task.TaskService) 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 55 with TaskService

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

the class ProcessPersistenceHumanTaskOnLaneTest method testProcessWithBAMListener.

@SuppressWarnings("unchecked")
@Test
public void testProcessWithBAMListener() throws Exception {
    createRuntimeManager("org/jbpm/test/functional/task/HumanTaskOnLane.bpmn2");
    RuntimeEngine runtimeEngine = getRuntimeEngine();
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new BAMTaskEventListener(true));
    ProcessInstance processInstance = ksession.startProcess("UserTask");
    assertProcessInstanceActive(processInstance.getId());
    long task1 = -1;
    long task2 = -1;
    // simulating a system restart
    logger.debug("Reloading the environemnt to simulate system restart");
    disposeRuntimeManager();
    createRuntimeManager("org/jbpm/test/functional/task/HumanTaskOnLane.bpmn2");
    runtimeEngine = getRuntimeEngine();
    ksession = runtimeEngine.getKieSession();
    taskService = runtimeEngine.getTaskService();
    ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new BAMTaskEventListener(true));
    // let john execute Task 1
    String taskUser = "john";
    String locale = "en-UK";
    List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner(taskUser, locale);
    assertEquals(1, list.size());
    TaskSummary task = list.get(0);
    task1 = task.getId();
    taskService.claim(task.getId(), taskUser);
    taskService.start(task.getId(), taskUser);
    taskService.complete(task.getId(), taskUser, null);
    List<Status> reservedOnly = new ArrayList<Status>();
    reservedOnly.add(Status.Reserved);
    list = taskService.getTasksAssignedAsPotentialOwnerByStatus(taskUser, reservedOnly, locale);
    assertEquals(1, list.size());
    task = list.get(0);
    task2 = task.getId();
    taskService.start(task.getId(), taskUser);
    taskService.complete(task.getId(), taskUser, null);
    assertProcessInstanceCompleted(processInstance.getId());
    EntityManager em = getEmf().createEntityManager();
    List<BAMTaskSummaryImpl> bamLogs = em.createQuery("from BAMTaskSummaryImpl").getResultList();
    em.close();
    assertNotNull(bamLogs);
    assertEquals(2, bamLogs.size());
    List<Long> taskIdsFromBAM = new ArrayList<Long>();
    for (BAMTaskSummaryImpl bamEntry : bamLogs) {
        taskIdsFromBAM.add(bamEntry.getTaskId());
    }
    assertTrue(taskIdsFromBAM.contains(task1));
    assertTrue(taskIdsFromBAM.contains(task2));
}
Also used : Status(org.kie.api.task.model.Status) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) BAMTaskEventListener(org.jbpm.services.task.lifecycle.listeners.BAMTaskEventListener) TaskService(org.kie.api.task.TaskService) ArrayList(java.util.ArrayList) EventService(org.kie.internal.task.api.EventService) EntityManager(javax.persistence.EntityManager) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) BAMTaskSummaryImpl(org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl) Test(org.junit.Test)

Aggregations

TaskService (org.kie.api.task.TaskService)129 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)111 KieSession (org.kie.api.runtime.KieSession)79 Test (org.junit.Test)73 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)70 TaskSummary (org.kie.api.task.model.TaskSummary)66 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)53 InternalTaskService (org.kie.internal.task.api.InternalTaskService)52 HashMap (java.util.HashMap)41 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)39 UserTaskService (org.jbpm.services.api.UserTaskService)38 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)34 ArrayList (java.util.ArrayList)17 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)17 TaskNotFoundException (org.jbpm.services.api.TaskNotFoundException)15 PermissionDeniedException (org.jbpm.services.task.exception.PermissionDeniedException)15 Task (org.kie.api.task.model.Task)14 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)12 BZ (qa.tools.ikeeper.annotation.BZ)10 TaskCleanUpProcessEventListener (org.jbpm.services.task.admin.listener.TaskCleanUpProcessEventListener)9