use of org.kie.api.task.TaskLifeCycleEventListener in project jbpm by kiegroup.
the class TaskEventSupport method fireBeforeTaskAssignmentsRemovedEvent.
public void fireBeforeTaskAssignmentsRemovedEvent(final Task task, TaskContext context, AssignmentType type, List<OrganizationalEntity> entities) {
final Iterator<TaskLifeCycleEventListener> iter = getEventListenersIterator();
if (iter.hasNext()) {
do {
TaskLifeCycleEventListener listener = iter.next();
listener.beforeTaskAssignmentsRemovedEvent(new TaskEventImpl(task, context), type, entities);
} while (iter.hasNext());
}
}
use of org.kie.api.task.TaskLifeCycleEventListener in project jbpm by kiegroup.
the class JbpmJUnitBaseTestCase method createRuntimeManager.
/**
* Creates default configuration of <code>RuntimeManager</code> with given <code>strategy</code> and all
* <code>resources</code> being added to knowledge base.
* <br/>
* There should be only one <code>RuntimeManager</code> created during single test.
* @param strategy - selected strategy of those that are supported
* @param resources - resources that shall be added to knowledge base
* @param identifier - identifies the runtime manager
* @return new instance of RuntimeManager
*/
protected RuntimeManager createRuntimeManager(Strategy strategy, Map<String, ResourceType> resources, String identifier) {
if (manager != null) {
throw new IllegalStateException("There is already one RuntimeManager active");
}
RuntimeEnvironmentBuilder builder = null;
if (!setupDataSource) {
builder = RuntimeEnvironmentBuilder.Factory.get().newEmptyBuilder().addConfiguration("drools.processSignalManagerFactory", DefaultSignalManagerFactory.class.getName()).addConfiguration("drools.processInstanceManagerFactory", DefaultProcessInstanceManagerFactory.class.getName()).registerableItemsFactory(new SimpleRegisterableItemsFactory() {
@Override
public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
Map<String, WorkItemHandler> handlers = new HashMap<String, WorkItemHandler>();
handlers.putAll(super.getWorkItemHandlers(runtime));
handlers.putAll(customHandlers);
return handlers;
}
@Override
public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
listeners.addAll(customProcessListeners);
return listeners;
}
@Override
public List<AgendaEventListener> getAgendaEventListeners(RuntimeEngine runtime) {
List<AgendaEventListener> listeners = super.getAgendaEventListeners(runtime);
listeners.addAll(customAgendaListeners);
return listeners;
}
@Override
public List<TaskLifeCycleEventListener> getTaskListeners() {
List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
listeners.addAll(customTaskListeners);
return listeners;
}
});
} else if (sessionPersistence) {
builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).registerableItemsFactory(new DefaultRegisterableItemsFactory() {
@Override
public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
Map<String, WorkItemHandler> handlers = new HashMap<String, WorkItemHandler>();
handlers.putAll(super.getWorkItemHandlers(runtime));
handlers.putAll(customHandlers);
return handlers;
}
@Override
public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
listeners.addAll(customProcessListeners);
return listeners;
}
@Override
public List<AgendaEventListener> getAgendaEventListeners(RuntimeEngine runtime) {
List<AgendaEventListener> listeners = super.getAgendaEventListeners(runtime);
listeners.addAll(customAgendaListeners);
return listeners;
}
@Override
public List<TaskLifeCycleEventListener> getTaskListeners() {
List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
listeners.addAll(customTaskListeners);
return listeners;
}
});
} else {
builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().entityManagerFactory(emf).registerableItemsFactory(new DefaultRegisterableItemsFactory() {
@Override
public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
Map<String, WorkItemHandler> handlers = new HashMap<String, WorkItemHandler>();
handlers.putAll(super.getWorkItemHandlers(runtime));
handlers.putAll(customHandlers);
return handlers;
}
@Override
public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
listeners.addAll(customProcessListeners);
return listeners;
}
@Override
public List<AgendaEventListener> getAgendaEventListeners(RuntimeEngine runtime) {
List<AgendaEventListener> listeners = super.getAgendaEventListeners(runtime);
listeners.addAll(customAgendaListeners);
return listeners;
}
@Override
public List<TaskLifeCycleEventListener> getTaskListeners() {
List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
listeners.addAll(customTaskListeners);
return listeners;
}
});
}
builder.userGroupCallback(userGroupCallback);
for (Entry<String, Object> envEntry : customEnvironmentEntries.entrySet()) {
builder.addEnvironmentEntry(envEntry.getKey(), envEntry.getValue());
}
for (Map.Entry<String, ResourceType> entry : resources.entrySet()) {
builder.addAsset(ResourceFactory.newClassPathResource(entry.getKey()), entry.getValue());
}
return createRuntimeManager(strategy, resources, builder.get(), identifier);
}
use of org.kie.api.task.TaskLifeCycleEventListener 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.TaskLifeCycleEventListener in project jbpm by kiegroup.
the class HTWorkItemHandlerBaseTest method testTaskCreateFailedWithLog.
@SuppressWarnings("unchecked")
@Test
public void testTaskCreateFailedWithLog() throws Exception {
TestWorkItemManager manager = new TestWorkItemManager();
((AbstractHTWorkItemHandler) handler).setAction(OnErrorAction.LOG);
TaskLifeCycleEventListener listener = new AddedTaskListener(true);
((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(listener);
ksession.setWorkItemManager(manager);
WorkItemImpl workItem = new WorkItemImpl();
workItem.setName("Human Task");
workItem.setParameter("NodeName", "TaskName");
workItem.setParameter("Comment", "Comment");
workItem.setParameter("Priority", "10");
workItem.setParameter("ActorId", "DoesNotExist");
workItem.setProcessInstanceId(10);
handler.executeWorkItem(workItem, manager);
assertFalse(manager.isAborted());
((EventService<TaskLifeCycleEventListener>) taskService).removeTaskEventListener(listener);
}
use of org.kie.api.task.TaskLifeCycleEventListener in project jbpm by kiegroup.
the class HTWorkItemHandlerBaseTest method testTaskCreateFailedWithAbort.
@SuppressWarnings("unchecked")
@Test
public void testTaskCreateFailedWithAbort() throws Exception {
TestWorkItemManager manager = new TestWorkItemManager();
TaskLifeCycleEventListener listener = new AddedTaskListener(true);
((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(listener);
((AbstractHTWorkItemHandler) handler).setAction(OnErrorAction.ABORT);
ksession.setWorkItemManager(manager);
WorkItemImpl workItem = new WorkItemImpl();
workItem.setName("Human Task");
workItem.setParameter("NodeName", "TaskName");
workItem.setParameter("Comment", "Comment");
workItem.setParameter("Priority", "10");
workItem.setParameter("ActorId", "DoesNotExist");
workItem.setProcessInstanceId(10);
handler.executeWorkItem(workItem, manager);
assertTrue(manager.isAborted());
((EventService<TaskLifeCycleEventListener>) taskService).removeTaskEventListener(listener);
}
Aggregations