[rules-dev] Drools Stateful Session Thread Safe

Harsh Jetly harsh.jetly at lntinfotech.com
Wed Aug 6 06:53:41 EDT 2008




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.dsl SimpleCATRule.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.




More information about the rules-dev mailing list