Search in sources :

Example 1 with HumanTaskConfigurator

use of org.jbpm.services.task.HumanTaskConfigurator in project jbpm by kiegroup.

the class HumanTaskServiceProducer method createHumanTaskConfigurator.

protected HumanTaskConfigurator createHumanTaskConfigurator() {
    HumanTaskConfigurator configurator = HumanTaskServiceFactory.newTaskServiceConfigurator();
    configureHumanTaskConfigurator(configurator);
    return configurator;
}
Also used : HumanTaskConfigurator(org.jbpm.services.task.HumanTaskConfigurator)

Example 2 with HumanTaskConfigurator

use of org.jbpm.services.task.HumanTaskConfigurator 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());
}
Also used : UserTransaction(javax.transaction.UserTransaction) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) HumanTaskConfigurator(org.jbpm.services.task.HumanTaskConfigurator) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) ArrayList(java.util.ArrayList) RegisterableItemsFactory(org.kie.api.runtime.manager.RegisterableItemsFactory) CountDownLatch(java.util.concurrent.CountDownLatch) PessimisticLockException(javax.persistence.PessimisticLockException) PessimisticLockException(javax.persistence.PessimisticLockException) EntityManagerFactory(javax.persistence.EntityManagerFactory) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) TaskLifeCycleEventListener(org.kie.api.task.TaskLifeCycleEventListener) Test(org.junit.Test)

Example 3 with HumanTaskConfigurator

use of org.jbpm.services.task.HumanTaskConfigurator in project jbpm by kiegroup.

the class TaskServiceEJBImpl method configureDelegate.

@PostConstruct
public void configureDelegate() {
    UserGroupCallback callback = UserDataServiceProvider.getUserGroupCallback();
    HumanTaskConfigurator configurator = HumanTaskServiceFactory.newTaskServiceConfigurator().entityManagerFactory(emf).userGroupCallback(callback);
    DeploymentDescriptorManager manager = new DeploymentDescriptorManager("org.jbpm.domain");
    DeploymentDescriptor descriptor = manager.getDefaultDescriptor();
    // in case there is descriptor with enabled audit register then by default
    if (!descriptor.getAuditMode().equals(AuditMode.NONE)) {
        JPATaskLifeCycleEventListener listener = new JPATaskLifeCycleEventListener(false);
        BAMTaskEventListener bamListener = new BAMTaskEventListener(false);
        // if the audit persistence unit is different than default for the engine perform proper init
        if (!"org.jbpm.domain".equals(descriptor.getAuditPersistenceUnit())) {
            EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(descriptor.getAuditPersistenceUnit());
            listener = new JPATaskLifeCycleEventListener(emf);
            bamListener = new BAMTaskEventListener(emf);
        }
        configurator.listener(listener);
        configurator.listener(bamListener);
    }
    delegate = (InternalTaskService) configurator.getTaskService();
}
Also used : JPATaskLifeCycleEventListener(org.jbpm.services.task.audit.JPATaskLifeCycleEventListener) HumanTaskConfigurator(org.jbpm.services.task.HumanTaskConfigurator) BAMTaskEventListener(org.jbpm.services.task.lifecycle.listeners.BAMTaskEventListener) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) EntityManagerFactory(javax.persistence.EntityManagerFactory) DeploymentDescriptorManager(org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorManager) UserGroupCallback(org.kie.api.task.UserGroupCallback) PostConstruct(javax.annotation.PostConstruct)

Example 4 with HumanTaskConfigurator

use of org.jbpm.services.task.HumanTaskConfigurator in project jbpm by kiegroup.

the class JBPMHelper method startTaskService.

@Deprecated
public static TaskService startTaskService() {
    Properties properties = getProperties();
    String dialect = properties.getProperty("persistence.persistenceunit.dialect", "org.hibernate.dialect.H2Dialect");
    Map<String, String> map = new HashMap<String, String>();
    map.put("hibernate.dialect", dialect);
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(properties.getProperty("taskservice.datasource.name", "org.jbpm.services.task"), map);
    System.setProperty("jbpm.user.group.mapping", properties.getProperty("taskservice.usergroupmapping", "classpath:/usergroups.properties"));
    TaskService taskService = new HumanTaskConfigurator().entityManagerFactory(emf).userGroupCallback(getUserGroupCallback()).getTaskService();
    return taskService;
}
Also used : HumanTaskConfigurator(org.jbpm.services.task.HumanTaskConfigurator) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) EntityManagerFactory(javax.persistence.EntityManagerFactory) Properties(java.util.Properties)

Example 5 with HumanTaskConfigurator

use of org.jbpm.services.task.HumanTaskConfigurator in project jbpm by kiegroup.

the class LocalTaskServiceFactory method newTaskService.

@Override
public TaskService newTaskService() {
    // all to reuse an already given instance of task service instead of producing new one
    TaskService providedTaskService = (TaskService) ((SimpleRuntimeEnvironment) runtimeEnvironment).getEnvironmentTemplate().get("org.kie.api.task.TaskService");
    if (providedTaskService != null) {
        return providedTaskService;
    }
    EntityManagerFactory emf = ((SimpleRuntimeEnvironment) runtimeEnvironment).getEmf();
    if (emf != null) {
        HumanTaskConfigurator configurator = HumanTaskServiceFactory.newTaskServiceConfigurator().environment(runtimeEnvironment.getEnvironment()).entityManagerFactory(emf).userGroupCallback(runtimeEnvironment.getUserGroupCallback()).userInfo(UserDataServiceProvider.getUserInfo());
        TaskService internalTaskService = configurator.getTaskService();
        return internalTaskService;
    } else {
        return null;
    }
}
Also used : SimpleRuntimeEnvironment(org.jbpm.runtime.manager.impl.SimpleRuntimeEnvironment) HumanTaskConfigurator(org.jbpm.services.task.HumanTaskConfigurator) TaskService(org.kie.api.task.TaskService) EntityManagerFactory(javax.persistence.EntityManagerFactory)

Aggregations

HumanTaskConfigurator (org.jbpm.services.task.HumanTaskConfigurator)5 EntityManagerFactory (javax.persistence.EntityManagerFactory)4 TaskService (org.kie.api.task.TaskService)3 HashMap (java.util.HashMap)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 PostConstruct (javax.annotation.PostConstruct)1 PessimisticLockException (javax.persistence.PessimisticLockException)1 UserTransaction (javax.transaction.UserTransaction)1 SimpleRuntimeEnvironment (org.jbpm.runtime.manager.impl.SimpleRuntimeEnvironment)1 DeploymentDescriptorManager (org.jbpm.runtime.manager.impl.deploy.DeploymentDescriptorManager)1 JPATaskLifeCycleEventListener (org.jbpm.services.task.audit.JPATaskLifeCycleEventListener)1 BAMTaskEventListener (org.jbpm.services.task.lifecycle.listeners.BAMTaskEventListener)1 Test (org.junit.Test)1 KieSession (org.kie.api.runtime.KieSession)1 RegisterableItemsFactory (org.kie.api.runtime.manager.RegisterableItemsFactory)1 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)1 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)1 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)1