use of org.drools.persistence.PersistableRunner in project drools by kiegroup.
the class JtaTransactionManagerTest method testSingleSessionCommandServiceAndJtaTransactionManagerTogether.
@Test
public void testSingleSessionCommandServiceAndJtaTransactionManagerTogether() throws Exception {
// Initialize drools environment stuff
Environment env = createEnvironment(context);
KieBase kbase = initializeKnowledgeBase(simpleRule);
KieSession commandKSession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
// StatefulKnowledgeSession commandKSession = JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env );
// initialize CSEM
commandKSession.getIdentifier();
PersistableRunner commandService = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) commandKSession).getRunner();
JpaPersistenceContextManager jpm = (JpaPersistenceContextManager) getValueOfField("jpm", commandService);
TransactionTestObject mainObject = new TransactionTestObject();
mainObject.setName("mainCommand");
TransactionTestObject subObject = new TransactionTestObject();
subObject.setName("subCommand");
mainObject.setSubObject(subObject);
UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
ut.begin();
HashMap<String, Object> emEnv = new HashMap<String, Object>();
emEnv.put(COMMAND_ENTITY_MANAGER_FACTORY, emf);
emEnv.put(COMMAND_ENTITY_MANAGER, jpm.getCommandScopedEntityManager());
TransactionTestCommand txTestCmd = new TransactionTestCommand(mainObject, subObject, emEnv);
commandKSession.execute(txTestCmd);
ut.commit();
}
use of org.drools.persistence.PersistableRunner in project drools by kiegroup.
the class JpaPersistentStatefulSessionTest method interceptor.
private void interceptor(final boolean withOOPath) {
final String str = getSimpleRule(withOOPath);
final KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
final KieSession ksession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
final PersistableRunner sscs = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
sscs.addInterceptor(new LoggingInterceptor());
sscs.addInterceptor(new FireAllRulesInterceptor());
sscs.addInterceptor(new LoggingInterceptor());
final List<Integer> list = new ArrayList<>();
ksession.setGlobal("list", list);
insertIntRange(ksession, 1, 3);
ksession.getWorkItemManager().completeWorkItem(0, null);
assertThat(list).hasSize(3);
}
use of org.drools.persistence.PersistableRunner in project drools by kiegroup.
the class JpaPersistentStatefulSessionTest method interceptorOnRollback.
private void interceptorOnRollback(final boolean withOOPath) throws Exception {
final String str = getSimpleRule(withOOPath);
final KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
final KieSession ksession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
final PersistableRunner sscs = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
sscs.addInterceptor(new LoggingInterceptor());
sscs.addInterceptor(new FireAllRulesInterceptor());
sscs.addInterceptor(new LoggingInterceptor());
ChainableRunner runner = sscs.getChainableRunner();
assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
runner = (ChainableRunner) runner.getNext();
assertThat(runner.getClass()).isEqualTo(FireAllRulesInterceptor.class);
runner = (ChainableRunner) runner.getNext();
assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
final UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
ut.begin();
final List<?> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.insert(1);
ut.rollback();
ksession.insert(3);
runner = sscs.getChainableRunner();
assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
runner = (ChainableRunner) runner.getNext();
assertThat(runner.getClass()).isEqualTo(FireAllRulesInterceptor.class);
runner = (ChainableRunner) runner.getNext();
assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
}
use of org.drools.persistence.PersistableRunner in project jbpm by kiegroup.
the class SingleSessionCommandServiceTest method testPersistenceTimer.
@Test
public void testPersistenceTimer() throws Exception {
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());
SessionConfiguration config = SessionConfiguration.newInstance(properties);
config.setOption(TimerJobFactoryOption.get(TimerJobFactoryType.JPA.getId()));
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
Collection<KiePackage> kpkgs = getProcessTimer();
kbase.addPackages(kpkgs);
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());
Thread.sleep(500);
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);
Thread.sleep(5000);
getProcessInstanceCommand = new GetProcessInstanceCommand();
getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
processInstance = service.execute(getProcessInstanceCommand);
assertNull(processInstance);
}
use of org.drools.persistence.PersistableRunner in project jbpm by kiegroup.
the class SingleSessionCommandServiceTest method testPersistenceWorkItemsUserTransaction.
@Test
public void testPersistenceWorkItemsUserTransaction() 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();
UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
ut.begin();
StartProcessCommand startProcessCommand = new StartProcessCommand();
startProcessCommand.setProcessId("org.drools.test.TestProcess");
ProcessInstance processInstance = service.execute(startProcessCommand);
logger.info("Started process instance {}", processInstance.getId());
ut.commit();
TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
WorkItem workItem = handler.getWorkItem();
assertNotNull(workItem);
service.dispose();
service = new PersistableRunner(sessionId, kbase, config, env);
ut.begin();
GetProcessInstanceCommand getProcessInstanceCommand = new GetProcessInstanceCommand();
getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
processInstance = service.execute(getProcessInstanceCommand);
assertNotNull(processInstance);
ut.commit();
service.dispose();
service = new PersistableRunner(sessionId, kbase, config, env);
ut.begin();
CompleteWorkItemCommand completeWorkItemCommand = new CompleteWorkItemCommand();
completeWorkItemCommand.setWorkItemId(workItem.getId());
service.execute(completeWorkItemCommand);
ut.commit();
workItem = handler.getWorkItem();
assertNotNull(workItem);
service.dispose();
service = new PersistableRunner(sessionId, kbase, config, env);
ut.begin();
getProcessInstanceCommand = new GetProcessInstanceCommand();
getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
processInstance = service.execute(getProcessInstanceCommand);
ut.commit();
assertNotNull(processInstance);
service.dispose();
service = new PersistableRunner(sessionId, kbase, config, env);
ut.begin();
completeWorkItemCommand = new CompleteWorkItemCommand();
completeWorkItemCommand.setWorkItemId(workItem.getId());
service.execute(completeWorkItemCommand);
ut.commit();
workItem = handler.getWorkItem();
assertNotNull(workItem);
service.dispose();
service = new PersistableRunner(sessionId, kbase, config, env);
ut.begin();
getProcessInstanceCommand = new GetProcessInstanceCommand();
getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
processInstance = service.execute(getProcessInstanceCommand);
ut.commit();
assertNotNull(processInstance);
service.dispose();
service = new PersistableRunner(sessionId, kbase, config, env);
ut.begin();
completeWorkItemCommand = new CompleteWorkItemCommand();
completeWorkItemCommand.setWorkItemId(workItem.getId());
service.execute(completeWorkItemCommand);
ut.commit();
workItem = handler.getWorkItem();
assertNull(workItem);
service.dispose();
service = new PersistableRunner(sessionId, kbase, config, env);
ut.begin();
getProcessInstanceCommand = new GetProcessInstanceCommand();
getProcessInstanceCommand.setProcessInstanceId(processInstance.getId());
processInstance = service.execute(getProcessInstanceCommand);
ut.commit();
assertNull(processInstance);
service.dispose();
}
Aggregations