[rules-users] External storage and timers

didierC didier.chevrere at gmail.com
Mon Jun 24 06:01:20 EDT 2013


Hi,

We want to use the external storage API for our Fusion project (Drools 6.0 -
beta2).
We have temporal based rules and when we load an existing session, we get:
...
Caused by: java.lang.NullPointerException
	at
org.drools.core.common.Scheduler$ActivationTimerInputMarshaller.deserialize(Scheduler.java:238)
	at
org.drools.core.marshalling.impl.ProtobufInputMarshaller.readTimer(ProtobufInputMarshaller.java:627)
	at
org.drools.core.marshalling.impl.ProtobufInputMarshaller.readSession(ProtobufInputMarshaller.java:282)
	at
org.drools.core.marshalling.impl.ProtobufInputMarshaller.readSession(ProtobufInputMarshaller.java:154)
	at
org.drools.core.marshalling.impl.ProtobufMarshaller.unmarshall(ProtobufMarshaller.java:114)
	at
org.drools.core.marshalling.impl.ProtobufMarshaller.unmarshall(ProtobufMarshaller.java:56)
	at
org.drools.persistence.SessionMarshallingHelper.loadSnapshot(SessionMarshallingHelper.java:83)
	... 35 more

I can reproduce the problem in a simple test case:
	
public class TestStorage {

	private SimpleKnowledgeSessionStorage storage;

	@Before
	public void initStorage() {
		storage = new SimpleKnowledgeSessionStorage();
	}

	@Test
	public void testStorageWithTimer() throws InterruptedException {
		KieSession session = getSession(null);
		int id = session.getId();

		String rule = "package test\n";
		rule += "import erdf.poc.cep.*;\n";
		rule += "rule \"test\"\n";
		rule += "timer (cron:0/2 * * * * ?)\n";
		rule += "when\n";
		rule += "then\n";
		rule += "insert(\"test\"+System.currentTimeMillis());\n";
		rule += "end";
		addRule(session, rule);

		session.fireAllRules();
		Thread.sleep(2000);
		session.fireAllRules();
		
		final int facts = session.getFactHandles().size();
		Assert.assertTrue(facts>=1);
		session.dispose();

		Thread.sleep(2000);
		
		session = getSession(id);
		addRule(session, rule);
		session.fireAllRules();
		
		Assert.assertTrue(session.getFactHandles().size()>facts);
		session.dispose();
	}

	private KieSession getSession(Integer i) {
		final Environment env = KnowledgeBaseFactory.newEnvironment();
		EnvironmentBuilder envBuilder = new
KnowledgeSessionStorageEnvironmentBuilder(storage);
		env.set(EnvironmentName.TRANSACTION_MANAGER,
envBuilder.getTransactionManager());
		env.set(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER,
envBuilder.getPersistenceContextManager());
		KieStoreServices storeS = KieServices.Factory.get().getStoreServices();
		final KieBase kieBase = KnowledgeBaseFactory.newKnowledgeBase();
		if (i != null) {
			return storeS.loadKieSession(i, kieBase, null, env);
		}
		return storeS.newKieSession(kieBase, null, env);
	}

	private static class SimpleKnowledgeSessionStorage implements
KnowledgeSessionStorage {

		public Map<Integer, SessionInfo> ksessions = new HashMap<Integer,
SessionInfo>();
		public Map<Long, WorkItemInfo> workItems = new HashMap<Long,
WorkItemInfo>();

		public SessionInfo findSessionInfo(Integer id) {
			return ksessions.get(id);
		}

		public void saveOrUpdate(SessionInfo storedObject) {
			ksessions.put(storedObject.getId(), storedObject);
		}

		public void saveOrUpdate(WorkItemInfo workItemInfo) {
			workItems.put(workItemInfo.getId(), workItemInfo);
		}

		public Long getNextWorkItemId() {
			return new Long(workItems.size() + 1);
		}

		public WorkItemInfo findWorkItemInfo(Long id) {
			return workItems.get(id);
		}

		public void remove(WorkItemInfo workItemInfo) {
			workItems.remove(workItemInfo.getId());
		}

		public Integer getNextStatefulKnowledgeSessionId() {
			return ksessions.size() + 1;
		}
	}
}

Any idea ?

Thanks for your help
Didier




--
View this message in context: http://drools.46999.n3.nabble.com/External-storage-and-timers-tp4024486.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list