[rules-users] Write Error when Serializing a stateful session to a file.

Wolfgang Laun wolfgang.laun at gmail.com
Mon Jul 9 06:02:15 EDT 2012


Verify that you can do
        FileOutputStream fos = new FileOutputStream(...);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeInt(1);
        oos.close();
in the same application using the same File and FileOutputStream.
-W

On 09/07/2012, chrisLi <shengtao0077 at 163.com> wrote:
> Hi, all
>
>      I am trying to serialize a StatefulKnowledgeSession into a file as a
> snapshot. But I got  the following error:
>
>   java.io.IOException: Write error
> 	at java.io.FileOutputStream.writeBytes(Native Method)
> 	at java.io.FileOutputStream.write(FileOutputStream.java:282)
> 	at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
> 	at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
> 	at
> java.io.ObjectOutputStream$BlockDataOutputStream.flush(ObjectOutputStream.java:1793)
> 	at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:699)
> 	at java.io.ObjectOutputStream.close(ObjectOutputStream.java:720)
> 	at com.sample.test.EventDataConsumer.run(EventDataConsumer.java:44)
> 	at
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> 	at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> 	at java.lang.Thread.run(Thread.java:662)
>
>     However, when I serialized it to a ByteArrayOutputStream, it works
> well.
>
>  I wrapped the stateful session in a class as a field. And I wrote
> writeObject and readObject methods in this class to provide custom
> serializing behaviour. The code is as follows:
>
>     private void writeObject(ObjectOutputStream out) throws IOException {
> 		System.out.println("writing");
> 		out.defaultWriteObject();
>
> 		DroolsObjectOutputStream droolsOut = new DroolsObjectOutputStream(
> 				(OutputStream) out);
> 		droolsOut.writeObject(knowledgeBase);
>
> 		Marshaller marshaller = createSerializableMarshaller(knowledgeBase);
> 		marshaller.marshall(droolsOut, session);
> 		System.out.println("written");
> 	}
>
> 	private void readObject(ObjectInputStream in) throws IOException,
> 			ClassNotFoundException {
> 		System.out.println("read");
> 		in.defaultReadObject();
>
> 		DroolsObjectInputStream droolsIn = new DroolsObjectInputStream(
> 				(InputStream) in);
> 		this.knowledgeBase = (KnowledgeBase) droolsIn.readObject();
>
> 		Marshaller marshaller = createSerializableMarshaller(knowledgeBase);
> 		session = 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;
> 	}
>
> Out of this class, I utilized the ordinary ObjectoutputStream to serialize
> an instance of this class to a file.
>
> Unfortunately, I got the Exception.
>
> Any idea or response will be appreciated, Thank you very much.
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/Write-Error-when-Serializing-a-stateful-session-to-a-file-tp4018545.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>


More information about the rules-users mailing list