Search in sources :

Example 91 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class TimerCycleOnBinaryPackageTest method testStartTimerCycleFromDisc.

@Test(timeout = 20000)
public void testStartTimerCycleFromDisc() throws Exception {
    NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("start", 2);
    KieBase kbase = createKnowledgeBaseFromDisc("BPMN2-StartTimerCycle.bpmn2");
    try {
        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
        ksession.addEventListener(countDownListener);
        assertEquals(0, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
        long sessionId = ksession.getIdentifier();
        Environment env = ksession.getEnvironment();
        final List<Long> list = new ArrayList<Long>();
        ksession.addEventListener(new DefaultProcessEventListener() {

            public void beforeProcessStarted(ProcessStartedEvent event) {
                list.add(event.getProcessInstance().getId());
            }
        });
        ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
        countDownListener.waitTillCompleted();
        assertEquals(2, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
        logger.info("dispose");
        ksession.dispose();
        countDownListener = new NodeLeftCountDownProcessEventListener("start", 2);
        ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
        ksession.addEventListener(countDownListener);
        AuditLoggerFactory.newInstance(Type.JPA, ksession, null);
        final List<Long> list2 = new ArrayList<Long>();
        ksession.addEventListener(new DefaultProcessEventListener() {

            public void beforeProcessStarted(ProcessStartedEvent event) {
                list2.add(event.getProcessInstance().getId());
            }
        });
        ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
        countDownListener.waitTillCompleted();
        assertEquals(4, getNumberOfProcessInstances("defaultPackage.TimerProcess"));
        abortProcessInstances(ksession);
        ksession.dispose();
    } finally {
        ksession = createKnowledgeSession(kbase);
        abortProcessInstances(ksession);
        ksession.dispose();
    }
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) ProcessStartedEvent(org.kie.api.event.process.ProcessStartedEvent) Environment(org.kie.api.runtime.Environment) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) TriggerRulesEventListener(org.jbpm.process.instance.event.listeners.TriggerRulesEventListener) Test(org.junit.Test)

Example 92 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class TimerCycleOnBinaryPackageTest method testStartTimerCycleFromDiscDRL.

@Test
@Ignore("beta4 phreak")
public void testStartTimerCycleFromDiscDRL() throws Exception {
    KieBase kbase = createKnowledgeBaseFromDisc("rules-timer.drl");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    long sessionId = ksession.getIdentifier();
    Environment env = ksession.getEnvironment();
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
    ksession.fireAllRules();
    Thread.sleep(5000);
    assertEquals(2, list.size());
    logger.info("dispose");
    ksession.dispose();
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
    AuditLoggerFactory.newInstance(Type.JPA, ksession, null);
    final List<String> list2 = new ArrayList<String>();
    ksession.setGlobal("list", list2);
    ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
    ksession.fireAllRules();
    Thread.sleep(6000);
    assertEquals(3, list2.size());
}
Also used : KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) TriggerRulesEventListener(org.jbpm.process.instance.event.listeners.TriggerRulesEventListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 93 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class TimerCycleOnBinaryPackageTest method testStartTimerCycleFromClasspathDRL.

@Test
@Ignore("beta4 phreak")
public void testStartTimerCycleFromClasspathDRL() throws Exception {
    KieBase kbase = createKnowledgeBaseWithoutDumper("rules-timer.drl");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    long sessionId = ksession.getIdentifier();
    Environment env = ksession.getEnvironment();
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
    ksession.fireAllRules();
    Thread.sleep(5000);
    assertEquals(2, list.size());
    logger.info("dispose");
    ksession.dispose();
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
    AuditLoggerFactory.newInstance(Type.JPA, ksession, null);
    final List<String> list2 = new ArrayList<String>();
    ksession.setGlobal("list", list2);
    ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession().addEventListener(new TriggerRulesEventListener(ksession));
    ksession.fireAllRules();
    Thread.sleep(5000);
    assertEquals(3, list2.size());
}
Also used : KieBase(org.kie.api.KieBase) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) TriggerRulesEventListener(org.jbpm.process.instance.event.listeners.TriggerRulesEventListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 94 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class MemoryLeakTest method createKnowledgeSessionStartProcessEtc.

private void createKnowledgeSessionStartProcessEtc(KieBase kbase) {
    logger.info("session count=" + kbase.getKieSessions().size());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, getKnowledgeSessionConfiguration(), env);
    addEventListenersToSession(ksession);
    /**
     * The following log line caused the memory leak.
     * The specific (reverse-ordered) stack trace is the following:
     *
     *   MemoryLeakTest.createKnowledgeSessionStartProcessEtc(KnowledgeBase) calls kbase.getKieSessions()
     *   ..
     *   KnowledgeBaseImpl.getStatefulKnowledgeSessions() line: 186
     *   StatefulKnowledgeSessionImpl.<init>(ReteooWorkingMemory, KnowledgeBase) line: 121
     *   ReteooStatefulSession(AbstractWorkingMemory).setKnowledgeRuntime(InternalKnowledgeRuntime) line: 1268
     *   ReteooStatefulSession(AbstractWorkingMemory).createProcessRuntime() line: 342
     *   ProcessRuntimeFactory.newProcessRuntime(AbstractWorkingMemory) line: 12
     *   ProcessRuntimeFactoryServiceImpl.newProcessRuntime(AbstractWorkingMemory) line: 1
     *   ProcessRuntimeFactoryServiceImpl.newProcessRuntime(AbstractWorkingMemory) line: 10
     *   ProcessRuntimeImpl.<init>(AbstractWorkingMemory) line: 84
     *   ProcessRuntimeImpl.initProcessEventListeners() line: 215
     *
     * And ProcessRuntimeImpl.initProcessEventListeners() is what adds a new listener
     * to AbstractRuleBase.eventSupport.listeners via this line (235):
     *   kruntime.getKnowledgeBase().addEventListener(knowledgeBaseListener);
     *
     * The StatefulKnowledgeSessionImpl instance created in this .getStatefulKnowledgeSessions()
     * method is obviously never disposed, which means that the listener is never removed.
     * The listener then contains a link to a field (signalManager) of the ProcessRuntimeImpl,
     * which contains a link to the StatefulKnowledgeSessionImpl instance created here. etc..
     */
    logger.info("session count=" + kbase.getKieSessions().size());
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    try {
        // create process instance, insert into session and start process
        Map<String, Object> processParams = new HashMap<String, Object>();
        String[] fireballVarHolder = new String[1];
        processParams.put("fireball", fireballVarHolder);
        ProcessInstance processInstance = ksession.createProcessInstance(PROCESS_NAME, processParams);
        ksession.insert(processInstance);
        ksession.startProcessInstance(processInstance.getId());
        // after the log line has been added, the DefaultProcessEventListener registered
        // in the addEventListenersToSession() method no longer works?!?
        ksession.fireAllRules();
        // test process variables
        String[] procVar = (String[]) ((WorkflowProcessInstance) processInstance).getVariable("fireball");
        assertEquals("Rule task did NOT fire or complete.", "boom!", procVar[0]);
        // complete task and process
        Map<String, Object> results = new HashMap<String, Object>();
        results.put("chaerg", new SerializableResult("zhrini", 302l, "F", "A", "T"));
        ksession.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), results);
        assertNull(ksession.getProcessInstance(processInstance.getId()));
    } finally {
        // This should clean up all listeners, but doesn't -> see docs above
        ksession.dispose();
    }
}
Also used : TestWorkItemHandler(org.jbpm.bpmn2.objects.TestWorkItemHandler) HashMap(java.util.HashMap) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 95 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class GetProcessInstancesTest method setUp.

@Before
public void setUp() throws Exception {
    context = setupWithPoolingDataSource(JBPM_PERSISTENCE_UNIT_NAME);
    env = createEnvironment(context);
    kbase = createBase();
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    sessionId = ksession.getIdentifier();
    ksession.dispose();
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Before(org.junit.Before)

Aggregations

StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)114 Test (org.junit.Test)79 KieBase (org.kie.api.KieBase)52 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)40 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)30 ArrayList (java.util.ArrayList)29 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)22 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)20 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)18 HashMap (java.util.HashMap)16 Environment (org.kie.api.runtime.Environment)12 KieSession (org.kie.api.runtime.KieSession)11 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)11 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)10 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 WorkItem (org.kie.api.runtime.process.WorkItem)9 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)8 TestWorkItemHandler (org.jbpm.persistence.session.objects.TestWorkItemHandler)8 Resource (org.kie.api.io.Resource)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7