Search in sources :

Example 6 with PersistableRunner

use of org.drools.persistence.PersistableRunner in project jbpm by kiegroup.

the class SingleSessionCommandServiceTest method testPersistenceWorkItems.

@Test
public void testPersistenceWorkItems() throws Exception {
    setUp();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KiePackage> kpkgs = getProcessWorkItems();
    kbase.addPackages(kpkgs);
    Properties properties = new Properties();
    properties.setProperty("drools.commandService", PersistableRunner.class.getName());
    properties.setProperty("drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty("drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty("drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    SessionConfiguration config = SessionConfiguration.newInstance(properties);
    PersistableRunner service = new PersistableRunner(kbase, config, env);
    Long sessionId = service.getSessionId();
    StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    ProcessInstance processInstance = service.execute(startProcessCommand);
    logger.info("Started process instance {}", processInstance.getId());
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    workItem = handler.getWorkItem();
    assertNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, kbase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
    processInstance = service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
    service.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) JPASignalManagerFactory(org.jbpm.persistence.processinstance.JPASignalManagerFactory) CompleteWorkItemCommand(org.drools.core.command.runtime.process.CompleteWorkItemCommand) Properties(java.util.Properties) JPAWorkItemManagerFactory(org.drools.persistence.jpa.processinstance.JPAWorkItemManagerFactory) StartProcessCommand(org.drools.core.command.runtime.process.StartProcessCommand) WorkItem(org.kie.api.runtime.process.WorkItem) PersistableRunner(org.drools.persistence.PersistableRunner) GetProcessInstanceCommand(org.drools.core.command.runtime.process.GetProcessInstanceCommand) KiePackage(org.kie.api.definition.KiePackage) JPAProcessInstanceManagerFactory(org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) SessionConfiguration(org.drools.core.SessionConfiguration) JpaJDKTimerService(org.drools.persistence.jpa.JpaJDKTimerService) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 7 with PersistableRunner

use of org.drools.persistence.PersistableRunner in project jbpm by kiegroup.

the class SingleSessionCommandServiceTest method testPersistenceSubProcess.

@Test
public void testPersistenceSubProcess() {
    setUp();
    Properties properties = new Properties();
    properties.setProperty("drools.commandService", PersistableRunner.class.getName());
    properties.setProperty("drools.processInstanceManagerFactory", JPAProcessInstanceManagerFactory.class.getName());
    properties.setProperty("drools.workItemManagerFactory", JPAWorkItemManagerFactory.class.getName());
    properties.setProperty("drools.processSignalManagerFactory", JPASignalManagerFactory.class.getName());
    properties.setProperty("drools.timerService", JpaJDKTimerService.class.getName());
    SessionConfiguration config = SessionConfiguration.newInstance(properties);
    InternalKnowledgeBase ruleBase = KnowledgeBaseFactory.newKnowledgeBase();
    KiePackage pkg = getProcessSubProcess();
    ruleBase.addPackages((Collection) Arrays.asList(pkg));
    PersistableRunner service = new PersistableRunner(ruleBase, config, env);
    Long sessionId = service.getSessionId();
    StartProcessCommand startProcessCommand = new StartProcessCommand();
    startProcessCommand.setProcessId("org.drools.test.TestProcess");
    RuleFlowProcessInstance processInstance = (RuleFlowProcessInstance) service.execute(startProcessCommand);
    logger.info("Started process instance {}", processInstance.getId());
    long processInstanceId = processInstance.getId();
    TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
    WorkItem workItem = handler.getWorkItem();
    assertNotNull(workItem);
    service.dispose();
    service = new PersistableRunner(sessionId, ruleBase, config, env);
    GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(processInstance);
    Collection<NodeInstance> nodeInstances = processInstance.getNodeInstances();
    assertEquals(1, nodeInstances.size());
    SubProcessNodeInstance subProcessNodeInstance = (SubProcessNodeInstance) nodeInstances.iterator().next();
    long subProcessInstanceId = subProcessNodeInstance.getProcessInstanceId();
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    RuleFlowProcessInstance subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNotNull(subProcessInstance);
    service.dispose();
    service = new PersistableRunner(sessionId, ruleBase, config, env);
    CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
    completeWorkItemCommand.setWorkItemId(workItem.getId());
    service.execute(completeWorkItemCommand);
    service.dispose();
    service = new PersistableRunner(sessionId, ruleBase, config, env);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(subProcessInstanceId);
    subProcessInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(subProcessInstance);
    getProcessInstanceCommand = new GetProcessInstanceCommand();
    getProcessInstanceCommand.setProcessInstanceId(processInstanceId);
    processInstance = (RuleFlowProcessInstance) service.execute(getProcessInstanceCommand);
    assertNull(processInstance);
    service.dispose();
}
Also used : TestWorkItemHandler(org.jbpm.persistence.session.objects.TestWorkItemHandler) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) JPASignalManagerFactory(org.jbpm.persistence.processinstance.JPASignalManagerFactory) CompleteWorkItemCommand(org.drools.core.command.runtime.process.CompleteWorkItemCommand) Properties(java.util.Properties) JPAWorkItemManagerFactory(org.drools.persistence.jpa.processinstance.JPAWorkItemManagerFactory) StartProcessCommand(org.drools.core.command.runtime.process.StartProcessCommand) WorkItem(org.kie.api.runtime.process.WorkItem) PersistableRunner(org.drools.persistence.PersistableRunner) GetProcessInstanceCommand(org.drools.core.command.runtime.process.GetProcessInstanceCommand) SubProcessNodeInstance(org.jbpm.workflow.instance.node.SubProcessNodeInstance) KiePackage(org.kie.api.definition.KiePackage) JPAProcessInstanceManagerFactory(org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory) SubProcessNodeInstance(org.jbpm.workflow.instance.node.SubProcessNodeInstance) NodeInstance(org.kie.api.runtime.process.NodeInstance) SessionConfiguration(org.drools.core.SessionConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) JpaJDKTimerService(org.drools.persistence.jpa.JpaJDKTimerService) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 8 with PersistableRunner

use of org.drools.persistence.PersistableRunner 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 9 with PersistableRunner

use of org.drools.persistence.PersistableRunner in project jbpm by kiegroup.

the class JPASessionFactory method addInterceptors.

protected void addInterceptors(KieSession ksession) {
    PersistableRunner runner = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
    runner.addInterceptor(new OptimisticLockRetryInterceptor());
    // even though it's added always TransactionLockInterceptor is by default disabled so won't do anything
    runner.addInterceptor(new TransactionLockInterceptor(ksession.getEnvironment()));
    runner.addInterceptor(new ExecutionErrorHandlerInterceptor(ksession.getEnvironment()));
}
Also used : ExecutionErrorHandlerInterceptor(org.jbpm.runtime.manager.impl.error.ExecutionErrorHandlerInterceptor) OptimisticLockRetryInterceptor(org.drools.persistence.jpa.OptimisticLockRetryInterceptor) TransactionLockInterceptor(org.drools.persistence.jta.TransactionLockInterceptor) PersistableRunner(org.drools.persistence.PersistableRunner)

Example 10 with PersistableRunner

use of org.drools.persistence.PersistableRunner in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method testInterceptor.

@Test
public void testInterceptor() {
    String str = "";
    str += "package org.kie.test\n";
    str += "global java.util.List list\n";
    str += "rule rule1\n";
    str += "when\n";
    str += "  Integer(intValue > 0)\n";
    str += "then\n";
    str += "  list.add( 1 );\n";
    str += "end\n";
    str += "\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    ks.newKieBuilder(kfs).buildAll();
    KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    KieSession ksession = ks.getStoreServices().newKieSession(kbase, null, env);
    PersistableRunner sscs = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
    sscs.addInterceptor(new LoggingInterceptor());
    sscs.addInterceptor(new FireAllRulesInterceptor());
    sscs.addInterceptor(new LoggingInterceptor());
    List<?> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    ksession.insert(1);
    ksession.insert(2);
    ksession.insert(3);
    ksession.getWorkItemManager().completeWorkItem(0, null);
    assertEquals(3, list.size());
}
Also used : LoggingInterceptor(org.drools.core.command.impl.LoggingInterceptor) KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) FireAllRulesInterceptor(org.drools.core.command.impl.FireAllRulesInterceptor) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) PersistableRunner(org.drools.persistence.PersistableRunner) Test(org.junit.Test)

Aggregations

PersistableRunner (org.drools.persistence.PersistableRunner)11 Test (org.junit.Test)8 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)6 Properties (java.util.Properties)5 SessionConfiguration (org.drools.core.SessionConfiguration)5 GetProcessInstanceCommand (org.drools.core.command.runtime.process.GetProcessInstanceCommand)5 StartProcessCommand (org.drools.core.command.runtime.process.StartProcessCommand)5 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 JPAWorkItemManagerFactory (org.drools.persistence.jpa.processinstance.JPAWorkItemManagerFactory)5 JPAProcessInstanceManagerFactory (org.jbpm.persistence.processinstance.JPAProcessInstanceManagerFactory)5 JPASignalManagerFactory (org.jbpm.persistence.processinstance.JPASignalManagerFactory)5 RuleFlowProcessInstance (org.jbpm.ruleflow.instance.RuleFlowProcessInstance)5 KiePackage (org.kie.api.definition.KiePackage)5 KieSession (org.kie.api.runtime.KieSession)5 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)5 KieBase (org.kie.api.KieBase)4 ArrayList (java.util.ArrayList)3 UserTransaction (javax.transaction.UserTransaction)3 FireAllRulesInterceptor (org.drools.core.command.impl.FireAllRulesInterceptor)3 LoggingInterceptor (org.drools.core.command.impl.LoggingInterceptor)3