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();
}
}
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());
}
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());
}
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();
}
}
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();
}
Aggregations