use of org.kie.api.task.TaskService 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();
}
use of org.kie.api.task.TaskService in project jbpm by kiegroup.
the class AsyncWorkItemHandlerTest method testRunProcessWithAsyncHandlerRecordExecutionErrorTaskAutoAck.
@SuppressWarnings("unchecked")
@Test(timeout = 20000)
public void testRunProcessWithAsyncHandlerRecordExecutionErrorTaskAutoAck() throws Exception {
CountDownAsyncJobListener countDownListener = new CountDownAsyncJobListener(1);
((ExecutorServiceImpl) executorService).addAsyncJobListener(countDownListener);
((ExecutorServiceImpl) executorService).setRetries(0);
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTaskWithRollback.bpmn2"), ResourceType.BPMN2).get();
manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
assertNotNull(manager);
RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);
ProcessInstance processInstance = ksession.startProcess("UserTaskWithRollback");
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
processInstance = runtime.getKieSession().getProcessInstance(processInstance.getId());
assertNotNull(processInstance);
manager.disposeRuntimeEngine(runtime);
runtime = manager.getRuntimeEngine(EmptyContext.get());
TaskService taskService = runtime.getTaskService();
List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
assertEquals(1, tasks.size());
long taskId = tasks.get(0).getId();
TaskLifeCycleEventListener listener = new DefaultTaskEventListener() {
@Override
public void afterTaskStartedEvent(TaskEvent event) {
throw new TaskExecutionException("On purpose");
}
};
try {
((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(listener);
taskService.start(taskId, "john");
fail("Start task should fail due to broken script");
} catch (Throwable e) {
// expected
}
manager.disposeRuntimeEngine(runtime);
ExecutionErrorManager errorManager = ((AbstractRuntimeManager) manager).getExecutionErrorManager();
assertNotNull("ErrorManager is null", errorManager);
ExecutionErrorStorage errorStorage = errorManager.getStorage();
assertNotNull("ErrorStorage is null", errorStorage);
List<ExecutionError> errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());
ExecutionError error = errors.get(0);
assertNotNull(error);
assertEquals("Task", error.getType());
assertEquals("UserTaskWithRollback", error.getProcessId());
assertEquals("Hello", error.getActivityName());
assertEquals(manager.getIdentifier(), error.getDeploymentId());
assertNotNull(error.getError());
assertNotNull(error.getErrorMessage());
assertNotNull(error.getActivityId());
assertNotNull(error.getProcessInstanceId());
assertNull(error.getAcknowledgedAt());
assertNull(error.getAcknowledgedBy());
assertFalse(error.isAcknowledged());
countDownListener.reset(1);
// first run should not ack the job as it's in error state
CommandContext ctx = new CommandContext();
ctx.setData("SingleRun", "true");
ctx.setData("EmfName", "org.jbpm.persistence.complete");
executorService.scheduleRequest(TaskAutoAckErrorCommand.class.getName(), ctx);
countDownListener.waitTillCompleted();
errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());
error = errors.get(0);
assertNotNull(error);
assertFalse(error.isAcknowledged());
runtime = manager.getRuntimeEngine(EmptyContext.get());
tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
assertEquals(1, tasks.size());
taskId = tasks.get(0).getId();
((EventService<TaskLifeCycleEventListener>) taskService).removeTaskEventListener(listener);
taskService.start(taskId, "john");
Map<String, Object> results = new HashMap<>();
results.put("output1", "ok");
taskService.complete(taskId, "john", results);
manager.disposeRuntimeEngine(runtime);
countDownListener.reset(1);
// since task was completed auto ack should work
executorService.scheduleRequest(TaskAutoAckErrorCommand.class.getName(), ctx);
countDownListener.waitTillCompleted();
errors = errorStorage.list(0, 10);
assertEquals(1, errors.size());
error = errors.get(0);
assertNotNull(error);
assertTrue(error.isAcknowledged());
}
use of org.kie.api.task.TaskService in project jbpm by kiegroup.
the class CallActivityWithReadOnlyProcessInstanceTest method testCallActivityWithReadOnlyProcessInstance.
@Test
public void testCallActivityWithReadOnlyProcessInstance() throws Exception {
// JBPM-6434
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/CallActivityWithVar-Main.bpmn2", "org/jbpm/test/functional/subprocess/CallActivityWithVar-Sub.bpmn2");
RuntimeEngine runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get());
KieSession ksession = runtimeEngine.getKieSession();
TaskService taskService = runtimeEngine.getTaskService();
ut.begin();
ProcessInstance processInstance = ksession.startProcess("helloMain", null);
long parentProcessInstanceId = processInstance.getId();
ut.commit();
manager.disposeRuntimeEngine(runtimeEngine);
runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(parentProcessInstanceId));
ksession = runtimeEngine.getKieSession();
taskService = runtimeEngine.getTaskService();
ut.begin();
WorkflowProcessInstance readOnlyParentProcessInstance = (WorkflowProcessInstance) ksession.getProcessInstance(parentProcessInstanceId, true);
assertEquals(0, readOnlyParentProcessInstance.getVariable("Var1"));
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);
ut.commit();
manager.disposeRuntimeEngine(runtimeEngine);
runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(processInstance.getId()));
ksession = runtimeEngine.getKieSession();
taskService = runtimeEngine.getTaskService();
ut.begin();
readOnlyParentProcessInstance = (WorkflowProcessInstance) ksession.getProcessInstance(parentProcessInstanceId, true);
assertEquals(1, readOnlyParentProcessInstance.getVariable("Var1"));
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);
ut.commit();
ut.begin();
assertProcessInstanceCompleted(parentProcessInstanceId);
ut.commit();
manager.disposeRuntimeEngine(runtimeEngine);
// avoid unnecessary WARN for PerRequest
activeEngines.clear();
}
use of org.kie.api.task.TaskService in project jbpm by kiegroup.
the class AdminAPIsWithListenerTest method automaticCleanUpForSubProcessWithSingletonStrategy.
@Test
public void automaticCleanUpForSubProcessWithSingletonStrategy() throws Exception {
TaskCleanUpProcessEventListener taskCleanUpProcessEventListener = new TaskCleanUpProcessEventListener(null);
this.addProcessEventListener(taskCleanUpProcessEventListener);
RuntimeManager manager = createRuntimeManager("org/jbpm/test/functional/task/ht-main.bpmn", "org/jbpm/test/functional/task/ht-sub.bpmn");
RuntimeEngine runtime = getRuntimeEngine(ProcessInstanceIdContext.get());
taskCleanUpProcessEventListener.setTaskService((InternalTaskService) runtime.getTaskService());
KieSession ksession = runtime.getKieSession();
// start a new process instance
Map<String, Object> params = new HashMap<String, Object>();
ProcessInstance pi = ksession.startProcess("com.mycompany.sample", params);
// obtain the task service
TaskService taskService = runtime.getTaskService();
List<TaskSummary> tasks1 = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
assertFalse(tasks1.isEmpty());
TaskSummary task1 = tasks1.get(0);
System.out.println("Sales-rep executing task " + task1.getName() + "(" + task1.getId() + ": " + task1.getDescription() + ")");
taskService.start(task1.getId(), "john");
Map<String, Object> results = new HashMap<String, Object>();
results.put("comment", "Agreed, existing laptop needs replacing");
results.put("outcome", "Accept");
// complete the human task of the main process
taskService.complete(task1.getId(), "john", results);
// abort the process instance
ksession.abortProcessInstance(pi.getId());
// main process instance shall be aborted
assertProcessInstanceAborted(pi.getId());
AuditService logService = runtime.getAuditService();
List<? extends ProcessInstanceLog> logs = logService.findProcessInstances("com.mycompany.sample");
assertNotNull(logs);
assertEquals(1, logs.size());
assertEquals(ProcessInstance.STATE_ABORTED, logs.get(0).getStatus().intValue());
logs = logService.findProcessInstances("com.mycompany.sample.subprocess");
assertNotNull(logs);
assertEquals(1, logs.size());
assertEquals(ProcessInstance.STATE_ABORTED, logs.get(0).getStatus().intValue());
manager.close();
final EntityManager em = emfTasks.createEntityManager();
assertEquals(0, em.createQuery("select t from TaskImpl t").getResultList().size());
assertEquals(0, em.createQuery("select i from I18NTextImpl i").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_BAs").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_ExclOwners").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_PotOwners").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_Recipients").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_Stakeholders").getResultList().size());
assertEquals(0, em.createQuery("select c from ContentImpl c").getResultList().size());
em.close();
}
use of org.kie.api.task.TaskService in project jbpm by kiegroup.
the class AdminAPIsWithListenerTest method automaticCleanUpTestAbortProcess.
@Test
public void automaticCleanUpTestAbortProcess() throws Exception {
createRuntimeManager("org/jbpm/test/functional/task/patient-appointment.bpmn");
RuntimeEngine runtimeEngine = getRuntimeEngine();
KieSession ksession = runtimeEngine.getKieSession();
TaskService taskService = runtimeEngine.getTaskService();
KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
ksession.addEventListener(new TaskCleanUpProcessEventListener(taskService));
logger.info("### Starting process ###");
Map<String, Object> parameters = new HashMap<String, Object>();
ProcessInstance process = ksession.startProcess("org.jbpm.PatientAppointment", parameters);
long processInstanceId = process.getId();
// The process is in the first Human Task waiting for its completion
assertEquals(ProcessInstance.STATE_ACTIVE, process.getState());
// gets frontDesk's tasks
List<TaskSummary> frontDeskTasks = taskService.getTasksAssignedAsPotentialOwner("frontDesk", "en-UK");
assertEquals(1, frontDeskTasks.size());
// doctor doesn't have any task
List<TaskSummary> doctorTasks = taskService.getTasksAssignedAsPotentialOwner("doctor", "en-UK");
Assert.assertTrue(doctorTasks.isEmpty());
// manager doesn't have any task
List<TaskSummary> managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
Assert.assertTrue(managerTasks.isEmpty());
taskService.start(frontDeskTasks.get(0).getId(), "frontDesk");
taskService.complete(frontDeskTasks.get(0).getId(), "frontDesk", null);
// Now doctor has 1 task
doctorTasks = taskService.getTasksAssignedAsPotentialOwner("doctor", "en-UK");
assertEquals(1, doctorTasks.size());
// No tasks for manager yet
managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
Assert.assertTrue(managerTasks.isEmpty());
taskService.start(doctorTasks.get(0).getId(), "doctor");
taskService.complete(doctorTasks.get(0).getId(), "doctor", null);
// abort process instance
ksession.abortProcessInstance(processInstanceId);
// since persisted process instance is completed it should be null
process = ksession.getProcessInstance(process.getId());
Assert.assertNull(process);
final EntityManager em = emfTasks.createEntityManager();
assertEquals(0, em.createQuery("select t from TaskImpl t").getResultList().size());
assertEquals(0, em.createQuery("select i from I18NTextImpl i").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_BAs").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_ExclOwners").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_PotOwners").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_Recipients").getResultList().size());
assertEquals(0, em.createNativeQuery("select * from PeopleAssignments_Stakeholders").getResultList().size());
assertEquals(0, em.createQuery("select c from ContentImpl c").getResultList().size());
em.close();
}
Aggregations