我使用此代码来完全序列化/反序列化 drools (5.4.0.CR1) 状态会话对象:
private static void writeObject(ObjectOutputStream out, KnowledgeBase knowledgeBase, StatefulKnowledgeSession session) throws IOException
{
// out.defaultWriteObject();
DroolsObjectOutputStream droolsOut = new DroolsObjectOutputStream(out);
droolsOut.writeObject(knowledgeBase);
Marshaller marshaller = createSerializableMarshaller(knowledgeBase);
marshaller.marshall(droolsOut, session);
}
private static Marshaller createSerializableMarshaller(KnowledgeBase knowledgeBase)
{
ObjectMarshallingStrategyAcceptor acceptor = MarshallerFactory.newClassFilterAcceptor(new String[] { "*.*" });
ObjectMarshallingStrategy strategy = MarshallerFactory.newSerializeMarshallingStrategy(acceptor);
Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase, new ObjectMarshallingStrategy[] { strategy });
return marshaller;
}
private static StatefulKnowledgeSession readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
// in.defaultReadObject();
DroolsObjectInputStream droolsIn = new DroolsObjectInputStream(in);
KnowledgeBase knowledgeBase = (KnowledgeBase) droolsIn.readObject();
Marshaller marshaller = createSerializableMarshaller(knowledgeBase);
StatefulKnowledgeSession statefulSession = marshaller.unmarshall(droolsIn);
return statefulSession;
}
它工作正常,唯一的问题是会话的全局变量丢失了。
我究竟做错了什么?