[rules-users] Sessions Sharing KnowledgeBase

malkhafaji moe.alkhafaji at medcpu.com
Tue Dec 22 00:09:42 EST 2009


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.



More information about the rules-users mailing list