[rules-dev] Drools Stateful Session Thread Safe

Edson Tirelli tirelli at post.com
Wed Aug 6 11:39:13 EDT 2008


   A session never was thread safe... it is supposed to be accessed by a
single thread.
   What *is* thread safe is a RuleBase, after its creation. So, if you
change your code to have:

final StatefulSession statefulSession = ruleBase.newStatefulSession();

    Inside your thread, it should work.
    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().

   []s
   Edson

2008/8/6 Harsh Jetly <harsh.jetly at lntinfotech.com>

>
>
>
> ricky.nfs wrote:
> >
> > Hi there, I am a newbie on Drools. I tried to create a multi threading
> > programs using drools. But in large amount of threads, the result in
> > stateful session is not safe anymore. Can anyone help me?
> >
> > private final static Map<Integer,Double> score = new HashMap<Integer,
> > Double>();
> >
> >       static
> >       {
> >               score.put( 0 , 0.0 );
> >               score.put( 1 , 0.84 );
> >               score.put( 2 , 1.26 );
> >               score.put( 3 , 1.68 );
> >               score.put( 4 , 1.05 ); //Else
> >       }
> >
> >       public static void main(String[] args) throws Exception{
> >
> >               Reader source = new InputStreamReader(
> > TestStatefulSimpe.class.getResourceAsStream( "/simple/SimpleCATRule.dslr"
> > ) );
> >               Reader dsl = new InputStreamReader(
> > TestStatefulSimpe.class.getResourceAsStream( "/simple/SimpleCATRule.dsl"
> )
> > );
> >               PackageBuilder builder = new PackageBuilder();
> >               builder.addPackageFromDrl( source, dsl );
> >
> >               //get the compiled package (which is serializable)
> >               Package pkg = builder.getPackage();
> >
> >               //add the package to a rulebase (deploy the rule package).
> >               org.drools.RuleBase ruleBase =
> RuleBaseFactory.newRuleBase();
> >               ruleBase.addPackage( pkg );
> >
> >               final StatefulSession statefulSession =
> ruleBase.newStatefulSession();
> >
> >               for( int i = 0 ; i < 200000 ; i++ )
> >               {
> >                       final int idx = i;
> >                       Thread t = new Thread( new Runnable(){
> >
> >                               @Override
> >                               public void run() {
> >                                       int amount = (int) Math.round( (
> Math.random()*4 ) );
> >                                       TestSimpleVO vo = new
> TestSimpleVO();
> >                                       vo.setDecisionMakersAmount( amount
> );
> >                                       FactHandle fact =
> statefulSession.insert( vo );
> >                                       statefulSession.fireAllRules();
> >                                       statefulSession.retract(fact);
> >                                       if(
>  score.get(amount).doubleValue() != vo.getScoreFactor() )
> >                                               System.out.println( idx + "
> - " +  amount + " : " +
> > score.get(amount).doubleValue() + " - " + vo.getScoreFactor() );
> >                                       statefulSession.dispose();
> >                               }});
> >                       t.start();
> >               }
> >       }
> >
> > And the dslr and dlr attachment :
> >  http://www.nabble.com/file/p18564976/SimpleCATRule.dslSimpleCATRule.dsl
> >  http://www.nabble.com/file/p18564976/SimpleCATRule.dslr
> > SimpleCATRule.dslr
> >
> > I really appreciate for any help or answer.
> >
> > Thanks in advance,
> > Ricky
> >
>
> --
> View this message in context:
> http://www.nabble.com/Drools-Stateful-Session-Thread-Safe-tp18564976p18848760.html
> Sent from the drools - dev mailing list archive at Nabble.com.
>
> _______________________________________________
> rules-dev mailing list
> rules-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-dev
>



-- 
Edson Tirelli
JBoss Drools Core Development
JBoss, a division of Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-dev/attachments/20080806/ad75dfc6/attachment.html 


More information about the rules-dev mailing list