Search in sources :

Example 86 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class ConcurrentOperationsTest method testExecuteProcessWithAsyncHandler.

@Test(timeout = 10000)
public void testExecuteProcessWithAsyncHandler() throws Exception {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Log", 1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addEnvironmentEntry("TRANSACTION_LOCK_ENABLED", true).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
            Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
            handlers.put("Log", new AsyncWorkItemHandler(((RuntimeEngineImpl) runtime).getManager()));
            return handlers;
        }

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            return listeners;
        }
    }).addAsset(ResourceFactory.newClassPathResource("BPMN2-CustomTask.bpmn2"), ResourceType.BPMN2).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long sessionId = ksession.getIdentifier();
    assertTrue(sessionId == 1);
    runtime = manager.getRuntimeEngine(EmptyContext.get());
    ksession = runtime.getKieSession();
    assertEquals(sessionId, ksession.getIdentifier());
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    ut.begin();
    ProcessInstance processInstance = ksession.startProcess("customtask");
    logger.debug("Started process, committing...");
    ut.commit();
    countDownListener.waitTillCompleted();
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNull(processInstance);
    // dispose session that should not have affect on the session at all
    manager.disposeRuntimeEngine(runtime);
    // close manager which will close session maintained by the manager
    manager.close();
}
Also used : UserTransaction(javax.transaction.UserTransaction) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) DefaultRegisterableItemsFactory(org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Map(java.util.Map) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 87 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class AdHocSubprocessAbortRuntimeManagerTest method testPerCaseRuntimeManagerScopeSignal.

@Test
public void testPerCaseRuntimeManagerScopeSignal() {
    RuntimeEnvironment environment = createEnvironment();
    manager = RuntimeManagerFactory.Factory.get().newPerCaseRuntimeManager(environment, "first");
    assertNotNull(manager);
    testAdHocSubprocess();
}
Also used : RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 88 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class InMemorySingletonRuntimeManagerTest method testCreationOfSessionInMemory.

@Test
public void testCreationOfSessionInMemory() {
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long sessionId = ksession.getIdentifier();
    assertTrue(sessionId == 0);
    runtime = manager.getRuntimeEngine(EmptyContext.get());
    ksession = runtime.getKieSession();
    assertEquals(sessionId, ksession.getIdentifier());
    // dispose session that should not have affect on the session at all
    manager.disposeRuntimeEngine(runtime);
    ksession = manager.getRuntimeEngine(EmptyContext.get()).getKieSession();
    assertEquals(sessionId, ksession.getIdentifier());
    // 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) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 89 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class FireRulesWithListeneronRMTest method testCreationOfSessionWithPersistence.

@Test
public void testCreationOfSessionWithPersistence() throws ParseException {
    String accountRule = "";
    accountRule += "package org.kie.test\n";
    accountRule += "import org.jbpm.runtime.manager.rule.Account\n";
    accountRule += "rule \"Account Eligibility\"\n";
    accountRule += "dialect \"java\" \n";
    accountRule += "when\n";
    accountRule += "$account:Account( getAccountStatus().equals(\"O\"))\n";
    accountRule += "then\n";
    accountRule += "$account.setAccountEligible(true);\n";
    accountRule += "System.out.println(\" Account Eligibility Rule called - then part after changing value  \"+$account);\n";
    accountRule += "end\n";
    accountRule += "\n";
    String customerRule = "";
    customerRule += "package org.kie.test\n";
    customerRule += "import org.jbpm.runtime.manager.rule.OrderEligibility\n";
    customerRule += "import function org.jbpm.runtime.manager.rule.OrderEligibilityCheck.dateDifference\n";
    customerRule += "rule \"Customer Eligibility\"\n";
    customerRule += "dialect \"java\" \n";
    customerRule += "when\n";
    customerRule += " $orderEligibility: OrderEligibility(dateDifference(getOrderDetails().getEndDate(), getOrderDetails().getStartDate()))\n";
    customerRule += "then\n";
    customerRule += " $orderEligibility.setOrderEligibile(true);\n";
    customerRule += "System.out.println(\" Order Eligibility Rule called - then part after changing value  \"+ $orderEligibility);\n";
    customerRule += "end\n";
    customerRule += "\n";
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().addAsset(ResourceFactory.newByteArrayResource(accountRule.getBytes()), ResourceType.DRL).addAsset(ResourceFactory.newByteArrayResource(customerRule.getBytes()), ResourceType.DRL).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    Account accountOpen = new Account("O");
    Account accountClosed = new Account("C");
    FactHandle accountOpen_FH = ksession.insert(accountOpen);
    FactHandle accountClosed_FH = ksession.insert(accountClosed);
    SimpleDateFormat stdDateFormat = new SimpleDateFormat("yyyy/MM/dd");
    Date today = stdDateFormat.parse("2014/03/10");
    Date yesterday = stdDateFormat.parse("2014/03/09");
    Date lastYear = stdDateFormat.parse("2013/03/10");
    OrderDetails lastYearOrder = new OrderDetails(lastYear, today);
    OrderDetails thisYearOrder = new OrderDetails(yesterday, today);
    OrderEligibility lastYearEligibility = new OrderEligibility(lastYearOrder);
    OrderEligibility thisYearEligibility = new OrderEligibility(thisYearOrder);
    FactHandle lye_FH = ksession.insert(lastYearEligibility);
    FactHandle tye_FH = ksession.insert(thisYearEligibility);
    ksession.fireAllRules();
    assertTrue(accountOpen.getAccountEligible());
    assertTrue(!accountClosed.getAccountEligible());
    ksession.delete(accountOpen_FH);
    ksession.delete(accountClosed_FH);
    assertTrue(lastYearEligibility.getOrderEligibile());
    assertTrue(!thisYearEligibility.getOrderEligibile());
    ksession.delete(lye_FH);
    ksession.delete(tye_FH);
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 90 with RuntimeEnvironment

use of org.kie.api.runtime.manager.RuntimeEnvironment in project jbpm by kiegroup.

the class ConcurrentGlobalTimerServiceTest method testSessionPerProcessInstance.

@Test
public void testSessionPerProcessInstance() throws Exception {
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("org/jbpm/test/functional/timer/IntermediateCatchEventTimerCycleWithHT.bpmn2"), ResourceType.BPMN2).schedulerService(globalScheduler).get();
    long startTimeStamp = System.currentTimeMillis();
    long maxEndTime = startTimeStamp + maxWaitTime;
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    // prepare task service with users and groups
    RuntimeEngine engine = manager.getRuntimeEngine(EmptyContext.get());
    TaskService taskService = engine.getTaskService();
    Group grouphr = TaskModelProvider.getFactory().newGroup();
    ((InternalOrganizationalEntity) grouphr).setId("HR");
    User mary = TaskModelProvider.getFactory().newUser();
    ((InternalOrganizationalEntity) mary).setId("mary");
    User john = TaskModelProvider.getFactory().newUser();
    ((InternalOrganizationalEntity) john).setId("john");
    ((InternalTaskService) taskService).addGroup(grouphr);
    ((InternalTaskService) taskService).addUser(mary);
    ((InternalTaskService) taskService).addUser(john);
    manager.disposeRuntimeEngine(engine);
    completedStart = 0;
    for (int i = 0; i < nbThreadsProcess; i++) {
        new StartProcessPerProcessInstanceRunnable(manager, i).run();
    }
    completedTask = 0;
    for (int i = 0; i < nbThreadsTask; i++) {
        new Thread(new CompleteTaskPerProcessInstanceRunnable(manager, i)).start();
    }
    while (completedStart < nbThreadsProcess || completedTask < nbThreadsTask) {
        Thread.sleep(100);
        if (System.currentTimeMillis() > maxEndTime) {
            fail("Failure, did not finish in time most likely hanging");
        }
    }
    // make sure all process instance were completed
    engine = manager.getRuntimeEngine(EmptyContext.get());
    AuditService logService = engine.getAuditService();
    // active
    List<? extends ProcessInstanceLog> logs = logService.findActiveProcessInstances("IntermediateCatchEvent");
    assertNotNull(logs);
    for (ProcessInstanceLog log : logs) {
        logger.debug("Left over {}", log.getProcessInstanceId());
    }
    assertEquals(0, logs.size());
    // completed
    logs = logService.findProcessInstances("IntermediateCatchEvent");
    assertNotNull(logs);
    assertEquals(nbThreadsProcess, logs.size());
    manager.disposeRuntimeEngine(engine);
    logger.debug("Done");
}
Also used : Group(org.kie.api.task.model.Group) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) User(org.kie.api.task.model.User) InternalTaskService(org.kie.internal.task.api.InternalTaskService) TaskService(org.kie.api.task.TaskService) InternalTaskService(org.kie.internal.task.api.InternalTaskService) InternalOrganizationalEntity(org.kie.internal.task.api.model.InternalOrganizationalEntity) AuditService(org.kie.api.runtime.manager.audit.AuditService) ProcessInstanceLog(org.kie.api.runtime.manager.audit.ProcessInstanceLog) Test(org.junit.Test)

Aggregations

RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)177 Test (org.junit.Test)163 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)148 KieSession (org.kie.api.runtime.KieSession)127 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)107 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)101 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)51 AbstractExecutorBaseTest (org.jbpm.test.util.AbstractExecutorBaseTest)49 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)48 HashMap (java.util.HashMap)47 DefaultRegisterableItemsFactory (org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory)42 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)38 ArrayList (java.util.ArrayList)21 TaskSummary (org.kie.api.task.model.TaskSummary)21 NodeTriggeredCountDownProcessEventListener (org.jbpm.test.listener.NodeTriggeredCountDownProcessEventListener)20 AuditService (org.kie.api.runtime.manager.audit.AuditService)20 CountDownAsyncJobListener (org.jbpm.executor.test.CountDownAsyncJobListener)19 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)19 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)19 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)17