Hi there,
I apologize in advance if this seems like a trivial question, but I can't
seem to figure out what the issue is. i am trying to serialize a stateful
session to file in a very simple test program. So I am using the code from
Michael Bali's book to define a stateful drools session:
//
package sertest;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import org.drools.KnowledgeBase;
import org.drools.common.DroolsObjectInputStream;
import org.drools.common.DroolsObjectOutputStream;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.marshalling.Marshaller;
import org.drools.marshalling.MarshallerFactory;
import org.drools.marshalling.ObjectMarshallingStrategy;
import org.drools.marshalling.ObjectMarshallingStrategyAcceptor;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.FactHandle;
/**
*
*
*/
public class DroolsService implements Serializable
{
private static final long serialVersionUID = 3L;
private transient KnowledgeBase knowledgeBase;
private transient StatefulKnowledgeSession statefulSession;
public DroolsService( KnowledgeBase knowledgeBase )
{
this.knowledgeBase = knowledgeBase;
statefulSession = createKnowledgeSession();
}
private StatefulKnowledgeSession createKnowledgeSession()
{
StatefulKnowledgeSession session =
knowledgeBase.newStatefulKnowledgeSession();
return session;
}
public void insertOrUpdate( Object fact )
{
if( fact == null )
{
return;
}
FactHandle factHandle = statefulSession.getFactHandle( fact );
if( factHandle == null )
{
statefulSession.insert( fact );
}
else
{
statefulSession.update( factHandle, fact );
}
}
public void executeRules()
{
KnowledgeRuntimeLogger logger =
KnowledgeRuntimeLoggerFactory.newFileLogger(statefulSession,
"accessPackageLog");
statefulSession.fireAllRules();
logger.close();
}
public void terminate()
{
statefulSession.dispose();
}
private void writeObject( ObjectOutputStream out ) throws IOException
{
out.defaultWriteObject();
DroolsObjectOutputStream droolsOut = new DroolsObjectOutputStream(
(OutputStream) out );
droolsOut.writeObject( knowledgeBase );
Marshaller marshaller = createSerializableMarshaller( knowledgeBase
);
marshaller.marshall( droolsOut, statefulSession );
}
private void readObject( ObjectInputStream in ) throws IOException,
ClassNotFoundException
{
in.defaultReadObject();
DroolsObjectInputStream droolsIn = new DroolsObjectInputStream(
(InputStream) in );
this.knowledgeBase = (KnowledgeBase) droolsIn.readObject();
Marshaller marshaller = createSerializableMarshaller( knowledgeBase
);
statefulSession = marshaller.unmarshall( droolsIn );
}
private 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;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
In the test client I just do the following:
//
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
DroolsService droolsService = new DroolsService( kbase );
File file = new File("out.ser");
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(droolsService);
oos.close();
///////////////////////////////////////////////////////////////////
The code then trows an exception:
Exception in thread "main" java.io.IOException: Write error
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:260)
at
java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1838)
at
java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1747)
at
java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1249)
at
java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1203)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1387)
at
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at
java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1538)
at
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:329)
at sertest.Main.main(Main.java:30)
If I take out the last 2 lines in the writeObject method above (i.e. don't
use a marshaller to write out the stateful session) then there is no
exception.
Any ideas what might be causing this??
Kind regards,
Chris
--
View this message in context:
http://drools-java-rules-engine.46999.n3.nabble.com/persisting-a-stateful...
Sent from the Drools - User mailing list archive at
Nabble.com.