Search in sources :

Example 1 with ContextMappingInfo

use of org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo in project jbpm by kiegroup.

the class CaseServiceImplTest method testPerCaseEnsureCleanupInDisposeEngine.

@Test
public void testPerCaseEnsureCleanupInDisposeEngine() throws InterruptedException {
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    roleAssignments.put("owner", new UserImpl("john"));
    Map<String, Object> data = new HashMap<>();
    data.put("s", "description");
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID, data, roleAssignments);
    CaseDefinition caseDef = caseRuntimeDataService.getCase(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID);
    assertNotNull(caseDef);
    assertEquals(deploymentUnit.getIdentifier(), caseDef.getDeploymentId());
    assertEquals(3, caseDef.getAdHocFragments().size());
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID, caseFile);
    Collection<ProcessInstanceDesc> desc = runtimeDataService.getProcessInstancesByDeploymentId(deploymentUnit.getIdentifier(), Arrays.asList(0, 1, 2, 3, 4), new QueryContext());
    List<Long> ids = desc.stream().map(ProcessInstanceDesc::getId).collect(Collectors.toList());
    Long pid = ids.get(0);
    RuntimeManager manager = RuntimeManagerRegistry.get().getManager(deploymentUnit.getIdentifier());
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pid));
    // no op so the runtime is not being init when lazy loading is used
    CountDownLatch latch = new CountDownLatch(1);
    manager.disposeRuntimeEngine(engine);
    try {
        Thread executor = new Thread(() -> {
            RuntimeEngine localEngine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pid));
            manager.disposeRuntimeEngine(localEngine);
            latch.countDown();
        });
        executor.start();
        latch.await(1000, TimeUnit.MILLISECONDS);
        assertEquals(0, latch.getCount());
    } finally {
        caseService.cancelCase(caseId);
    }
    logger.info("cancelled");
    EntityManager em = this.emf.createEntityManager();
    List<ContextMappingInfo> contextMappingInfo = em.createQuery("SELECT o FROM ContextMappingInfo o", ContextMappingInfo.class).getResultList();
    logger.info("ContextMappingInfo found {}", contextMappingInfo.stream().map(ContextMappingInfo::toString).collect(Collectors.toList()));
    assertEquals(1, contextMappingInfo.size());
    List<SessionInfo> sessionsInfo = em.createQuery("SELECT o FROM SessionInfo o", SessionInfo.class).getResultList();
    logger.info("Sessions found {}", sessionsInfo.stream().map(SessionInfo::getId).collect(Collectors.toList()));
    assertEquals(1, sessionsInfo.size());
    em.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) SessionInfo(org.drools.persistence.info.SessionInfo) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) QueryContext(org.kie.api.runtime.query.QueryContext) CountDownLatch(java.util.concurrent.CountDownLatch) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ContextMappingInfo(org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo) EntityManager(javax.persistence.EntityManager) CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) UserImpl(org.jbpm.services.task.impl.model.UserImpl) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 2 with ContextMappingInfo

use of org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo in project jbpm by kiegroup.

the class CaseServiceImplTest method testPerCaseCleanup.

@Test
public void testPerCaseCleanup() throws InterruptedException {
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    roleAssignments.put("owner", new UserImpl("john"));
    Map<String, Object> data = new HashMap<>();
    data.put("s", "description");
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID, data, roleAssignments);
    CaseDefinition caseDef = caseRuntimeDataService.getCase(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID);
    assertNotNull(caseDef);
    assertEquals(deploymentUnit.getIdentifier(), caseDef.getDeploymentId());
    assertEquals(3, caseDef.getAdHocFragments().size());
    Map<String, AdHocFragment> mappedFragments = mapAdHocFragments(caseDef.getAdHocFragments());
    assertTrue(mappedFragments.containsKey("Hello2"));
    assertTrue(mappedFragments.containsKey("Milestone1"));
    assertTrue(mappedFragments.containsKey("Milestone2"));
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_CASE_P_ID, caseFile);
    assertNotNull(caseId);
    assertEquals(HR_CASE_ID, caseId);
    Collection<ProcessInstanceDesc> desc = runtimeDataService.getProcessInstancesByDeploymentId(deploymentUnit.getIdentifier(), Arrays.asList(0, 1, 2, 3, 4), new QueryContext());
    List<Long> ids = desc.stream().map(e -> e.getId()).collect(Collectors.toList());
    Long pid = ids.get(0);
    CountDownLatch latch = new CountDownLatch(1);
    Runnable run = new Runnable() {

        public void run() {
            for (int i = 0; i <= 5; i++) {
                RuntimeManager manager = RuntimeManagerRegistry.get().getManager(deploymentUnit.getIdentifier());
                RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pid));
                try {
                    KieSession ksession = engine.getKieSession();
                    ((org.drools.core.process.instance.WorkItemManager) ksession.getWorkItemManager()).getWorkItem(1000000);
                } catch (SessionNotFoundException e) {
                    throw new ProcessInstanceNotFoundException("Process instance with id " + pid + " was not found", e);
                } finally {
                    manager.disposeRuntimeEngine(engine);
                }
                if (i == 3) {
                    try {
                        latch.await();
                    } catch (InterruptedException e) {
                    // ignore
                    }
                }
            }
        }
    };
    Thread executor = new Thread(run);
    executor.start();
    processService.abortProcessInstance(pid);
    logger.info("cancelled");
    latch.countDown();
    executor.join();
    EntityManager em = this.emf.createEntityManager();
    List<ContextMappingInfo> contextMappingInfo = em.createQuery("SELECT o FROM ContextMappingInfo o", ContextMappingInfo.class).getResultList();
    logger.info("ContextMappingInfo found {}", contextMappingInfo.stream().map(ContextMappingInfo::toString).collect(Collectors.toList()));
    assertEquals(1, contextMappingInfo.size());
    List<SessionInfo> sessionsInfo = em.createQuery("SELECT o FROM SessionInfo o", SessionInfo.class).getResultList();
    logger.info("Sessions found {}", sessionsInfo.stream().map(SessionInfo::getId).collect(Collectors.toList()));
    assertEquals(1, sessionsInfo.size());
    em.close();
}
Also used : TaskSummary(org.kie.api.task.model.TaskSummary) Arrays(java.util.Arrays) RuntimeManagerRegistry(org.kie.internal.runtime.manager.RuntimeManagerRegistry) CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) Date(java.util.Date) CommentSortBy(org.jbpm.casemgmt.api.model.instance.CommentSortBy) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) CaseAssignment(org.kie.api.runtime.process.CaseAssignment) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) DocumentImpl(org.jbpm.document.service.impl.DocumentImpl) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) After(org.junit.After) KieInternalServices(org.kie.internal.KieInternalServices) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) AuthorizationManager(org.jbpm.casemgmt.api.auth.AuthorizationManager) CaseMilestoneInstance(org.jbpm.casemgmt.api.model.instance.CaseMilestoneInstance) TaskSpecification(org.jbpm.casemgmt.api.dynamic.TaskSpecification) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) CaseDefinitionNotFoundException(org.jbpm.casemgmt.api.CaseDefinitionNotFoundException) Collection(java.util.Collection) Activation(org.drools.core.spi.Activation) UUID(java.util.UUID) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException) QueryContext(org.kie.api.runtime.query.QueryContext) Collectors(java.util.stream.Collectors) ContextMappingInfo(org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo) CountDownLatch(java.util.concurrent.CountDownLatch) CaseFileItem(org.jbpm.casemgmt.api.model.CaseFileItem) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Context(org.kie.api.runtime.Context) Assert.assertFalse(org.junit.Assert.assertFalse) AdHocFragment(org.jbpm.casemgmt.api.model.AdHocFragment) Person(org.jbpm.bpmn2.objects.Person) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) Status(org.kie.api.task.model.Status) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.process.NodeLeftCountDownProcessEventListener) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) UserImpl(org.jbpm.services.task.impl.model.UserImpl) CountDownListenerFactory(org.jbpm.casemgmt.impl.util.CountDownListenerFactory) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) ObjectModel(org.kie.internal.runtime.conf.ObjectModel) HashMap(java.util.HashMap) SessionInfo(org.drools.persistence.info.SessionInfo) QueryFilter(org.kie.internal.query.QueryFilter) ExecutableCommand(org.kie.api.command.ExecutableCommand) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext) VariableViolationException(org.jbpm.process.core.context.variable.VariableViolationException) ArrayList(java.util.ArrayList) CaseStageInstance(org.jbpm.casemgmt.api.model.instance.CaseStageInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) StageStatus(org.jbpm.casemgmt.api.model.instance.StageStatus) TestName(org.junit.rules.TestName) CaseInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseInstanceImpl) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) CaseStage(org.jbpm.casemgmt.api.model.CaseStage) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) Before(org.junit.Before) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) VariableDesc(org.jbpm.services.api.model.VariableDesc) CorrelationKey(org.kie.internal.process.CorrelationKey) Assert.assertNotNull(org.junit.Assert.assertNotNull) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) RegistryContext(org.kie.internal.command.RegistryContext) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) EntityManager(javax.persistence.EntityManager) GroupImpl(org.jbpm.services.task.impl.model.GroupImpl) Assertions.entry(org.assertj.core.api.Assertions.entry) MilestoneStatus(org.jbpm.casemgmt.api.model.instance.MilestoneStatus) TimeUnit(java.util.concurrent.TimeUnit) NodeInstanceDesc(org.jbpm.services.api.model.NodeInstanceDesc) Mockito(org.mockito.Mockito) MilestoneNodeInstance(org.jbpm.workflow.instance.node.MilestoneNodeInstance) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseStatus(org.jbpm.casemgmt.api.model.CaseStatus) Document(org.jbpm.document.Document) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) EchoService(org.jbpm.casemgmt.impl.objects.EchoService) Collections(java.util.Collections) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) Assert.assertEquals(org.junit.Assert.assertEquals) GroupElement(org.drools.core.rule.GroupElement) HashMap(java.util.HashMap) SessionInfo(org.drools.persistence.info.SessionInfo) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ContextMappingInfo(org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo) CaseDefinition(org.jbpm.casemgmt.api.model.CaseDefinition) UserImpl(org.jbpm.services.task.impl.model.UserImpl) KieSession(org.kie.api.runtime.KieSession) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) QueryContext(org.kie.api.runtime.query.QueryContext) CountDownLatch(java.util.concurrent.CountDownLatch) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException) AdHocFragment(org.jbpm.casemgmt.api.model.AdHocFragment) EntityManager(javax.persistence.EntityManager) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 3 with ContextMappingInfo

use of org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo in project jbpm by kiegroup.

the class JPAMapper method removeMapping.

@Override
public void removeMapping(Context context, String ownerId) {
    EntityManagerInfo info = getEntityManager(context);
    EntityManager em = info.getEntityManager();
    ContextMappingInfo contextMappingInfo = findContextByContextId(resolveContext(context, em), ownerId, em);
    logger.debug("Removing {}", contextMappingInfo);
    if (contextMappingInfo != null) {
        em.remove(contextMappingInfo);
    }
    if (!info.isShared()) {
        em.close();
    }
}
Also used : ContextMappingInfo(org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo) EntityManager(javax.persistence.EntityManager)

Example 4 with ContextMappingInfo

use of org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo in project jbpm by kiegroup.

the class JPAMapper method findContextId.

@Override
public Object findContextId(Long ksessionId, String ownerId) {
    EntityManagerInfo info = getEntityManager(null);
    EntityManager em = info.getEntityManager();
    try {
        Query findQuery = em.createNamedQuery("FindContextMapingByKSessionId").setParameter("ksessionId", ksessionId).setParameter("ownerId", ownerId);
        @SuppressWarnings("unchecked") List<ContextMappingInfo> contextMapping = findQuery.getResultList();
        if (contextMapping.isEmpty()) {
            return null;
        } else if (contextMapping.size() == 1) {
            return contextMapping.get(0).getContextId();
        } else {
            return contextMapping.stream().map(cmi -> cmi.getContextId()).collect(Collectors.toList());
        }
    } catch (NoResultException e) {
        return null;
    } catch (NonUniqueResultException e) {
        return null;
    } finally {
        if (!info.isShared()) {
            em.close();
        }
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) ContextMappingInfo(org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException)

Example 5 with ContextMappingInfo

use of org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo in project jbpm by kiegroup.

the class JPAMapper method saveMapping.

@Override
public void saveMapping(Context context, Long ksessionId, String ownerId) {
    EntityManagerInfo info = getEntityManager(context);
    EntityManager em = info.getEntityManager();
    Context<Object> resolvedContext = resolveContext(context, em);
    ContextMappingInfo contextMappingInfo = new ContextMappingInfo(resolvedContext.getContextId().toString(), ksessionId, ownerId);
    logger.debug("Persisting {}", contextMappingInfo);
    em.persist(contextMappingInfo);
    if (!info.isShared()) {
        em.close();
    }
}
Also used : ContextMappingInfo(org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo) EntityManager(javax.persistence.EntityManager)

Aggregations

ContextMappingInfo (org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo)8 EntityManager (javax.persistence.EntityManager)7 NoResultException (javax.persistence.NoResultException)3 NonUniqueResultException (javax.persistence.NonUniqueResultException)3 Query (javax.persistence.Query)3 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 SessionInfo (org.drools.persistence.info.SessionInfo)2 CaseDefinition (org.jbpm.casemgmt.api.model.CaseDefinition)2 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)2 AbstractCaseServicesBaseTest (org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest)2 ProcessInstanceDesc (org.jbpm.services.api.model.ProcessInstanceDesc)2 UserImpl (org.jbpm.services.task.impl.model.UserImpl)2 Test (org.junit.Test)2 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)2 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)2 QueryContext (org.kie.api.runtime.query.QueryContext)2 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1