[rules-users] Implementing Timers With Drools Persistence Enabled

nanic23 nanic23 at hotmail.com
Fri Jun 25 19:17:05 EDT 2010


Hi salaboy,

Here is an image of my flow:

http://drools-java-rules-engine.46999.n3.nabble.com/file/n923330/SnapShot.jpg 

Here is the flow definition:

<?xml version="1.0" encoding="UTF-8"?> 
<process xmlns="http://drools.org/drools-5.0/process"
         xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
         xs:schemaLocation="http://drools.org/drools-5.0/process
drools-processes-5.0.xsd"
         type="RuleFlow" name="simple-embeded-timer-with-persistence"
id="simple-embeded-timer-with-persistence" package-name="com.flow" >

  <header>
  </header>

  <nodes>
    <start id="1" name="Start" x="16" y="16" width="48" height="48" />
    <workItem id="2" name="Suspend With Timer" x="96" y="16" width="144"
height="48" >
      <work name="doNothingHandler" >
      </work>
      <timers>
        <timer id="1" delay="500" period="1000" >
        <action type="expression" dialect="mvel" >System.out.println("****
Timer Triggered ****");</action>
        </timer>
      </timers>
    </workItem>
    <actionNode id="3" name="Print Out" x="272" y="20" width="80"
height="40" >
        <action type="expression" dialect="mvel" >System.out.println("****
WorkItem Completed!!! Finishing Flow ****");</action>
    </actionNode>
    <end id="4" name="End" x="384" y="20" width="80" height="40" />
  </nodes>

  <connections>
    <connection from="1" to="2" />
    <connection from="2" to="3" />
    <connection from="3" to="4" />
  </connections>

</process>

Here is the test code: 

        @Test
	public void shouldStartFlowAndSuspend() {
		/*
		 * Persistence enabled - Timer does NOT work!!!
		 */
		ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null,
droolsEnvironment);

		/*
		 * Persistence disabled - Timer DOES work!!!
		 */
		//ksession = kbase.newStatefulKnowledgeSession();
		
		sessionId = ksession.getId();
		
		ksession.getWorkItemManager()
				.registerWorkItemHandler("doNothingHandler", new
DoNothingWorkItemHandler());

		ksession.startProcess("simple-embeded-timer-with-persistence", null);
		
		// start timer thread
		new Thread() {
			@Override
			public void run() {
				System.out.println("**** Timer thread started ****");
				ksession.fireUntilHalt();
	        }
		}.start();
		
		// Allow timer to run for 10s
	    try {
	        Thread.sleep(10000);
	    } catch (InterruptedException e) {
	        e.printStackTrace();
	    }
	}
	
	@Test //NOTE: comment out when not using persistence.
	public void shouldResumeFlowAndComplete() {
		ksession = 
			JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null,
droolsEnvironment);
		
		ksession.getWorkItemManager()
			.registerWorkItemHandler("doNothingHandler", new
DoNothingWorkItemHandler());
		
		ksession.getWorkItemManager().completeWorkItem(1, null);
	}

NOTE: when running WITHOUT persistence the second test method for obvious
reasons will fail so it needs to be commented out.

Persistence is set up correctly, I've checked the database and everything is
being persisted. I can as well resume and complete with no problems BUT the
timer is NOT working.

Let me know if this is enough and/or if you have any question.

Thanks,

Nick.

-- 
View this message in context: http://drools-java-rules-engine.46999.n3.nabble.com/Implementing-Timers-With-Drools-Persistence-Enabled-tp923142p923330.html
Sent from the Drools - User mailing list archive at Nabble.com.



More information about the rules-users mailing list