Search in sources :

Example 1 with PersistableRunner

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();
}
Also used : UserTransaction(javax.transaction.UserTransaction) HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) DroolsPersistenceUtil.createEnvironment(org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) PersistableRunner(org.drools.persistence.PersistableRunner) JpaPersistenceContextManager(org.drools.persistence.jpa.JpaPersistenceContextManager) Test(org.junit.Test)

Example 2 with PersistableRunner

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoggingInterceptor(org.drools.core.command.impl.LoggingInterceptor) KieBase(org.kie.api.KieBase) FireAllRulesInterceptor(org.drools.core.command.impl.FireAllRulesInterceptor) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) PersistableRunner(org.drools.persistence.PersistableRunner)

Example 3 with PersistableRunner

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);
}
Also used : UserTransaction(javax.transaction.UserTransaction) ChainableRunner(org.drools.core.runtime.ChainableRunner) LoggingInterceptor(org.drools.core.command.impl.LoggingInterceptor) KieBase(org.kie.api.KieBase) FireAllRulesInterceptor(org.drools.core.command.impl.FireAllRulesInterceptor) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) PersistableRunner(org.drools.persistence.PersistableRunner)

Example 4 with PersistableRunner

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);
}
Also used : JPASignalManagerFactory(org.jbpm.persistence.processinstance.JPASignalManagerFactory) Properties(java.util.Properties) JPAWorkItemManagerFactory(org.drools.persistence.jpa.processinstance.JPAWorkItemManagerFactory) StartProcessCommand(org.drools.core.command.runtime.process.StartProcessCommand) 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) SessionConfiguration(org.drools.core.SessionConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 5 with PersistableRunner

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();
}
Also used : UserTransaction(javax.transaction.UserTransaction) 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) InitialContext(javax.naming.InitialContext) 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)

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