@Override protected void registerItems(RuntimeEngine runtime) { super.registerItems(runtime); if (getCaseEventSupport() != null) { // add any process listeners from case event listeners List<? extends EventListener> eventListener = getCaseEventSupport().getEventListeners(); for (EventListener listener : eventListener) { if (listener instanceof ProcessEventListener) { runtime.getKieSession().addEventListener((ProcessEventListener) listener); } } } }
public RuntimeManager newPerCaseRuntimeManager(RuntimeEnvironment environment, String identifier) { SessionFactory factory = getSessionFactory(environment, identifier); TaskServiceFactory taskServiceFactory = getTaskServiceFactory(environment); RuntimeManager manager = new PerCaseRuntimeManager(environment, factory, taskServiceFactory, identifier); initTimerService(environment, manager); ((AbstractRuntimeManager) manager).init(); return manager; }
@SuppressWarnings("unchecked") @Override public RuntimeEngine getRuntimeEngine(Context<?> context) { if (isClosed()) { throw new IllegalStateException("Runtime manager " + identifier + " is already closed"); checkPermission(); RuntimeEngine runtime = null; Object contextId = context.getContextId(); RuntimeEngine localRuntime = findLocalRuntime(contextId); if (localRuntime != null) { return localRuntime; ksession.execute(new SaveMappingCommand(mapper, context, ksessionId, getIdentifier())); 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, (String) contextId)); saveLocalRuntime(null, Long.parseLong(o.toString()), finalRuntimeEngnie); } catch (NumberFormatException e) { saveLocalRuntime(o.toString(), null, finalRuntimeEngnie); finalKieSession.getEnvironment().set("CaseId", o.toString());
@Override public TaskService initTaskService(Context<?> context, InternalRuntimeManager manager, RuntimeEngine engine) { InternalTaskService internalTaskService = newTaskService(taskServiceFactory); if (internalTaskService != null) { registerDisposeCallback(engine, new DisposeSessionTransactionSynchronization(manager, engine), ((CommandBasedTaskService) internalTaskService).getEnvironment()); configureRuntimeOnTaskService(internalTaskService, engine); } return internalTaskService; }
@Override public void disposeRuntimeEngine(RuntimeEngine runtime) { if (isClosed()) { throw new IllegalStateException("Runtime manager " + identifier + " is already closed"); if (canDispose(runtime)) { removeLocalRuntime(runtime); releaseAndCleanLock(ksessionId, runtime); if (runtime instanceof Disposable) { TimerService timerService = TimerServiceRegistry.getInstance().get(getIdentifier() + TimerServiceRegistry.TIMER_SERVICE_SUFFIX); if (timerService != null) { if (timerService instanceof GlobalTimerService) { releaseAndCleanLock(runtime); removeLocalRuntime(runtime); throw new RuntimeException(e);
@Override public void signalEvent(String type, Object event) { // first signal with new context in case there are start event with signal RuntimeEngine runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get()); runtimeEngine.getKieSession().signalEvent(type, event); if (canDispose(runtimeEngine)) { disposeRuntimeEngine(runtimeEngine); } // next find out all instances waiting for given event type List<String> processInstances = ((InternalMapper) mapper).findContextIdForEvent(type, getIdentifier()); for (String piId : processInstances) { runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(Long.parseLong(piId))); runtimeEngine.getKieSession().signalEvent(type, event); if (canDispose(runtimeEngine)) { disposeRuntimeEngine(runtimeEngine); } } // process currently active runtime engines Map<Object, RuntimeEngine> currentlyActive = local.get(); if (currentlyActive != null && !currentlyActive.isEmpty()) { RuntimeEngine[] activeEngines = currentlyActive.values().toArray(new RuntimeEngine[currentlyActive.size()]); for (RuntimeEngine engine : activeEngines) { Context<?> context = ((RuntimeEngineImpl) engine).getContext(); if (context != null && context instanceof ProcessInstanceIdContext && ((ProcessInstanceIdContext) context).getContextId() != null) { engine.getKieSession().signalEvent(type, event, ((ProcessInstanceIdContext) context).getContextId()); } } } }
Long ksessionId = null; RuntimeEngine localRuntime = ((PerCaseRuntimeManager) manager).findLocalRuntime(contextId); if (localRuntime != null && ((RuntimeEngineImpl) engine).internalGetKieSession() != null) { return localRuntime.getKieSession(); registerItems(engine); attachManager(engine); registerDisposeCallback(engine, new DisposeSessionTransactionSynchronization(manager, engine), ksession.getEnvironment()); ksession.addEventListener(new MaintainMappingListener(ksessionId, engine, manager.getIdentifier(), contextId.toString())); try { saveLocalRuntime(null, Long.parseLong(o.toString()), engine); } catch (NumberFormatException e) { saveLocalRuntime(o.toString(), null, engine); finalKieSession.getEnvironment().set("CaseId", o.toString());
public void destroyCase(CaseContext caseContext) { KieSession kieSession = null; RuntimeEngine localRuntime = findLocalRuntime(caseContext.getContextId()); if (localRuntime != null) { kieSession = localRuntime.getKieSession(); } else { Long ksessionId = mapper.findMapping(caseContext, this.identifier); if (ksessionId != null) { kieSession = factory.findKieSessionById(ksessionId); } } factory.onDispose(kieSession.getIdentifier()); List<ExecutableCommand<?>> cmds = new ArrayList<>(); RemoveMappingCommand removeMapping = new RemoveMappingCommand(mapper, caseContext, getIdentifier()); cmds.add(removeMapping); DestroyKSessionCommand destroy = new DestroyKSessionCommand(kieSession, this); cmds.add(destroy); BatchExecutionCommand batchCmd = new BatchExecutionCommandImpl(cmds); kieSession.execute(batchCmd); }
@Test public void testInMemorySessionCleanup() { for ( int i = 0; i < 20; i++ ) { Context<?> context = getContext(); RuntimeEngine runtime = manager.getRuntimeEngine(context); KieSession ksession = runtime.getKieSession(); ProcessInstance pi = ksession.startProcess("ScriptTask"); assertEquals(ProcessInstance.STATE_COMPLETED, pi.getState()); manager.disposeRuntimeEngine( runtime ); if ("case".equals(strategy)) { ((PerCaseRuntimeManager) manager).destroyCase((CaseContext) context); } } InMemorySessionFactory factory = (InMemorySessionFactory) ((AbstractRuntimeManager) manager).getFactory(); int expectedSessionsInFactory = getExpectedSessionsInFactory(); assertEquals(expectedSessionsInFactory, factory.getSessions().size()); }
@Override public void disposeRuntimeEngine(RuntimeEngine runtime) { if (isClosed()) { throw new IllegalStateException("Runtime manager " + identifier + " is already closed"); if (canDispose(runtime)) { removeLocalRuntime(runtime); releaseAndCleanLock(ksessionId, runtime); if (runtime instanceof Disposable) { TimerService timerService = TimerServiceRegistry.getInstance().get(getIdentifier() + TimerServiceRegistry.TIMER_SERVICE_SUFFIX); if (timerService != null) { if (timerService instanceof GlobalTimerService) { releaseAndCleanLock(runtime); removeLocalRuntime(runtime); throw new RuntimeException(e);
Long ksessionId = null; RuntimeEngine localRuntime = ((PerCaseRuntimeManager) manager).findLocalRuntime(contextId); if (localRuntime != null && ((RuntimeEngineImpl) engine).internalGetKieSession() != null) { return localRuntime.getKieSession(); registerItems(engine); attachManager(engine); registerDisposeCallback(engine, new DisposeSessionTransactionSynchronization(manager, engine), ksession.getEnvironment()); ksession.addEventListener(new MaintainMappingListener(ksessionId, engine, manager.getIdentifier(), contextId.toString())); try { saveLocalRuntime(null, Long.parseLong(o.toString()), engine); } catch (NumberFormatException e) { saveLocalRuntime(o.toString(), null, engine); finalKieSession.getEnvironment().set("CaseId", o.toString());
@Override public void signalEvent(String type, Object event) { // first signal with new context in case there are start event with signal RuntimeEngine runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get()); runtimeEngine.getKieSession().signalEvent(type, event); if (canDispose(runtimeEngine)) { disposeRuntimeEngine(runtimeEngine); } // next find out all instances waiting for given event type List<String> processInstances = ((InternalMapper) mapper).findContextIdForEvent(type, getIdentifier()); for (String piId : processInstances) { runtimeEngine = getRuntimeEngine(ProcessInstanceIdContext.get(Long.parseLong(piId))); runtimeEngine.getKieSession().signalEvent(type, event); if (canDispose(runtimeEngine)) { disposeRuntimeEngine(runtimeEngine); } } // process currently active runtime engines Map<Object, RuntimeEngine> currentlyActive = local.get(); if (currentlyActive != null && !currentlyActive.isEmpty()) { RuntimeEngine[] activeEngines = currentlyActive.values().toArray(new RuntimeEngine[currentlyActive.size()]); for (RuntimeEngine engine : activeEngines) { Context<?> context = ((RuntimeEngineImpl) engine).getContext(); if (context != null && context instanceof ProcessInstanceIdContext && ((ProcessInstanceIdContext) context).getContextId() != null) { engine.getKieSession().signalEvent(type, event, ((ProcessInstanceIdContext) context).getContextId()); } } } }
@Override public TaskService initTaskService(Context<?> context, InternalRuntimeManager manager, RuntimeEngine engine) { InternalTaskService internalTaskService = newTaskService(taskServiceFactory); if (internalTaskService != null) { registerDisposeCallback(engine, new DisposeSessionTransactionSynchronization(manager, engine), ((CommandBasedTaskService) internalTaskService).getEnvironment()); configureRuntimeOnTaskService(internalTaskService, engine); } return internalTaskService; }
public void destroyCase(CaseContext caseContext) { KieSession kieSession = null; RuntimeEngine localRuntime = findLocalRuntime(caseContext.getContextId()); if (localRuntime != null) { kieSession = localRuntime.getKieSession(); } else { Long ksessionId = mapper.findMapping(caseContext, this.identifier); if (ksessionId != null) { kieSession = factory.findKieSessionById(ksessionId); } } factory.onDispose(kieSession.getIdentifier()); List<ExecutableCommand<?>> cmds = new ArrayList<>(); RemoveMappingCommand removeMapping = new RemoveMappingCommand(mapper, caseContext, getIdentifier()); cmds.add(removeMapping); DestroyKSessionCommand destroy = new DestroyKSessionCommand(kieSession, this); cmds.add(destroy); BatchExecutionCommand batchCmd = new BatchExecutionCommandImpl(cmds); kieSession.execute(batchCmd); }
@SuppressWarnings("unchecked") @Override public RuntimeEngine getRuntimeEngine(Context<?> context) { if (isClosed()) { throw new IllegalStateException("Runtime manager " + identifier + " is already closed"); checkPermission(); RuntimeEngine runtime = null; Object contextId = context.getContextId(); RuntimeEngine localRuntime = findLocalRuntime(contextId); if (localRuntime != null) { return localRuntime; ksession.execute(new SaveMappingCommand(mapper, context, ksessionId, getIdentifier())); 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, (String) contextId)); saveLocalRuntime(null, Long.parseLong(o.toString()), finalRuntimeEngnie); } catch (NumberFormatException e) { saveLocalRuntime(o.toString(), null, finalRuntimeEngnie); finalKieSession.getEnvironment().set("CaseId", o.toString());
@Override protected void registerItems(RuntimeEngine runtime) { super.registerItems(runtime); if (getCaseEventSupport() != null) { // add any process listeners from case event listeners List<? extends EventListener> eventListener = getCaseEventSupport().getEventListeners(); for (EventListener listener : eventListener) { if (listener instanceof ProcessEventListener) { runtime.getKieSession().addEventListener((ProcessEventListener) listener); } } } }
public RuntimeManager newPerCaseRuntimeManager(RuntimeEnvironment environment, String identifier) { SessionFactory factory = getSessionFactory(environment, identifier); TaskServiceFactory taskServiceFactory = getTaskServiceFactory(environment); RuntimeManager manager = new PerCaseRuntimeManager(environment, factory, taskServiceFactory, identifier); initTimerService(environment, manager); ((AbstractRuntimeManager) manager).init(); return manager; }
@SuppressWarnings({"rawtypes", "unchecked"}) @Override public void notifyOnDeploy(DeploymentUnit unit, DeployedUnit deployedUnit) { super.notifyOnDeploy(unit, deployedUnit); InternalRuntimeManager runtimeManager = (InternalRuntimeManager) deployedUnit.getRuntimeManager(); if (runtimeManager instanceof PerCaseRuntimeManager) { AbstractEventSupport eventSupport =((PerCaseRuntimeManager) runtimeManager).getCaseEventSupport(); Map<String, CaseEventListener> foundBeans = context.getBeansOfType(CaseEventListener.class); for (CaseEventListener listener : foundBeans.values()) { eventSupport.addEventListener(listener); logger.debug("Registering {} as case event listener on {}", listener, runtimeManager.getIdentifier()); } } }