Permalink
Browse files

JBRULES-3146 rules with timers do not persist

(cherry picked from commit 0c13299)
  • Loading branch information...
1 parent e1ea603 commit a9f80d30cffce945a8814c4a8ea42f8ff7d7931a @mdproctor mdproctor committed Sep 16, 2011
View
39 jbpm-flow-builder/src/test/java/org/jbpm/integrationtests/ProcessTimerTest.java
@@ -8,14 +8,21 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import org.drools.ClockType;
+import org.drools.KnowledgeBaseFactory;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
+import org.drools.SessionConfiguration;
import org.drools.StatefulSession;
import org.drools.common.InternalWorkingMemory;
import org.drools.compiler.DroolsError;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
+import org.drools.runtime.KnowledgeSessionConfiguration;
+import org.drools.runtime.conf.ClockTypeOption;
+import org.drools.time.SessionPseudoClock;
import org.jbpm.JbpmTestCase;
import org.jbpm.Message;
import org.jbpm.process.instance.InternalProcessRuntime;
@@ -685,7 +692,15 @@ public void testMultipleTimers() throws Exception {
Package pkg = builder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
- final StatefulSession session = ruleBase.newStatefulSession();
+
+ SessionConfiguration conf = new SessionConfiguration();
+ conf.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) );
+
+ final StatefulSession session = ruleBase.newStatefulSession(conf, null);
+ SessionPseudoClock clock = ( SessionPseudoClock) session.getSessionClock();
+ clock.advanceTime( 300,
+ TimeUnit.MILLISECONDS );
+
List<String> myList = new ArrayList<String>();
session.setGlobal("myList", myList);
@@ -695,11 +710,10 @@ public void run() {
}
}).start();
- ProcessInstance processInstance = ( ProcessInstance )
- session.startProcess("org.drools.timer");
+ ProcessInstance processInstance = ( ProcessInstance ) session.startProcess("org.drools.timer");
assertEquals(0, myList.size());
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
- assertEquals(2, ((InternalProcessRuntime) ((InternalWorkingMemory) session).getProcessRuntime()).getTimerManager().getTimers().size());
+ assertEquals(2, ((InternalProcessRuntime) ((InternalWorkingMemory) session).getProcessRuntime()).getTimerManager().getTimers().size());
final StatefulSession session2 = getSerialisedStatefulSession( session );
myList = (List<String>) session2.getGlobal( "myList" );
@@ -712,16 +726,15 @@ public void run() {
assertEquals(2, ((InternalProcessRuntime) ((InternalWorkingMemory) session2).getProcessRuntime()).getTimerManager().getTimers().size());
- try {
- Thread.sleep(300);
- } catch (InterruptedException e) {
- // do nothing
- }
+ clock = ( SessionPseudoClock) session2.getSessionClock();
+ clock.advanceTime( 500,
+ TimeUnit.MILLISECONDS );
assertEquals(1, myList.size());
assertEquals("Executing timer2", myList.get(0));
session2.halt();
final StatefulSession session3 = getSerialisedStatefulSession( session2 );
+ session3.setGlobal("myList", myList);
myList = (List<String>) session.getGlobal( "myList" );
new Thread(new Runnable() {
@@ -730,11 +743,9 @@ public void run() {
}
}).start();
- try {
- Thread.sleep(400);
- } catch (InterruptedException e) {
- // do nothing
- }
+ clock = ( SessionPseudoClock) session3.getSessionClock();
+ clock.advanceTime( 500,
+ TimeUnit.MILLISECONDS );
assertEquals(2, myList.size());
session3.halt();
View
5 jbpm-flow-builder/src/test/java/org/jbpm/integrationtests/SerializationHelper.java
@@ -15,6 +15,7 @@
import org.drools.marshalling.Marshaller;
import org.drools.marshalling.MarshallerFactory;
import org.drools.marshalling.ObjectMarshallingStrategy;
+import org.drools.reteoo.ReteooStatefulSession;
import org.drools.runtime.StatefulKnowledgeSession;
/**
@@ -65,10 +66,10 @@ public static StatefulSession getSerialisedStatefulSession(StatefulSession sessi
bos.close();
// Get the bytes of the serialized object
- final byte[] b1 = bos.toByteArray();
+ final byte[] b1 = bos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream( b1 );
- StatefulSession session2 = ruleBase.newStatefulSession( bais );
+ StatefulSession session2 = ruleBase.newStatefulSession( bais, true, ((ReteooStatefulSession)session).getSessionConfiguration() );
bais.close();
bos = new ByteArrayOutputStream();
View
6 jbpm-flow/src/main/java/org/jbpm/marshalling/impl/AbstractProcessInstanceMarshaller.java
@@ -73,13 +73,13 @@
// Output methods
public void writeProcessInstance(MarshallerWriteContext context,
- ProcessInstance processInstance) throws IOException {
+ ProcessInstance processInstance) throws IOException {
WorkflowProcessInstanceImpl workFlow = (WorkflowProcessInstanceImpl) processInstance;
ObjectOutputStream stream = context.stream;
stream.writeLong(workFlow.getId());
stream.writeUTF(workFlow.getProcessId());
stream.writeInt(workFlow.getState());
- stream.writeLong(workFlow.getNodeInstanceCounter());
+ stream.writeLong(workFlow.getNodeInstanceCounter());
SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) workFlow.getContextInstance(SwimlaneContext.SWIMLANE_SCOPE);
if (swimlaneContextInstance != null) {
@@ -107,7 +107,7 @@ public int compare(NodeInstance o1,
writeNodeInstance(context,
nodeInstance);
}
- stream.writeShort(PersisterEnums.END);
+ stream.writeShort(PersisterEnums.END);
List<ContextInstance> exclusiveGroupInstances =
workFlow.getContextInstances(ExclusiveGroup.EXCLUSIVE_GROUP);
View
3 jbpm-flow/src/main/java/org/jbpm/marshalling/impl/ProcessMarshallerImpl.java
@@ -36,6 +36,7 @@ public int compare(org.drools.runtime.process.ProcessInstance o1,
return (int) (o1.getId() - o2.getId());
}
} );
+
for ( org.drools.runtime.process.ProcessInstance processInstance : processInstances ) {
stream.writeShort(PersisterEnums.PROCESS_INSTANCE);
String processType = processInstance.getProcess().getType();
@@ -194,6 +195,7 @@ public void readProcessTimers(MarshallerReaderContext inCtx) throws IOException,
TimerManager timerManager = ((InternalProcessRuntime) ((InternalWorkingMemory) inCtx.wm).getProcessRuntime()).getTimerManager();
timerManager.internalSetTimerId( stream.readLong() );
+
int token;
while ((token = inCtx.readShort()) != PersisterEnums.END) {
@@ -228,3 +230,4 @@ public static TimerInstance readTimer(MarshallerReaderContext context) throws IO
}
}
+
View
4 jbpm-flow/src/main/java/org/jbpm/process/StatefulProcessSession.java
@@ -237,8 +237,8 @@ public Globals getGlobals() {
return globals;
}
- public <T extends SessionClock> T getSessionClock() {
- throw new UnsupportedOperationException();
+ public SessionClock getSessionClock() {
+ return (SessionClock) this.timerService;
}
public void registerChannel(String name, Channel channel) {
View
11 jbpm-flow/src/main/java/org/jbpm/process/instance/ProcessRuntimeImpl.java
@@ -26,6 +26,7 @@
import org.drools.runtime.process.ProcessInstance;
import org.drools.runtime.process.WorkItemManager;
import org.drools.time.AcceptsTimerJobFactoryManager;
+import org.drools.time.impl.DefaultTimerJobFactoryManager;
import org.drools.time.impl.TrackableTimeJobFactoryManager;
import org.drools.util.CompositeClassLoader;
import org.jbpm.process.core.event.EventFilter;
@@ -51,6 +52,10 @@
public ProcessRuntimeImpl(InternalKnowledgeRuntime kruntime) {
this.kruntime = kruntime;
+ AcceptsTimerJobFactoryManager jfm = ( AcceptsTimerJobFactoryManager ) kruntime.getTimerService();
+ if ( jfm.getTimerJobFactoryManager() instanceof DefaultTimerJobFactoryManager ) {
+ jfm.setTimerJobFactoryManager( new TrackableTimeJobFactoryManager() );
+ }
((AcceptsTimerJobFactoryManager)kruntime.getTimerService()).setTimerJobFactoryManager( new TrackableTimeJobFactoryManager() );
((CompositeClassLoader) getRootClassLoader()).addClassLoader( getClass().getClassLoader() );
initProcessInstanceManager();
@@ -63,7 +68,11 @@ public ProcessRuntimeImpl(InternalKnowledgeRuntime kruntime) {
public ProcessRuntimeImpl(AbstractWorkingMemory workingMemory) {
this.workingMemory = workingMemory;
- ((AcceptsTimerJobFactoryManager)workingMemory.getTimerService()).setTimerJobFactoryManager( new TrackableTimeJobFactoryManager() );
+ AcceptsTimerJobFactoryManager jfm = ( AcceptsTimerJobFactoryManager ) workingMemory.getTimerService();
+ if ( jfm.getTimerJobFactoryManager() instanceof DefaultTimerJobFactoryManager ) {
+ jfm.setTimerJobFactoryManager( new TrackableTimeJobFactoryManager() );
+ }
+
this.kruntime = (InternalKnowledgeRuntime) workingMemory.getKnowledgeRuntime();
((CompositeClassLoader) getRootClassLoader()).addClassLoader( getClass().getClassLoader() );
initProcessInstanceManager();

0 comments on commit a9f80d3

Please sign in to comment.