Search in sources :

Example 21 with TaskService

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

the class UserTaskServiceImpl method getCommentById.

@Override
public Comment getCommentById(String deploymentId, Long taskId, Long commentId) {
    UserTaskInstanceDesc task = dataService.getTaskById(taskId);
    validateTask(deploymentId, taskId, task);
    RuntimeManager manager = getRuntimeManager(task);
    if (manager == null) {
        logger.warn("Cannot find runtime manager for task {}", taskId);
        return null;
    }
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(task.getProcessInstanceId()));
    try {
        TaskService taskService = engine.getTaskService();
        // perform actual operation
        return ((InternalTaskService) taskService).getCommentById(commentId);
    } finally {
        disposeRuntimeEngine(manager, engine);
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) InternalTaskService(org.kie.internal.task.api.InternalTaskService) UserTaskService(org.jbpm.services.api.UserTaskService) TaskService(org.kie.api.task.TaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) InternalTaskService(org.kie.internal.task.api.InternalTaskService) UserTaskInstanceDesc(org.jbpm.services.api.model.UserTaskInstanceDesc)

Example 22 with TaskService

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

the class AsyncWorkItemHandlerTest method testRunProcessWithAsyncHandlerRecordExecutionErrorProcessAutoAck.

@Test(timeout = 20000)
public void testRunProcessWithAsyncHandlerRecordExecutionErrorProcessAutoAck() 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();
    taskService.start(taskId, "john");
    Map<String, Object> results = new HashMap<>();
    results.put("output1", "rollback");
    try {
        taskService.complete(taskId, "john", results);
        fail("Complete 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("Process", error.getType());
    assertEquals("UserTaskWithRollback", error.getProcessId());
    assertEquals("Script Task 1", 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(ProcessAutoAckErrorCommand.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();
    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(ProcessAutoAckErrorCommand.class.getName(), ctx);
    countDownListener.waitTillCompleted();
    errors = errorStorage.list(0, 10);
    assertEquals(1, errors.size());
    error = errors.get(0);
    assertNotNull(error);
    assertTrue(error.isAcknowledged());
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) ExecutionError(org.kie.internal.runtime.error.ExecutionError) ExecutorServiceImpl(org.jbpm.executor.impl.ExecutorServiceImpl) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) CommandContext(org.kie.api.executor.CommandContext) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) CountDownAsyncJobListener(org.jbpm.executor.test.CountDownAsyncJobListener) ExecutionErrorManager(org.kie.internal.runtime.error.ExecutionErrorManager) TaskSummary(org.kie.api.task.model.TaskSummary) ExecutionErrorStorage(org.kie.internal.runtime.error.ExecutionErrorStorage) ProcessAutoAckErrorCommand(org.jbpm.executor.commands.error.ProcessAutoAckErrorCommand) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractExecutorBaseTest(org.jbpm.test.util.AbstractExecutorBaseTest) Test(org.junit.Test)

Example 23 with TaskService

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

the class AbstractCaseServicesBaseTest method configureServices.

protected void configureServices() {
    buildDatasource();
    emf = EntityManagerFactoryManager.get().getOrCreate("org.jbpm.domain");
    identityProvider = new TestIdentityProvider();
    authorizationManager = new AuthorizationManagerImpl(identityProvider, new TransactionalCommandService(emf));
    // build definition service
    bpmn2Service = new BPMN2DataServiceImpl();
    DeploymentRolesManager deploymentRolesManager = new DeploymentRolesManager();
    queryService = new QueryServiceImpl();
    ((QueryServiceImpl) queryService).setIdentityProvider(identityProvider);
    ((QueryServiceImpl) queryService).setCommandService(new TransactionalCommandService(emf));
    ((QueryServiceImpl) queryService).init();
    // build deployment service
    deploymentService = new KModuleDeploymentService();
    ((KModuleDeploymentService) deploymentService).setBpmn2Service(bpmn2Service);
    ((KModuleDeploymentService) deploymentService).setEmf(emf);
    ((KModuleDeploymentService) deploymentService).setIdentityProvider(identityProvider);
    ((KModuleDeploymentService) deploymentService).setManagerFactory(new RuntimeManagerFactoryImpl());
    ((KModuleDeploymentService) deploymentService).setFormManagerService(new FormManagerServiceImpl());
    TaskService taskService = HumanTaskServiceFactory.newTaskServiceConfigurator().entityManagerFactory(emf).getTaskService();
    // build runtime data service
    runtimeDataService = new RuntimeDataServiceImpl();
    ((RuntimeDataServiceImpl) runtimeDataService).setCommandService(new TransactionalCommandService(emf));
    ((RuntimeDataServiceImpl) runtimeDataService).setIdentityProvider(identityProvider);
    ((RuntimeDataServiceImpl) runtimeDataService).setTaskService(taskService);
    ((RuntimeDataServiceImpl) runtimeDataService).setDeploymentRolesManager(deploymentRolesManager);
    ((RuntimeDataServiceImpl) runtimeDataService).setTaskAuditService(TaskAuditServiceFactory.newTaskAuditServiceConfigurator().setTaskService(taskService).getTaskAuditService());
    ((KModuleDeploymentService) deploymentService).setRuntimeDataService(runtimeDataService);
    // build process service
    processService = new ProcessServiceImpl();
    ((ProcessServiceImpl) processService).setDataService(runtimeDataService);
    ((ProcessServiceImpl) processService).setDeploymentService(deploymentService);
    // build user task service
    userTaskService = new UserTaskServiceImpl();
    ((UserTaskServiceImpl) userTaskService).setDataService(runtimeDataService);
    ((UserTaskServiceImpl) userTaskService).setDeploymentService(deploymentService);
    // build case id generator
    caseIdGenerator = new TableCaseIdGenerator(new TransactionalCommandService(emf));
    // build case runtime data service
    caseRuntimeDataService = new CaseRuntimeDataServiceImpl();
    ((CaseRuntimeDataServiceImpl) caseRuntimeDataService).setCaseIdGenerator(caseIdGenerator);
    ((CaseRuntimeDataServiceImpl) caseRuntimeDataService).setRuntimeDataService(runtimeDataService);
    ((CaseRuntimeDataServiceImpl) caseRuntimeDataService).setCommandService(new TransactionalCommandService(emf));
    ((CaseRuntimeDataServiceImpl) caseRuntimeDataService).setIdentityProvider(identityProvider);
    ((CaseRuntimeDataServiceImpl) caseRuntimeDataService).setDeploymentRolesManager(deploymentRolesManager);
    // build case service
    caseService = new CaseServiceImpl();
    ((CaseServiceImpl) caseService).setCaseIdGenerator(caseIdGenerator);
    ((CaseServiceImpl) caseService).setCaseRuntimeDataService(caseRuntimeDataService);
    ((CaseServiceImpl) caseService).setProcessService(processService);
    ((CaseServiceImpl) caseService).setDeploymentService(deploymentService);
    ((CaseServiceImpl) caseService).setRuntimeDataService(runtimeDataService);
    ((CaseServiceImpl) caseService).setCommandService(new TransactionalCommandService(emf));
    ((CaseServiceImpl) caseService).setAuthorizationManager(authorizationManager);
    ((CaseServiceImpl) caseService).setIdentityProvider(identityProvider);
    CaseConfigurationDeploymentListener configurationListener = new CaseConfigurationDeploymentListener(identityProvider);
    // set runtime data service as listener on deployment service
    ((KModuleDeploymentService) deploymentService).addListener((RuntimeDataServiceImpl) runtimeDataService);
    ((KModuleDeploymentService) deploymentService).addListener((BPMN2DataServiceImpl) bpmn2Service);
    ((KModuleDeploymentService) deploymentService).addListener((QueryServiceImpl) queryService);
    ((KModuleDeploymentService) deploymentService).addListener((CaseRuntimeDataServiceImpl) caseRuntimeDataService);
    ((KModuleDeploymentService) deploymentService).addListener(configurationListener);
}
Also used : FormManagerServiceImpl(org.jbpm.kie.services.impl.FormManagerServiceImpl) BPMN2DataServiceImpl(org.jbpm.kie.services.impl.bpmn2.BPMN2DataServiceImpl) RuntimeManagerFactoryImpl(org.jbpm.runtime.manager.impl.RuntimeManagerFactoryImpl) CaseConfigurationDeploymentListener(org.jbpm.casemgmt.impl.event.CaseConfigurationDeploymentListener) UserTaskService(org.jbpm.services.api.UserTaskService) TaskService(org.kie.api.task.TaskService) UserTaskServiceImpl(org.jbpm.kie.services.impl.UserTaskServiceImpl) TableCaseIdGenerator(org.jbpm.casemgmt.impl.generator.TableCaseIdGenerator) DeploymentRolesManager(org.jbpm.kie.services.impl.security.DeploymentRolesManager) KModuleDeploymentService(org.jbpm.kie.services.impl.KModuleDeploymentService) ProcessServiceImpl(org.jbpm.kie.services.impl.ProcessServiceImpl) RuntimeDataServiceImpl(org.jbpm.kie.services.impl.RuntimeDataServiceImpl) CaseRuntimeDataServiceImpl(org.jbpm.casemgmt.impl.CaseRuntimeDataServiceImpl) TransactionalCommandService(org.jbpm.shared.services.impl.TransactionalCommandService) CaseServiceImpl(org.jbpm.casemgmt.impl.CaseServiceImpl) CaseRuntimeDataServiceImpl(org.jbpm.casemgmt.impl.CaseRuntimeDataServiceImpl) QueryServiceImpl(org.jbpm.kie.services.impl.query.QueryServiceImpl) AuthorizationManagerImpl(org.jbpm.casemgmt.impl.AuthorizationManagerImpl)

Example 24 with TaskService

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

the class SingletonRuntimeManagerTest method testInterceptorAfterRollback.

@Test
public void testInterceptorAfterRollback() throws Exception {
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTaskWithRollback.bpmn2"), ResourceType.BPMN2).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    ProcessInstance processInstance = ksession.startProcess("UserTaskWithRollback");
    ExecutableRunner commandService = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
    assertEquals(PersistableRunner.class, commandService.getClass());
    ChainableRunner internalCommandService = ((PersistableRunner) commandService).getChainableRunner();
    assertEquals(ExecutionErrorHandlerInterceptor.class, internalCommandService.getClass());
    internalCommandService = (ChainableRunner) ((ExecutionErrorHandlerInterceptor) internalCommandService).getNext();
    assertEquals(TransactionLockInterceptor.class, internalCommandService.getClass());
    TaskService taskService = runtime.getTaskService();
    List<Long> taskIds = taskService.getTasksByProcessInstanceId(processInstance.getId());
    taskService.start(taskIds.get(0), "john");
    HashMap<String, Object> result = new HashMap<String, Object>();
    result.put("output1", "rollback");
    try {
        // rollback transaction
        taskService.complete(taskIds.get(0), "john", result);
    } catch (WorkflowRuntimeException e) {
    // ignore
    }
    result = new HashMap<String, Object>();
    result.put("output1", "ok");
    // this time, execute normally
    taskService.complete(taskIds.get(0), "john", result);
    internalCommandService = ((PersistableRunner) commandService).getChainableRunner();
    assertEquals(ExecutionErrorHandlerInterceptor.class, internalCommandService.getClass());
    internalCommandService = (ChainableRunner) ((ExecutionErrorHandlerInterceptor) internalCommandService).getNext();
    assertEquals(TransactionLockInterceptor.class, internalCommandService.getClass());
    internalCommandService = (ChainableRunner) ((TransactionLockInterceptor) internalCommandService).getNext();
    assertEquals(OptimisticLockRetryInterceptor.class, internalCommandService.getClass());
    internalCommandService = (ChainableRunner) ((OptimisticLockRetryInterceptor) internalCommandService).getNext();
    assertEquals("org.drools.persistence.PersistableRunner$TransactionInterceptor", internalCommandService.getClass().getName());
    // close manager which will close session maintained by the manager
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) PersistableRunner(org.drools.persistence.PersistableRunner) ChainableRunner(org.drools.core.runtime.ChainableRunner) ExecutionErrorHandlerInterceptor(org.jbpm.runtime.manager.impl.error.ExecutionErrorHandlerInterceptor) OptimisticLockRetryInterceptor(org.drools.persistence.jpa.OptimisticLockRetryInterceptor) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) TransactionLockInterceptor(org.drools.persistence.jta.TransactionLockInterceptor) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 25 with TaskService

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

the class TimerInitFailureRuntimeManagerTest method testPerCaseRuntimeManager.

@Test(timeout = 10000)
@BMScript(value = "byteman-scripts/failOnRuntimeManagerInitRules.btm")
public void testPerCaseRuntimeManager() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Intermediate Catch Event 1", 1);
    RuntimeEnvironment environment = createEnvironment(countDownListener);
    manager = RuntimeManagerFactory.Factory.get().newPerCaseRuntimeManager(environment, "first");
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(CaseContext.get("CASE-001"));
    KieSession ksession = runtime.getKieSession();
    // start a new process instance
    Map<String, Object> params = new HashMap<>();
    ProcessInstance pi = ksession.startProcess("TimerInitFailure", params);
    assertEquals(ProcessInstance.STATE_ACTIVE, pi.getState());
    manager.disposeRuntimeEngine(runtime);
    countDownListener.waitTillCompleted();
    // User access
    runtime = manager.getRuntimeEngine(CaseContext.get("CASE-001"));
    runtime.getKieSession();
    TaskService taskService = runtime.getTaskService();
    List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertEquals(1, list.size());
    long taskId = list.get(0).getId();
    taskService.start(taskId, "john");
    taskService.complete(taskId, "john", null);
    manager.disposeRuntimeEngine(runtime);
    runtime = manager.getRuntimeEngine(CaseContext.get("CASE-001"));
    AuditService auditService = runtime.getAuditService();
    ProcessInstanceLog log = auditService.findProcessInstance(pi.getId());
    assertEquals(ProcessInstance.STATE_COMPLETED, log.getStatus().intValue());
    auditService.dispose();
    manager.disposeRuntimeEngine(runtime);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AuditService(org.kie.api.runtime.manager.audit.AuditService) ProcessInstanceLog(org.kie.api.runtime.manager.audit.ProcessInstanceLog) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) BMScript(org.jboss.byteman.contrib.bmunit.BMScript)

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