Search in sources :

Example 1 with KieContainerSessionsPool

use of org.kie.api.runtime.KieContainerSessionsPool in project drools by kiegroup.

the class SessionsPoolTest method testStatelessKieSessionsPool.

@Test
public void testStatelessKieSessionsPool() {
    KieContainerSessionsPool pool = getKieContainer().newKieSessionsPool(1);
    StatelessKieSession session = pool.newStatelessKieSession();
    List<String> list = new ArrayList<>();
    session.setGlobal("list", list);
    session.execute("test");
    assertEquals(1, list.size());
    list.clear();
    session.execute("test");
    assertEquals(1, list.size());
}
Also used : KieContainerSessionsPool(org.kie.api.runtime.KieContainerSessionsPool) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 2 with KieContainerSessionsPool

use of org.kie.api.runtime.KieContainerSessionsPool in project drools by kiegroup.

the class SessionsPoolTest method testListenersReset.

@Test
public void testListenersReset() {
    final KieContainerSessionsPool pool = getKieContainer().newKieSessionsPool(1);
    KieSession ksession = pool.newKieSession();
    try {
        ksession.addEventListener(new DefaultAgendaEventListener());
        ksession.addEventListener(new DefaultRuleRuntimeEventListener());
        ((RuleEventManager) ksession).addEventListener(new RuleEventListener() {
        });
    } finally {
        ksession.dispose();
    }
    ksession = pool.newKieSession();
    try {
        Assertions.assertThat(ksession.getAgendaEventListeners()).hasSize(0);
        Assertions.assertThat(ksession.getRuleRuntimeEventListeners()).hasSize(0);
        Assertions.assertThat(((EventSupport) ksession).getRuleEventSupport().getEventListeners()).hasSize(0);
    } finally {
        ksession.dispose();
    }
}
Also used : KieContainerSessionsPool(org.kie.api.runtime.KieContainerSessionsPool) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) DefaultRuleRuntimeEventListener(org.drools.core.event.DefaultRuleRuntimeEventListener) Test(org.junit.Test)

Example 3 with KieContainerSessionsPool

use of org.kie.api.runtime.KieContainerSessionsPool in project drools by kiegroup.

the class SessionsPoolTest method testKieSessionsPoolInMultithreadEnv.

@Test
public void testKieSessionsPoolInMultithreadEnv() throws InterruptedException, ExecutionException {
    KieContainerSessionsPool pool = getKieContainer().newKieSessionsPool(4);
    final int THREAD_NR = 10;
    ExecutorService executor = Executors.newFixedThreadPool(THREAD_NR, r -> {
        final Thread t = new Thread(r);
        t.setDaemon(true);
        return t;
    });
    try {
        CompletionService<Boolean> ecs = new ExecutorCompletionService<>(executor);
        for (int i = 0; i < THREAD_NR; i++) {
            ecs.submit(() -> {
                try {
                    KieSession ksession = pool.newKieSession();
                    try {
                        checkKieSession(ksession);
                    } finally {
                        ksession.dispose();
                    }
                    return true;
                } catch (final Exception e) {
                    return false;
                }
            });
        }
        boolean success = true;
        for (int i = 0; i < THREAD_NR; i++) {
            success = ecs.take().get() && success;
        }
        assertTrue(success);
    } finally {
        executor.shutdown();
    }
    pool.shutdown();
    try {
        pool.newKieSession();
        fail("after pool shutdown it shouldn't be possible to get sessions from it");
    } catch (IllegalStateException e) {
    }
}
Also used : KieContainerSessionsPool(org.kie.api.runtime.KieContainerSessionsPool) ExecutorService(java.util.concurrent.ExecutorService) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 4 with KieContainerSessionsPool

use of org.kie.api.runtime.KieContainerSessionsPool in project drools by kiegroup.

the class SessionsPoolTest method testKieSessionsPool.

@Test
public void testKieSessionsPool() {
    KieContainerSessionsPool pool = getKieContainer().newKieSessionsPool(1);
    KieSession ksession = pool.newKieSession();
    try {
        checkKieSession(ksession);
    } finally {
        ksession.dispose();
    }
    try {
        ksession.insert("test2");
        fail("it shouldn't be possible to operate on a disposed session even if created from a pool");
    } catch (Exception e) {
    }
    KieSession ksession2 = pool.newKieSession();
    // using a pool with only one session so it should return the same one as before
    assertSame(ksession, ksession2);
    assertNull(ksession2.getGlobal("list"));
    checkKieSession(ksession2);
    pool.shutdown();
    try {
        ksession.insert("test3");
        fail("after pool shutdown all sessions created from it should be disposed");
    } catch (IllegalStateException e) {
    }
    try {
        pool.newKieSession();
        fail("after pool shutdown it shouldn't be possible to get sessions from it");
    } catch (IllegalStateException e) {
    }
}
Also used : KieContainerSessionsPool(org.kie.api.runtime.KieContainerSessionsPool) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 KieContainerSessionsPool (org.kie.api.runtime.KieContainerSessionsPool)4 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)4 KieSession (org.kie.api.runtime.KieSession)3 ExecutionException (java.util.concurrent.ExecutionException)2 ArrayList (java.util.ArrayList)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 ExecutorService (java.util.concurrent.ExecutorService)1 DefaultAgendaEventListener (org.drools.core.event.DefaultAgendaEventListener)1 DefaultRuleRuntimeEventListener (org.drools.core.event.DefaultRuleRuntimeEventListener)1 RuleEventListener (org.kie.internal.event.rule.RuleEventListener)1 RuleEventManager (org.kie.internal.event.rule.RuleEventManager)1