Search in sources :

Example 1 with ProcessInstanceIdContext

use of org.kie.internal.runtime.manager.context.ProcessInstanceIdContext in project jbpm by kiegroup.

the class PerCaseRuntimeManager method signalEvent.

@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());
            }
        }
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) InternalMapper(org.jbpm.runtime.manager.impl.mapper.InternalMapper) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext)

Example 2 with ProcessInstanceIdContext

use of org.kie.internal.runtime.manager.context.ProcessInstanceIdContext in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManager method getRuntimeEngine.

@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();
    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;
        }
        // lazy initialization of ksession and task service
        runtime = new RuntimeEngineImpl(context, new PerProcessInstanceInitializer());
        ((RuntimeEngineImpl) runtime).setManager(this);
    }
    createLockOnGetEngine(context, runtime);
    saveLocalRuntime(contextId, runtime);
    return runtime;
}
Also used : EmptyContext(org.kie.internal.runtime.manager.context.EmptyContext) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) InternalTaskService(org.kie.internal.task.api.InternalTaskService) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext) CorrelationKeyContext(org.kie.internal.runtime.manager.context.CorrelationKeyContext) DisposeSessionTransactionSynchronization(org.jbpm.runtime.manager.impl.tx.DisposeSessionTransactionSynchronization) KieSession(org.kie.api.runtime.KieSession) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException)

Example 3 with ProcessInstanceIdContext

use of org.kie.internal.runtime.manager.context.ProcessInstanceIdContext in project jbpm by kiegroup.

the class PerCaseRuntimeManager method getRuntimeEngine.

@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();
    if (!(context instanceof ProcessInstanceIdContext || context instanceof CaseContext)) {
        logger.warn("ProcessInstanceIdContext or CaseContext shall be used when interacting with PerCase runtime manager");
    }
    if (engineInitEager) {
        KieSession ksession = null;
        Long ksessionId = null;
        RuntimeEngine localRuntime = findLocalRuntime(contextId);
        if (localRuntime != null) {
            return localRuntime;
        }
        synchronized (this) {
            ksessionId = mapper.findMapping(context, this.identifier);
            if (ksessionId == null) {
                ksession = factory.newKieSession();
                ksessionId = ksession.getIdentifier();
                if (context instanceof CaseContext) {
                    ksession.execute(new SaveMappingCommand(mapper, context, ksessionId, getIdentifier()));
                }
            } else {
                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, (String) contextId));
        if (context instanceof CaseContext) {
            ksession.getEnvironment().set("CaseId", context.getContextId());
        } else {
            Object contexts = mapper.findContextId(ksession.getIdentifier(), this.identifier);
            if (contexts instanceof Collection) {
                RuntimeEngine finalRuntimeEngnie = runtime;
                KieSession finalKieSession = ksession;
                ((Collection<Object>) contexts).forEach(o -> {
                    try {
                        saveLocalRuntime(null, Long.parseLong(o.toString()), finalRuntimeEngnie);
                    } catch (NumberFormatException e) {
                        saveLocalRuntime(o.toString(), null, finalRuntimeEngnie);
                        finalKieSession.getEnvironment().set("CaseId", o.toString());
                    }
                });
            }
        }
    } else {
        RuntimeEngine localRuntime = findLocalRuntime(contextId);
        if (localRuntime != null) {
            return localRuntime;
        }
        // lazy initialization of ksession and task service
        runtime = new RuntimeEngineImpl(context, new PerCaseInitializer());
        ((RuntimeEngineImpl) runtime).setManager(this);
    }
    String caseId = null;
    Long processInstanceId = null;
    if (context instanceof CaseContext) {
        caseId = (String) contextId;
    } else if (context instanceof ProcessInstanceIdContext) {
        processInstanceId = (Long) contextId;
    }
    Long ksessionId = mapper.findMapping(context, this.identifier);
    createLockOnGetEngine(ksessionId, runtime);
    saveLocalRuntime(caseId, processInstanceId, runtime);
    return runtime;
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) InternalTaskService(org.kie.internal.task.api.InternalTaskService) CaseContext(org.kie.internal.runtime.manager.context.CaseContext) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext) DisposeSessionTransactionSynchronization(org.jbpm.runtime.manager.impl.tx.DisposeSessionTransactionSynchronization) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession)

Example 4 with ProcessInstanceIdContext

use of org.kie.internal.runtime.manager.context.ProcessInstanceIdContext in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManager method signalEvent.

@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);
    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);
        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());
            }
        }
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) InternalMapper(org.jbpm.runtime.manager.impl.mapper.InternalMapper) ProcessInstanceIdContext(org.kie.internal.runtime.manager.context.ProcessInstanceIdContext)

Aggregations

RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)4 ProcessInstanceIdContext (org.kie.internal.runtime.manager.context.ProcessInstanceIdContext)4 InternalMapper (org.jbpm.runtime.manager.impl.mapper.InternalMapper)2 DisposeSessionTransactionSynchronization (org.jbpm.runtime.manager.impl.tx.DisposeSessionTransactionSynchronization)2 KieSession (org.kie.api.runtime.KieSession)2 InternalTaskService (org.kie.internal.task.api.InternalTaskService)2 Collection (java.util.Collection)1 SessionNotFoundException (org.kie.internal.runtime.manager.SessionNotFoundException)1 CaseContext (org.kie.internal.runtime.manager.context.CaseContext)1 CorrelationKeyContext (org.kie.internal.runtime.manager.context.CorrelationKeyContext)1 EmptyContext (org.kie.internal.runtime.manager.context.EmptyContext)1