Search in sources :

Example 6 with ExecutableRunner

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

the class DynamicUtils method internalAddDynamicSubProcess.

public static long internalAddDynamicSubProcess(final WorkflowProcessInstance processInstance, final DynamicNodeInstance dynamicContext, KieRuntime ksession, final String processId, final Map<String, Object> parameters) {
    final SubProcessNodeInstance subProcessNodeInstance = new SubProcessNodeInstance();
    subProcessNodeInstance.setNodeInstanceContainer(dynamicContext == null ? processInstance : dynamicContext);
    subProcessNodeInstance.setProcessInstance(processInstance);
    subProcessNodeInstance.setMetaData("NodeType", "SubProcessNode");
    if (ksession instanceof StatefulKnowledgeSessionImpl) {
        return executeSubProcess((StatefulKnowledgeSessionImpl) ksession, processId, parameters, processInstance, subProcessNodeInstance);
    } else if (ksession instanceof CommandBasedStatefulKnowledgeSession) {
        ExecutableRunner commandService = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
        return commandService.execute(new ExecutableCommand<Long>() {

            private static final long serialVersionUID = 5L;

            public Long execute(Context context) {
                StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
                WorkflowProcessInstance realProcessInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstance.getId());
                subProcessNodeInstance.setProcessInstance(realProcessInstance);
                if (dynamicContext == null) {
                    subProcessNodeInstance.setNodeInstanceContainer(realProcessInstance);
                } else {
                    DynamicNodeInstance realDynamicContext = findDynamicContext(realProcessInstance, dynamicContext.getUniqueId());
                    subProcessNodeInstance.setNodeInstanceContainer(realDynamicContext);
                }
                return executeSubProcess((StatefulKnowledgeSessionImpl) ksession, processId, parameters, processInstance, subProcessNodeInstance);
            }
        });
    } else {
        throw new IllegalArgumentException("Unsupported ksession: " + ksession == null ? "null" : ksession.getClass().getName());
    }
}
Also used : RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance)

Example 7 with ExecutableRunner

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

the class GlobalJpaTimerJobInstance method call.

@Override
public Void call() throws Exception {
    AsyncExecutionMarker.markAsync();
    ExecutableRunner runner = null;
    TransactionManager jtaTm = null;
    boolean success = false;
    try {
        JDKCallableJobCommand command = new JDKCallableJobCommand(this);
        if (scheduler == null) {
            scheduler = (InternalSchedulerService) TimerServiceRegistry.getInstance().get(timerServiceId);
        }
        if (scheduler == null) {
            throw new RuntimeException("No scheduler found for " + timerServiceId);
        }
        jtaTm = startTxIfNeeded(((GlobalTimerService) scheduler).getRuntimeManager().getEnvironment().getEnvironment());
        runner = ((GlobalTimerService) scheduler).getRunner(getJobContext());
        runner.execute(command);
        GlobalJPATimerJobFactoryManager timerService = ((GlobalJPATimerJobFactoryManager) ((GlobalTimerService) scheduler).getTimerJobFactoryManager());
        timerService.removeTimerJobInstance(((DefaultJobHandle) getJobHandle()).getTimerJobInstance());
        success = true;
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        success = false;
        throw e;
    } finally {
        AsyncExecutionMarker.reset();
        if (runner != null && runner instanceof DisposableCommandService) {
            if (allowedToDispose(((DisposableCommandService) runner).getEnvironment())) {
                logger.debug("Allowed to dispose command service from global timer job instance");
                ((DisposableCommandService) runner).dispose();
            }
        }
        closeTansactionIfNeeded(jtaTm, success);
    }
}
Also used : DisposableCommandService(org.jbpm.process.core.timer.impl.GlobalTimerService.DisposableCommandService) TransactionManager(org.drools.persistence.api.TransactionManager) ContainerManagedTransactionManager(org.jbpm.persistence.jta.ContainerManagedTransactionManager) JDKCallableJobCommand(org.drools.persistence.jpa.JDKCallableJobCommand) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService)

Aggregations

ExecutableRunner (org.kie.api.runtime.ExecutableRunner)7 KieSession (org.kie.api.runtime.KieSession)5 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExecuteCommand (org.drools.core.command.ExecuteCommand)2 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)2 RegistryContext (org.drools.core.command.impl.RegistryContext)2 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)2 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)2 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)2 KieBase (org.kie.api.KieBase)2 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)2 Context (org.kie.api.runtime.Context)2 ExecutionResults (org.kie.api.runtime.ExecutionResults)2 RequestContext (org.kie.api.runtime.RequestContext)2 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1