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);
}
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());
}
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);
}
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);
}
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));
}
Aggregations