PerProcessInstanceRuntimeManager extends AbstractRuntimeManager { @Override public RuntimeEngine getRuntimeEngine(Context<?> context) { if (isClosed()) { throw new IllegalStateException("Runtime manager " + identifier + " is already closed"); } RuntimeEngine runtime = null; Object contextId = context.getContextId(); if (!(context instanceof ProcessInstanceIdContext || context instanceof CorrelationKeyContext)) { logger.warn("ProcessInstanceIdContext or CorrelationKeyContext shall be used when interacting with PerProcessInstance runtime manager"); } if (engineInitEager) { KieSession ksession = null; Long ksessionId = null; if (contextId == null || context instanceof EmptyContext) { ksession = factory.newKieSession(); ksessionId = ksession.getIdentifier(); } else { RuntimeEngine localRuntime = findLocalRuntime(contextId); if (localRuntime != null) { return localRuntime; } ksessionId = mapper.findMapping(context, this.identifier); if (ksessionId == null) { throw new SessionNotFoundException("No session found for context " + context.getContextId()); } ksession = factory.findKieSessionById(ksessionId); } InternalTaskService internalTaskService = newTaskService(taskServiceFactory); runtime = new RuntimeEngineImpl(ksession, internalTaskService); ((RuntimeEngineImpl) runtime).setManager(this); ((RuntimeEngineImpl) runtime).setContext(context); configureRuntimeOnTaskService(internalTaskService, runtime); registerDisposeCallback(runtime, new DisposeSessionTransactionSynchronization(this, runtime), ksession.getEnvironment()); registerItems(runtime); attachManager(runtime); ksession.addEventListener(new MaintainMappingListener(ksessionId, runtime, this.identifier)); } else { RuntimeEngine localRuntime = findLocalRuntime(contextId); if (localRuntime != null) { return localRuntime; } runtime = new RuntimeEngineImpl(context, new PerProcessInstanceInitializer()); ((RuntimeEngineImpl) runtime).setManager(this); } createLockOnGetEngine(context, runtime); saveLocalRuntime(contextId, runtime); return runtime; } PerProcessInstanceRuntimeManager(RuntimeEnvironment environment, SessionFactory factory, TaskServiceFactory taskServiceFactory, String identifier); @Override RuntimeEngine getRuntimeEngine(Context<?> context); @Override void signalEvent(String type, Object event); @Override void validate(KieSession ksession, Context<?> context); @Override void disposeRuntimeEngine(RuntimeEngine runtime); @Override void softDispose(RuntimeEngine runtimeEngine); @Override void close(); boolean validate(Long ksessionId, Long processInstanceId); SessionFactory getFactory(); void setFactory(SessionFactory factory); TaskServiceFactory getTaskServiceFactory(); void setTaskServiceFactory(TaskServiceFactory taskServiceFactory); Mapper getMapper(); void setMapper(Mapper mapper); @Override void init(); @Override void activate(); @Override void deactivate();  }
@Test public void testCreationOfRuntimeManagerWithinTransaction() throws Exception { System.setProperty("jbpm.tm.jndi.lookup", "java:comp/UserTransaction"); UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction"); ut.begin(); RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get() .newDefaultBuilder() .userGroupCallback(userGroupCallback) .addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2) .get(); manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment); assertNotNull(manager); RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get()); KieSession ksession = runtime.getKieSession(); assertNotNull(ksession); ksession.startProcess("ScriptTask"); ut.commit(); System.clearProperty("jbpm.tm.jndi.lookup"); }
@Test(expected=UnsupportedOperationException.class) public void testAuditServiceNotAvailable() { RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get() .newDefaultInMemoryBuilder() .userGroupCallback(userGroupCallback) .addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2) .addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2) .get(); manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment); assertNotNull(manager); RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get()); runtime.getAuditService(); }