Search in sources :

Example 1 with ExecutableRunner

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

the class ExecuteCommandDisconnectedTest method executeDisconnected.

@Test
public void executeDisconnected() {
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = kbase.newKieSession();
    ExecutionResultImpl localKresults = new ExecutionResultImpl();
    RequestContext context = RequestContext.create().with(ksession);
    ExecutableRunner runner = ExecutableRunner.create();
    List cmds = new ArrayList();
    cmds.add(new InsertObjectCommand(new String("Hi!"), "handle"));
    BatchExecutionCommand batchCmd = CommandFactory.newBatchExecution(cmds, "kresults");
    ExecuteCommand execCmd = new ExecuteCommand(batchCmd, true);
    ExecutionResults results = execCmd.execute(context);
    assertNotNull(results);
    assertNotNull(results.getFactHandle("handle"));
    assertTrue(((DefaultFactHandle) results.getFactHandle("handle")).isDisconnected());
    cmds = new ArrayList();
    cmds.add(new InsertObjectCommand(new String("Hi!"), "handle"));
    batchCmd = CommandFactory.newBatchExecution(cmds, "kresults");
    execCmd = new ExecuteCommand(batchCmd);
    results = execCmd.execute(context);
    assertNotNull(results);
    assertNotNull(results.getFactHandle("handle"));
    assertFalse(((DefaultFactHandle) results.getFactHandle("handle")).isDisconnected());
}
Also used : KieBase(org.kie.api.KieBase) ExecutionResults(org.kie.api.runtime.ExecutionResults) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) ExecuteCommand(org.drools.core.command.ExecuteCommand) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.kie.api.runtime.RequestContext) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) Test(org.junit.Test)

Example 2 with ExecutableRunner

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

the class SingletonRuntimeManagerTest method testInterceptorAfterRollback.

@Test
public void testInterceptorAfterRollback() throws Exception {
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTaskWithRollback.bpmn2"), ResourceType.BPMN2).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    ProcessInstance processInstance = ksession.startProcess("UserTaskWithRollback");
    ExecutableRunner commandService = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
    assertEquals(PersistableRunner.class, commandService.getClass());
    ChainableRunner internalCommandService = ((PersistableRunner) commandService).getChainableRunner();
    assertEquals(ExecutionErrorHandlerInterceptor.class, internalCommandService.getClass());
    internalCommandService = (ChainableRunner) ((ExecutionErrorHandlerInterceptor) internalCommandService).getNext();
    assertEquals(TransactionLockInterceptor.class, internalCommandService.getClass());
    TaskService taskService = runtime.getTaskService();
    List<Long> taskIds = taskService.getTasksByProcessInstanceId(processInstance.getId());
    taskService.start(taskIds.get(0), "john");
    HashMap<String, Object> result = new HashMap<String, Object>();
    result.put("output1", "rollback");
    try {
        // rollback transaction
        taskService.complete(taskIds.get(0), "john", result);
    } catch (WorkflowRuntimeException e) {
    // ignore
    }
    result = new HashMap<String, Object>();
    result.put("output1", "ok");
    // this time, execute normally
    taskService.complete(taskIds.get(0), "john", result);
    internalCommandService = ((PersistableRunner) commandService).getChainableRunner();
    assertEquals(ExecutionErrorHandlerInterceptor.class, internalCommandService.getClass());
    internalCommandService = (ChainableRunner) ((ExecutionErrorHandlerInterceptor) internalCommandService).getNext();
    assertEquals(TransactionLockInterceptor.class, internalCommandService.getClass());
    internalCommandService = (ChainableRunner) ((TransactionLockInterceptor) internalCommandService).getNext();
    assertEquals(OptimisticLockRetryInterceptor.class, internalCommandService.getClass());
    internalCommandService = (ChainableRunner) ((OptimisticLockRetryInterceptor) internalCommandService).getNext();
    assertEquals("org.drools.persistence.PersistableRunner$TransactionInterceptor", internalCommandService.getClass().getName());
    // close manager which will close session maintained by the manager
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) PersistableRunner(org.drools.persistence.PersistableRunner) ChainableRunner(org.drools.core.runtime.ChainableRunner) ExecutionErrorHandlerInterceptor(org.jbpm.runtime.manager.impl.error.ExecutionErrorHandlerInterceptor) OptimisticLockRetryInterceptor(org.drools.persistence.jpa.OptimisticLockRetryInterceptor) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) TransactionLockInterceptor(org.drools.persistence.jta.TransactionLockInterceptor) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with ExecutableRunner

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

the class DynamicUtils method internalAddDynamicWorkItem.

private static void internalAddDynamicWorkItem(final WorkflowProcessInstance processInstance, final DynamicNodeInstance dynamicContext, KieRuntime ksession, String workItemName, Map<String, Object> parameters) {
    final WorkItemImpl workItem = new WorkItemImpl();
    workItem.setState(WorkItem.ACTIVE);
    workItem.setProcessInstanceId(processInstance.getId());
    workItem.setDeploymentId((String) ksession.getEnvironment().get(EnvironmentName.DEPLOYMENT_ID));
    workItem.setName(workItemName);
    workItem.setParameters(parameters);
    for (Map.Entry<String, Object> entry : workItem.getParameters().entrySet()) {
        if (entry.getValue() instanceof String) {
            String s = (String) entry.getValue();
            Object variableValue = null;
            Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
            while (matcher.find()) {
                String paramName = matcher.group(1);
                variableValue = processInstance.getVariable(paramName);
                if (variableValue == null) {
                    try {
                        variableValue = MVELSafeHelper.getEvaluator().eval(paramName, new ProcessInstanceResolverFactory(processInstance));
                    } catch (Throwable t) {
                        logger.error("Could not find variable scope for variable {}", paramName);
                        logger.error("when trying to replace variable in string for Dynamic Work Item {}", workItemName);
                        logger.error("Continuing without setting parameter.");
                    }
                }
            }
            if (variableValue != null) {
                workItem.setParameter(entry.getKey(), variableValue);
            }
        }
    }
    final WorkItemNodeInstance workItemNodeInstance = new WorkItemNodeInstance();
    workItemNodeInstance.internalSetWorkItem(workItem);
    workItemNodeInstance.setMetaData("NodeType", workItemName);
    workItem.setNodeInstanceId(workItemNodeInstance.getId());
    if (ksession instanceof StatefulKnowledgeSessionImpl) {
        workItemNodeInstance.setProcessInstance(processInstance);
        workItemNodeInstance.setNodeInstanceContainer(dynamicContext == null ? processInstance : dynamicContext);
        workItemNodeInstance.addEventListeners();
        executeWorkItem((StatefulKnowledgeSessionImpl) ksession, workItem, workItemNodeInstance);
    } else if (ksession instanceof CommandBasedStatefulKnowledgeSession) {
        ExecutableRunner runner = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
        runner.execute(new ExecutableCommand<Void>() {

            private static final long serialVersionUID = 5L;

            public Void execute(Context context) {
                StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
                WorkflowProcessInstance realProcessInstance = (WorkflowProcessInstance) ksession.getProcessInstance(processInstance.getId());
                workItemNodeInstance.setProcessInstance(realProcessInstance);
                if (dynamicContext == null) {
                    workItemNodeInstance.setNodeInstanceContainer(realProcessInstance);
                } else {
                    DynamicNodeInstance realDynamicContext = findDynamicContext(realProcessInstance, dynamicContext.getUniqueId());
                    workItemNodeInstance.setNodeInstanceContainer(realDynamicContext);
                }
                workItemNodeInstance.addEventListeners();
                executeWorkItem((StatefulKnowledgeSessionImpl) ksession, workItem, workItemNodeInstance);
                return null;
            }
        });
    } 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) Matcher(java.util.regex.Matcher) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) ProcessInstanceResolverFactory(org.jbpm.workflow.instance.impl.ProcessInstanceResolverFactory) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession) Map(java.util.Map) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance)

Example 4 with ExecutableRunner

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

the class ExecuteCommandDisconnectedTest method executeDisconnected.

@Test
public void executeDisconnected() {
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = kbase.newKieSession();
    ExecutionResultImpl localKresults = new ExecutionResultImpl();
    RequestContext context = RequestContext.create().with(ksession);
    ExecutableRunner runner = ExecutableRunner.create();
    List cmds = new ArrayList();
    cmds.add(new InsertObjectCommand(new String("Hi!"), "handle"));
    BatchExecutionCommand batchCmd = CommandFactory.newBatchExecution(cmds, "kresults");
    ExecuteCommand execCmd = new ExecuteCommand(batchCmd, true);
    ExecutionResults results = execCmd.execute(context);
    assertNotNull(results);
    assertNotNull(results.getFactHandle("handle"));
    assertTrue(((DefaultFactHandle) results.getFactHandle("handle")).isDisconnected());
    cmds = new ArrayList();
    cmds.add(new InsertObjectCommand(new String("Hi!"), "handle"));
    batchCmd = CommandFactory.newBatchExecution(cmds, "kresults");
    execCmd = new ExecuteCommand(batchCmd);
    results = execCmd.execute(context);
    assertNotNull(results);
    assertNotNull(results.getFactHandle("handle"));
    assertFalse(((DefaultFactHandle) results.getFactHandle("handle")).isDisconnected());
}
Also used : KieBase(org.kie.api.KieBase) ExecutionResults(org.kie.api.runtime.ExecutionResults) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) ExecuteCommand(org.drools.core.command.ExecuteCommand) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(org.kie.api.runtime.RequestContext) InsertObjectCommand(org.drools.core.command.runtime.rule.InsertObjectCommand) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) Test(org.junit.Test)

Example 5 with ExecutableRunner

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

the class JpaTimerJobInstance method call.

public Void call() throws Exception {
    try {
        JDKCallableJobCommand command = new JDKCallableJobCommand(this);
        ExecutableRunner runner = ((CommandServiceTimerJobFactoryManager) ((TimerService) scheduler).getTimerJobFactoryManager()).getRunner();
        runner.execute(command);
        return null;
    } catch (Exception e) {
        logger.error("Unable to execute timer job!", e);
        throw e;
    }
}
Also used : CommandServiceTimerJobFactoryManager(org.drools.core.time.impl.CommandServiceTimerJobFactoryManager) ExecutableRunner(org.kie.api.runtime.ExecutableRunner)

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