Hello!
I've got a problem with using XML beans (v. 2.2) as facts with shadow
proxy turned on (Drools 4.0.7). I'm using a stateful session created by
method newStatefulSession(java.io.InputStream stream). I have a base
WorkingMemory instance with some predefined facts implemented as Apache
XML beans. Then, I write it to an array of bytes using
ByteArrayOutputStream and ObjectOutputStream in order to create
ByteArrayInputStream and create new stateful session using the method
mentioned above. Here is the fragment of my code:
// --> BEGIN OF CODE FRAGMENT
// This is just an initialization of an XML bean fact
MyBean fact1 = MyBean.Factory.newInstance();
fact1.setField1("someText");
fact1.setField2(FieldType.Factory.newInstance());
// props contains some properties defined, but it doesn't matter
RuleAgent ruleAgent = RuleAgent.newRuleAgent(props);
RuleBase ruleBase = ruleAgent.getRuleBase();
// Here I create a base working memory instance
WorkingMemory baseWm = ruleBase.newStatefulSession(false);
// and insert the fact
baseWm.insert(fact1);
// Here I write a base working memory to a byte buffer
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
ObjectOutputStream oos1 = new ObjectOutputStream(baos1);
oos1.writeObject(baseWm);
oos1.close();
byte[] buf1 = baos1.toByteArray();
// and create a new stateful session based on the buffer
ByteArrayInputStream bais1 = new ByteArrayInputStream(buf1);
StatefulSession session1 = ruleBase.newStatefulSession(bais1);
// --> END OF CODE FRAGMENT
When shadow proxies is turned off, everything works fine. I can fire
rules and they see the fact. But when I turn the shadow proxies on, I've
got an exception:
java.io.NotSerializableException: org.example.myBean.MyBeanShadowProxy
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1081)
at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1251)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1075)
at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)
at java.util.HashMap.writeObject(HashMap.java:2336)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:917)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1339)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
at
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1375)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1347)
at
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302)
at com.sample.DroolsTest4.main(DroolsTest4.java:51)
I've checked and MyBean class is serializable. It looks like the proxy
of MyBean is not. What can be wrong here? Did I do any mistake?
Everything is fine when I use ordinary beans, not XML beans.
How the mechanism of creating shadow proxies work?
Best regards!
--
Przemysław Różycki
IT Architect
AMG.net, A Bull Group Company
ul. Łąkowa 29
90-554 Łódź
www.amg.net.pl