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();
}
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();
}
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();
}
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()));
}
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());
}
Aggregations