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();
}
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();
}
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();
}
}
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();
}
}
}
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();
}
}
Aggregations