Search in sources :

Example 1 with DefaultTaskEventListener

use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManagerTest method testCreationOfSessionWithCustomTaskListener.

@Test
public void testCreationOfSessionWithCustomTaskListener() {
    final List<Long> addedTasks = new ArrayList<Long>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().userGroupCallback(userGroupCallback).entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public List<TaskLifeCycleEventListener> getTaskListeners() {
            List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
            listeners.add(new DefaultTaskEventListener() {

                @Override
                public void afterTaskAddedEvent(TaskEvent event) {
                    addedTasks.add(event.getTask().getId());
                }
            });
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long ksession1Id = ksession.getIdentifier();
    assertTrue(ksession1Id == 1);
    // FIXME quick hack to overcome problems with same pi ids when not using persistence
    ksession.startProcess("ScriptTask");
    // ksession for process instance #2
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession2 = runtime2.getKieSession();
    assertNotNull(ksession2);
    long ksession2Id = ksession2.getIdentifier();
    assertTrue(ksession2Id == 2);
    ProcessInstance pi1 = ksession.startProcess("UserTask");
    ProcessInstance pi2 = ksession2.startProcess("UserTask");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    assertEquals(ProcessInstance.STATE_ACTIVE, pi2.getState());
    runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId()));
    ksession = runtime.getKieSession();
    assertEquals(ksession1Id, ksession.getIdentifier());
    runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi2.getId()));
    ksession2 = runtime2.getKieSession();
    assertEquals(ksession2Id, ksession2.getIdentifier());
    assertEquals(2, addedTasks.size());
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ArrayList(java.util.ArrayList) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) TaskEvent(org.kie.api.task.TaskEvent) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) JPATaskLifeCycleEventListener(org.jbpm.services.task.audit.JPATaskLifeCycleEventListener) TaskLifeCycleEventListener(org.kie.api.task.TaskLifeCycleEventListener) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 2 with DefaultTaskEventListener

use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.

the class ExecutionErrorHandlingRuntimeManagerTest method testUserTaskFailure.

@SuppressWarnings("unchecked")
@Test
public void testUserTaskFailure() {
    RuntimeEngine runtime1 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession1 = runtime1.getKieSession();
    assertNotNull(ksession1);
    ksession1.startProcess("UserTaskWithRollback");
    TaskService taskService = runtime1.getTaskService();
    List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertEquals(1, tasks.size());
    long taskId = tasks.get(0).getId();
    try {
        ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {

            @Override
            public void afterTaskStartedEvent(TaskEvent event) {
                throw new TaskExecutionException("On purpose");
            }
        });
        taskService.start(taskId, "john");
        fail("Start task should fail due to broken script");
    } catch (Throwable e) {
    // expected
    }
    manager.disposeRuntimeEngine(runtime1);
    ExecutionErrorManager errorManager = ((AbstractRuntimeManager) manager).getExecutionErrorManager();
    ExecutionErrorStorage storage = errorManager.getStorage();
    List<ExecutionError> errors = storage.list(0, 10);
    assertNotNull(errors);
    assertEquals(1, errors.size());
    assertExecutionError(errors.get(0), "Task", "UserTaskWithRollback", "Hello");
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) ExecutionError(org.kie.internal.runtime.error.ExecutionError) TaskService(org.kie.api.task.TaskService) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) TaskExecutionException(org.jbpm.services.task.exception.TaskExecutionException) ExecutionErrorManager(org.kie.internal.runtime.error.ExecutionErrorManager) TaskSummary(org.kie.api.task.model.TaskSummary) TaskEvent(org.kie.api.task.TaskEvent) ExecutionErrorStorage(org.kie.internal.runtime.error.ExecutionErrorStorage) KieSession(org.kie.api.runtime.KieSession) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 3 with DefaultTaskEventListener

use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.

the class LifeCycleBaseTest method testCompleteWithMergeOfResults.

@Test
public void testCompleteWithMergeOfResults() {
    final Map<String, Object> outputsAfterCompletion = new HashMap<String, Object>();
    // One potential owner, should go straight to state Reserved
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet'), new User('Darth Vader') ], businessAdministrators = [ new User('Administrator') ],}),";
    str += "name = 'This is my task name' })";
    ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {

        @Override
        public void afterTaskCompletedEvent(TaskEvent event) {
            Map<String, Object> outs = event.getTask().getTaskData().getTaskOutputVariables();
            if (outs != null) {
                outputsAfterCompletion.putAll(outs);
            }
        }
    });
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    taskService.start(taskId, "Darth Vader");
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    task1 = taskService.getTaskById(taskId);
    Map<String, Object> outputs = getTaskOutput(task1);
    assertEquals(0, outputs.size());
    taskService.complete(taskId, "Darth Vader", null);
    task1 = taskService.getTaskById(taskId);
    assertEquals(Status.Completed, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    outputs = getTaskOutput(task1);
    assertEquals(0, outputs.size());
    // now let's check what was actually given to listeners
    assertEquals(0, outputsAfterCompletion.size());
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) HashMap(java.util.HashMap) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) TaskEvent(org.kie.api.task.TaskEvent) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with DefaultTaskEventListener

use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.

the class LifeCycleBaseTest method testCompleteWithMergeOfResultsNoDataBeforeCompletion.

@Test
public void testCompleteWithMergeOfResultsNoDataBeforeCompletion() {
    final Map<String, Object> outputsAfterCompletion = new HashMap<String, Object>();
    // One potential owner, should go straight to state Reserved
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet'), new User('Darth Vader') ], businessAdministrators = [ new User('Administrator') ],}),";
    str += "name = 'This is my task name' })";
    ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {

        @Override
        public void afterTaskCompletedEvent(TaskEvent event) {
            outputsAfterCompletion.putAll(event.getTask().getTaskData().getTaskOutputVariables());
        }
    });
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    taskService.start(taskId, "Darth Vader");
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    task1 = taskService.getTaskById(taskId);
    Map<String, Object> outputs = getTaskOutput(task1);
    assertEquals(0, outputs.size());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("response", "let's do it");
    params.put("feedback", "ok");
    taskService.complete(taskId, "Darth Vader", params);
    task1 = taskService.getTaskById(taskId);
    assertEquals(Status.Completed, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    outputs = getTaskOutput(task1);
    assertEquals(2, outputs.size());
    assertEquals("let's do it", outputs.get("response"));
    assertEquals("ok", outputs.get("feedback"));
    // now let's check what was actually given to listeners
    assertEquals(2, outputsAfterCompletion.size());
    assertEquals("let's do it", outputsAfterCompletion.get("response"));
    assertEquals("ok", outputsAfterCompletion.get("feedback"));
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) HashMap(java.util.HashMap) TaskEvent(org.kie.api.task.TaskEvent) StringReader(java.io.StringReader) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) Test(org.junit.Test)

Example 5 with DefaultTaskEventListener

use of org.jbpm.services.task.events.DefaultTaskEventListener in project jbpm by kiegroup.

the class LifeCycleBaseTest method testCompleteWithMergeOfResultsOverrideAndAddAtCompletion.

@Test
public void testCompleteWithMergeOfResultsOverrideAndAddAtCompletion() {
    final Map<String, Object> outputsAfterCompletion = new HashMap<String, Object>();
    // One potential owner, should go straight to state Reserved
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet'), new User('Darth Vader') ], businessAdministrators = [ new User('Administrator') ],}),";
    str += "name = 'This is my task name' })";
    ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {

        @Override
        public void afterTaskCompletedEvent(TaskEvent event) {
            outputsAfterCompletion.putAll(event.getTask().getTaskData().getTaskOutputVariables());
        }
    });
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    taskService.start(taskId, "Darth Vader");
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("response", "not sure");
    taskService.addContent(taskId, params);
    task1 = taskService.getTaskById(taskId);
    Map<String, Object> outputs = getTaskOutput(task1);
    assertEquals(1, outputs.size());
    assertEquals("not sure", outputs.get("response"));
    params.clear();
    params.put("response", "let's do it");
    params.put("feedback", "ok");
    taskService.complete(taskId, "Darth Vader", params);
    task1 = taskService.getTaskById(taskId);
    assertEquals(Status.Completed, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    outputs = getTaskOutput(task1);
    assertEquals(2, outputs.size());
    assertEquals("let's do it", outputs.get("response"));
    assertEquals("ok", outputs.get("feedback"));
    // now let's check what was actually given to listeners
    assertEquals(2, outputsAfterCompletion.size());
    assertEquals("let's do it", outputsAfterCompletion.get("response"));
    assertEquals("ok", outputsAfterCompletion.get("feedback"));
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) HashMap(java.util.HashMap) TaskEvent(org.kie.api.task.TaskEvent) StringReader(java.io.StringReader) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) Test(org.junit.Test)

Aggregations

DefaultTaskEventListener (org.jbpm.services.task.events.DefaultTaskEventListener)8 Test (org.junit.Test)8 TaskEvent (org.kie.api.task.TaskEvent)8 EventService (org.kie.internal.task.api.EventService)7 HashMap (java.util.HashMap)6 StringReader (java.io.StringReader)5 Task (org.kie.api.task.model.Task)5 InternalTask (org.kie.internal.task.api.model.InternalTask)5 KieSession (org.kie.api.runtime.KieSession)3 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)3 AbstractRuntimeManager (org.jbpm.runtime.manager.impl.AbstractRuntimeManager)2 TaskExecutionException (org.jbpm.services.task.exception.TaskExecutionException)2 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)2 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 TaskLifeCycleEventListener (org.kie.api.task.TaskLifeCycleEventListener)2 TaskService (org.kie.api.task.TaskService)2 TaskSummary (org.kie.api.task.model.TaskSummary)2 ExecutionError (org.kie.internal.runtime.error.ExecutionError)2 ExecutionErrorManager (org.kie.internal.runtime.error.ExecutionErrorManager)2