[rules-users] ksession.insert() executes sequentially in multithreaded StatefulKnowledgeSessions

Marco Rietveld mrietvel at redhat.com
Thu Oct 27 08:02:13 EDT 2011


Hi Juan Carlos,

Sorry for the late reply.

Would you mind sending me the Sample.drl en .bpmn files as well?

I happen to have a bit of code that I can use to look into this.

Thanks,
Marco

10/06/2011 01:28 PM, juancarlos.fernandezj:
> Hello,
>
> I'm having trouble when trying to insert() facts inside parallel
> StatefulKnowledgeSessions. From a main() java program I start multiple
> threads. Every thread creates new KnowledgeBase and every KnowledgeBase
> creates new StatefulKnowledgeSession so I have one StatefulKnowledgeSession
> in every thread. Once StatefulKnowledgeSession has been created, I insert
> lots of facts in each StatefulKnowledgeSession.
>
> What was expected? I expected to run insert() in parallel, each insert
> inside its thread StatefulKnowledgeSession. 4 threads inserting lots of
> facts inside its own StatefulKnowledgeSession is expected to run in parallel
> and see how CPU usage increases.
>
> What have I seen? when all threads are inserting facts in its own
> StatefulKnowledgeSession I can see that only one CPU is being used so there
> is no parallel insertion in different StatefulKnowledgeSession.
>
> Is there a synchronization inside insert() code? It's so strange. Even if i
> try with 12 threads, I can't see a CPU usage increase when executing
> parallel insert() inside different StatefulKnowledgeSession (threads).
>
> Help please.
>
> This is my thread code (no static objects):
>
> public class KnowledgeSessionThread extends Thread {
> 	
> 	private StatefulKnowledgeSession session;
> 	private Message[] facts;
> 	
>      public KnowledgeSessionThread(Message[] facts) throws Exception {
>      	session = readKnowledgeBase().newStatefulKnowledgeSession();
>      	this.facts = facts;
>      }
>
>      public void run() {
>      	try {
> 	    	for( int i = 0; i<  facts.length; i++ ) {
> 	            session.insert(facts[i]);
> 	    	}
> 	    	session.startProcess("flowId");
> 	    	session.fireAllRules();
> 	    	session.dispose();
> 	    	System.out.println("Thread finished");
>      	} catch( Exception e ) {
>      		e.printStackTrace();
>      	}
>      }
>
>      private KnowledgeBase readKnowledgeBase() throws Exception {
>          KnowledgeBuilder kbuilder =
> KnowledgeBuilderFactory.newKnowledgeBuilder();
>          kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"),
> ResourceType.DRL);
>          kbuilder.add(ResourceFactory.newClassPathResource("Sample.bpmn"),
> ResourceType.BPMN2);
>          KnowledgeBuilderErrors errors = kbuilder.getErrors();
>          if (errors.size()>  0) {
>              for (KnowledgeBuilderError error: errors) {
>                  System.err.println(error);
>              }
>              throw new IllegalArgumentException("Could not parse
> knowledge.");
>          }
>          KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
>          kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
>          return kbase;
>      }
> }
>
> --
> View this message in context:http://drools.46999.n3.nabble.com/ksession-insert-executes-sequentially-in-multithreaded-StatefulKnowledgeSessions-tp3399339p3399339.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


-- 
jBPM/Drools developer
Utrecht, the Netherlands




More information about the rules-users mailing list