Search in sources :

Example 66 with RuntimeEngine

use of org.kie.api.runtime.manager.RuntimeEngine in project jbpm by kiegroup.

the class AsyncWorkItemHandlerCmdCallback method onCommandDone.

@Override
public void onCommandDone(CommandContext ctx, ExecutionResults results) {
    WorkItem workItem = (WorkItem) ctx.getData("workItem");
    logger.debug("About to complete work item {}", workItem);
    // find the right runtime to do the complete
    RuntimeManager manager = getRuntimeManager(ctx);
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get((Long) ctx.getData("processInstanceId")));
    try {
        engine.getKieSession().getWorkItemManager().completeWorkItem(workItem.getId(), results == null ? null : results.getData());
    } finally {
        manager.disposeRuntimeEngine(engine);
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) WorkItem(org.kie.api.runtime.process.WorkItem)

Example 67 with RuntimeEngine

use of org.kie.api.runtime.manager.RuntimeEngine 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 68 with RuntimeEngine

use of org.kie.api.runtime.manager.RuntimeEngine in project jbpm by kiegroup.

the class PerCaseRuntimeManager method destroyCase.

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);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession)

Example 69 with RuntimeEngine

use of org.kie.api.runtime.manager.RuntimeEngine 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 70 with RuntimeEngine

use of org.kie.api.runtime.manager.RuntimeEngine in project jbpm by kiegroup.

the class PerRequestRuntimeManager method getRuntimeEngine.

@Override
public RuntimeEngine getRuntimeEngine(Context<?> context) {
    if (isClosed()) {
        throw new IllegalStateException("Runtime manager " + identifier + " is already closed");
    }
    checkPermission();
    RuntimeEngine runtime = null;
    if (local.get().get(identifier) != null) {
        RuntimeEngine engine = local.get().get(identifier);
        // check if engine is not already disposed as afterCompletion might be issued from another thread
        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;
}
Also used : DestroySessionTransactionSynchronization(org.jbpm.runtime.manager.impl.tx.DestroySessionTransactionSynchronization) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) DisposeSessionTransactionSynchronization(org.jbpm.runtime.manager.impl.tx.DisposeSessionTransactionSynchronization) InternalTaskService(org.kie.internal.task.api.InternalTaskService)

Aggregations

RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)388 Test (org.junit.Test)269 KieSession (org.kie.api.runtime.KieSession)265 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)229 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)149 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)116 TaskService (org.kie.api.task.TaskService)111 HashMap (java.util.HashMap)110 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)105 TaskSummary (org.kie.api.task.model.TaskSummary)86 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)82 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)67 ArrayList (java.util.ArrayList)59 InternalTaskService (org.kie.internal.task.api.InternalTaskService)59 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)58 AbstractExecutorBaseTest (org.jbpm.test.util.AbstractExecutorBaseTest)49 DefaultRegisterableItemsFactory (org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory)43 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)40 UserTaskService (org.jbpm.services.api.UserTaskService)36 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)34