PerRequestRuntimeManager extends AbstractRuntimeManager { @Override public RuntimeEngine getRuntimeEngine(Context<?> context) { if (isClosed()) { throw new IllegalStateException("Runtime manager " + identifier + " is already closed"); } RuntimeEngine runtime = null; if (local.get().get(identifier) != null) { RuntimeEngine engine = local.get().get(identifier); if (engine != null && ((RuntimeEngineImpl) engine).isDisposed()) { return null; } return engine; } if (engineInitEager) { InternalTaskService internalTaskService = newTaskService(taskServiceFactory); runtime = new RuntimeEngineImpl(factory.newKieSession(), internalTaskService); ((RuntimeEngineImpl) runtime).setManager(this); configureRuntimeOnTaskService(internalTaskService, runtime); registerDisposeCallback(runtime, new DisposeSessionTransactionSynchronization(this, runtime), runtime.getKieSession().getEnvironment()); registerDisposeCallback(runtime, new DestroySessionTransactionSynchronization(runtime.getKieSession()), runtime.getKieSession().getEnvironment()); registerItems(runtime); attachManager(runtime); } else { runtime = new RuntimeEngineImpl(context, new PerRequestInitializer()); ((RuntimeEngineImpl) runtime).setManager(this); } local.get().put(identifier, runtime); return runtime; } PerRequestRuntimeManager(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(); SessionFactory getFactory(); void setFactory(SessionFactory factory); TaskServiceFactory getTaskServiceFactory(); void setTaskServiceFactory(TaskServiceFactory taskServiceFactory); @Override void init();  }
@Test public void testCreationOfSessionWithinTransaction() throws Exception { System.setProperty("jbpm.tm.jndi.lookup", "java:comp/UserTransaction"); RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get() .newDefaultBuilder() .userGroupCallback(userGroupCallback) .addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2) .get(); manager = RuntimeManagerFactory.Factory.get().newPerRequestRuntimeManager(environment); assertNotNull(manager); UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction"); ut.begin(); RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get()); KieSession ksession = runtime.getKieSession(); assertNotNull(ksession); long sessionId = ksession.getIdentifier(); assertTrue(sessionId == 1); ut.commit(); try { ksession.getIdentifier(); fail("Should fail as session manager was closed and with that it's session"); } catch (IllegalStateException e) { } catch (UndeclaredThrowableException e) { TestUtil.checkDisposedSessionException(e); } System.clearProperty("jbpm.tm.jndi.lookup"); }
@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().newPerRequestRuntimeManager(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"); }