I also was getting this error. I brought out package building logic to execute once, and KnowledgeSession related logic to repeat the required number of times.

 

Seems there required some delay in preparing rules related classes into one package during iteration and loading the same. Above step was working fine for me.

 

Pseudo code:

 

          KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

 

      // this will parse and compile in one step

     

      // get the compiled packages (which are serializable)

      Collection<KnowledgePackage> pkgs = null;

 

      // add the packages to a knowledgebase (deploy the knowledge packages).

      KnowledgeBase kbase = null;

     

      StatefulKnowledgeSession ksession = null;

          // constructor to be called once

public RuleEngine (byte[] rules) {

            kbuilder.add(ResourceFactory.newByteArrayResource(rules),ResourceType.DRL);

//          Check the builder for errors

            if (kbuilder.hasErrors()) {

                  System.out.println(kbuilder.getErrors().toString());

                  throw new RuntimeException("Unable to compile drl.");

            }

            pkgs = kbuilder.getKnowledgePackages();

 

            // add the packages to a knowledgebase (deploy the knowledge packages).

            kbase = KnowledgeBaseFactory.newKnowledgeBase();

            kbase.addKnowledgePackages(pkgs);        

      }

     

// this method will be called required number of times

      public void fire(Object obj) {     

           

            ksession = kbase.newStatefulKnowledgeSession();

            ksession.addEventListener(new DebugAgendaEventListener());

            ksession.addEventListener(new DebugWorkingMemoryEventListener());

 

            // setup the audit logging

            //KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log/drools.log");

 

            ksession.insert(obj);

 

            ksession.fireAllRules();

 

            //logger.close();

 

            ksession.dispose();

      }

 

 

Check this out.

 

 

Thanks & Regards

Santhosh Pallerla

 

From: rules-users-bounces@lists.jboss.org [mailto:rules-users-bounces@lists.jboss.org] On Behalf Of nicolas
Sent: Thursday, September 29, 2011 8:09 AM
To: rules-users@lists.jboss.org
Subject: [rules-users] Concurrency problem in PackageClassLoader

 

Hi,

We are using a single (Drools 5.1.1) Stateless Sessions for the parallel processing of thousands of facts.

>From time to time we experienced the following error :

org.drools.runtime.rule.ConsequenceException: rule: Priorite - Groupe: TMK2612G01, P10

      at org.drools.runtime.rule.impl.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:39)
      at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:927)
      at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:856)
      at org.drools.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1071)
      at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:785)
      at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:751)
      at org.drools.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:218)
      at acme.srv.ept.service.impl.traitement.MoteurTraitementDemandes.traiterDemande(MoteurTraitementDemandes.java:121)
(...)
Caused by: java.util.ConcurrentModificationException
      at java.util.HashMap$AbstractMapIterator.checkConcurrentMod(Unknown Source)
      at java.util.HashMap$AbstractMapIterator.makeNext(Unknown Source)
      at java.util.HashMap$KeyIterator.next(Unknown Source)
      at java.util.HashMap.analyzeMap(Unknown Source)
      at java.util.HashMap.rehash(Unknown Source)
      at java.util.HashMap.rehash(Unknown Source)
      at java.util.HashMap.putImpl(Unknown Source)
      at java.util.HashMap.put(Unknown Source)
      at org.drools.rule.JavaDialectRuntimeData$PackageClassLoader.loadClass(JavaDialectRuntimeData.java:503)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:609)
      at  acme.srv.ept.regle.Rule_Priorite___Groupe__TMK2612G01__P10_0DefaultConsequenceInvoker.evaluate(Rule_Priorite___Groupe__TMK2612G01__P10_0DefaultConsequenceInvoker.java:29)
      at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:917)
      ... 49 more

We get the same error when we used a pool of Stateless Sessions (serial access) based on the same KnowledgeBase for the processing.


Thanks,