Search in sources :

Example 1 with SetProcessInstanceVariablesCommand

use of org.drools.core.command.runtime.process.SetProcessInstanceVariablesCommand in project jbpm by kiegroup.

the class AsyncWorkItemHandlerTest method testRunProcessWithAsyncHandlerCallbackErrorRetry.

@Test(timeout = 10000)
public void testRunProcessWithAsyncHandlerCallbackErrorRetry() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Task 1", 1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTaskWithError.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
            Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
            handlers.put("async", new AsyncWorkItemHandler(executorService, "org.jbpm.executor.commands.PrintOutCommand"));
            return handlers;
        }

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    assertEquals(0, IncrementService.get());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("retryAsync", "1s, 2s, 4s");
    ProcessInstance processInstance = ksession.startProcess("ScriptTask", params);
    assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
    countDownListener.waitTillCompleted(2000);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNotNull(processInstance);
    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("x", "should be fixed now");
    ksession.execute(new SetProcessInstanceVariablesCommand(processInstance.getId(), variables));
    countDownListener.waitTillCompleted();
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNull(processInstance);
    assertEquals(1, IncrementService.get());
}
Also used : DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) SetProcessInstanceVariablesCommand(org.drools.core.command.runtime.process.SetProcessInstanceVariablesCommand) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) AbstractExecutorBaseTest(org.jbpm.test.util.AbstractExecutorBaseTest) Test(org.junit.Test)

Example 2 with SetProcessInstanceVariablesCommand

use of org.drools.core.command.runtime.process.SetProcessInstanceVariablesCommand in project jbpm by kiegroup.

the class ProcessServiceImpl method setProcessVariables.

@Override
public void setProcessVariables(String deploymentId, Long processInstanceId, Map<String, Object> variables) {
    DeployedUnit deployedUnit = deploymentService.getDeployedUnit(deploymentId);
    if (deployedUnit == null) {
        throw new DeploymentNotFoundException("No deployments available for " + deploymentId);
    }
    RuntimeManager manager = deployedUnit.getRuntimeManager();
    variables = process(variables, ((InternalRuntimeManager) manager).getEnvironment().getClassLoader());
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
    try {
        KieSession ksession = engine.getKieSession();
        ProcessInstance pi = ksession.getProcessInstance(processInstanceId);
        if (pi == null) {
            throw new ProcessInstanceNotFoundException("Process instance with id " + processInstanceId + " was not found");
        }
        ksession.execute(new SetProcessInstanceVariablesCommand(processInstanceId, variables));
    } catch (SessionNotFoundException e) {
        throw new ProcessInstanceNotFoundException("Process instance with id " + processInstanceId + " was not found", e);
    } finally {
        disposeRuntimeEngine(manager, engine);
    }
}
Also used : DeploymentNotFoundException(org.jbpm.services.api.DeploymentNotFoundException) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) DeployedUnit(org.jbpm.services.api.model.DeployedUnit) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) SetProcessInstanceVariablesCommand(org.drools.core.command.runtime.process.SetProcessInstanceVariablesCommand) KieSession(org.kie.api.runtime.KieSession) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) ProcessInstanceNotFoundException(org.jbpm.services.api.ProcessInstanceNotFoundException) SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException)

Aggregations

SetProcessInstanceVariablesCommand (org.drools.core.command.runtime.process.SetProcessInstanceVariablesCommand)2 KieSession (org.kie.api.runtime.KieSession)2 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 HashMap (java.util.HashMap)1 RuleFlowProcessInstance (org.jbpm.ruleflow.instance.RuleFlowProcessInstance)1 DefaultRegisterableItemsFactory (org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory)1 DeploymentNotFoundException (org.jbpm.services.api.DeploymentNotFoundException)1 ProcessInstanceNotFoundException (org.jbpm.services.api.ProcessInstanceNotFoundException)1 DeployedUnit (org.jbpm.services.api.model.DeployedUnit)1 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)1 AbstractExecutorBaseTest (org.jbpm.test.util.AbstractExecutorBaseTest)1 Test (org.junit.Test)1 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)1 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)1 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)1 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)1 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)1 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)1 SessionNotFoundException (org.kie.internal.runtime.manager.SessionNotFoundException)1