Hi,

   I fixed a problem related to deadlock when sharing packages among multiple kbases a couple weeks ago. Not sure if it is the same, but try trunk and let me know, plz.

   Meanwhile, I am curious why you can't simply create a kbase with all your rules and share the kbase among all your requests, just creating one session per request?

    Reason is that adding packages to a kbase for each request still adds unnecessary overhead to your application, and kbases are designed to be shared in such cases. If you need to orchestrate multiple execution paths for your rules, you could use rule flow or any other coordination mechanism. Finally, Rete is known by performing really well with increasing number of rules, since the performance is not dependent on the number of existing rules, but on the number of affected constraints.

   Just my .02c

   Edson


2009/12/22 malkhafaji <moe.alkhafaji@medcpu.com>

Hello,

I am trying to create multiple sessions, and for each session I will load
one knowledge package representing one drl. Each drl may optionally load
additional knowledge packages (drls). Since all sessions will be accessing
the same list of knowledge packages, and since they take a long time to
compile resources, I decided to create a Map with all possible knowledge
packages that all sessions may use. That worked beautifully saving me
compilation time (it is done at start up).

Now, here is how I am creating the static map:

private static ConcurrentMap<String, KnowledgePackage> knowledgePackages =
new
               ConcurrentHashMap<String, KnowledgePackage>();

Here is how I am initializing it:

               KnowledgeBuilder kbuilder = null;

               for (MedCPUKnowledgeBase medcpuKnowledgeBase : medcpuKnowledgeBases) {
                       try {
                               kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

                               // This call actually compiles the drl file.

kbuilder.add(ResourceFactory.newClassPathResource(medcpuKnowledgeBase.getFileName()),
                                               ResourceType.DRL);

                               KnowledgeBuilderErrors errors = kbuilder.getErrors();

                               if (errors.size() > 0) {
                                       .....
                               } else {
                                       knowledgePackages.put(medcpuKnowledgeBase.getFileName(),
                                                       [the package just added above]);
                               }
                       } catch(Exception ex) {
                               ....
                               } else {
                                       .....
                               }
                       }
               }


Each session will do this to get the pre-compiled KnowledgePackage:

KnowledgePackage knowledgePackage = knowledgePackages.get(kb);
......
Collection<KnowledgePackage> packages = new ArrayList<KnowledgePackage>();
packages.add(knowledgePackage);

// The problem is this guy (after a relatively small number of requests each
opening a new session) just gets stuck!! It does not throw an exception and
it does not return.
this.knowledgeBase.addKnowledgePackages(packages);

When I did not share the KnowledgePackages like I did above, I was able to
get thousands of requests with each having its own session, knowledgebase,
and its own knowledge packages after they compile the respective drls.
However, because this was slow compiling the same drls over and over again I
switched to the design above which after only 20 or 30 requests it gets
stuck on the line above. I still have a knowledgebase per session per
request, but now I am sharing the knowledge packages.

I cannot find anywhere where it says that KnowledgePackage objects are not
thread-safe (not safe to be shared between sessions/threads). Any idea why I
am stuck on the line above? Thanks!
--
View this message in context: http://n3.nabble.com/Sessions-Sharing-KnowledgeBase-tp96894p96894.html
Sent from the Drools - User mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users



--
 Edson Tirelli
 JBoss Drools Core Development
 JBoss by Red Hat @ www.jboss.com