<div dir="ltr"><br> A session never was thread safe... it is supposed to be accessed by a single thread. <br> What *is* thread safe is a RuleBase, after its creation. So, if you change your code to have:<br><br>final StatefulSession statefulSession = ruleBase.newStatefulSession();<br>
<br> Inside your thread, it should work. <br> I.e., you share your rulebase, but creates a session for each thread, that seems precisely what you need, since your threads are (correctly) calling session.dispose().<br>
<br> []s<br> Edson<br><br><div class="gmail_quote">2008/8/6 Harsh Jetly <span dir="ltr"><<a href="mailto:harsh.jetly@lntinfotech.com">harsh.jetly@lntinfotech.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
<br>
ricky.nfs wrote:<br>
><br>
> Hi there, I am a newbie on Drools. I tried to create a multi threading<br>
> programs using drools. But in large amount of threads, the result in<br>
> stateful session is not safe anymore. Can anyone help me?<br>
><br>
> private final static Map<Integer,Double> score = new HashMap<Integer,<br>
> Double>();<br>
><br>
> static<br>
> {<br>
> score.put( 0 , 0.0 );<br>
> score.put( 1 , 0.84 );<br>
> score.put( 2 , 1.26 );<br>
> score.put( 3 , 1.68 );<br>
> score.put( 4 , 1.05 ); //Else<br>
> }<br>
><br>
> public static void main(String[] args) throws Exception{<br>
><br>
> Reader source = new InputStreamReader(<br>
> TestStatefulSimpe.class.getResourceAsStream( "/simple/SimpleCATRule.dslr"<br>
> ) );<br>
> Reader dsl = new InputStreamReader(<br>
> TestStatefulSimpe.class.getResourceAsStream( "/simple/SimpleCATRule.dsl" )<br>
> );<br>
> PackageBuilder builder = new PackageBuilder();<br>
> builder.addPackageFromDrl( source, dsl );<br>
><br>
> //get the compiled package (which is serializable)<br>
> Package pkg = builder.getPackage();<br>
><br>
> //add the package to a rulebase (deploy the rule package).<br>
> org.drools.RuleBase ruleBase = RuleBaseFactory.newRuleBase();<br>
> ruleBase.addPackage( pkg );<br>
><br>
> final StatefulSession statefulSession = ruleBase.newStatefulSession();<br>
><br>
> for( int i = 0 ; i < 200000 ; i++ )<br>
> {<br>
> final int idx = i;<br>
> Thread t = new Thread( new Runnable(){<br>
><br>
> @Override<br>
> public void run() {<br>
> int amount = (int) Math.round( ( Math.random()*4 ) );<br>
> TestSimpleVO vo = new TestSimpleVO();<br>
> vo.setDecisionMakersAmount( amount );<br>
> FactHandle fact = statefulSession.insert( vo );<br>
> statefulSession.fireAllRules();<br>
> statefulSession.retract(fact);<br>
> if( score.get(amount).doubleValue() != vo.getScoreFactor() )<br>
> System.out.println( idx + " - " + amount + " : " +<br>
> score.get(amount).doubleValue() + " - " + vo.getScoreFactor() );<br>
> statefulSession.dispose();<br>
> }});<br>
> t.start();<br>
> }<br>
> }<br>
><br>
> And the dslr and dlr attachment :<br>
> <a href="http://www.nabble.com/file/p18564976/SimpleCATRule.dsl" target="_blank">http://www.nabble.com/file/p18564976/SimpleCATRule.dsl</a> SimpleCATRule.dsl<br>
> <a href="http://www.nabble.com/file/p18564976/SimpleCATRule.dslr" target="_blank">http://www.nabble.com/file/p18564976/SimpleCATRule.dslr</a><br>
> SimpleCATRule.dslr<br>
><br>
> I really appreciate for any help or answer.<br>
><br>
> Thanks in advance,<br>
> Ricky<br>
><br>
<font color="#888888"><br>
--<br>
View this message in context: <a href="http://www.nabble.com/Drools-Stateful-Session-Thread-Safe-tp18564976p18848760.html" target="_blank">http://www.nabble.com/Drools-Stateful-Session-Thread-Safe-tp18564976p18848760.html</a><br>
Sent from the drools - dev mailing list archive at Nabble.com.<br>
<br>
_______________________________________________<br>
rules-dev mailing list<br>
<a href="mailto:rules-dev@lists.jboss.org">rules-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-dev</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br> Edson Tirelli<br> JBoss Drools Core Development<br> JBoss, a division of Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>
</div>