[jboss-user] [jBPM] - Using KnowledgeAgentEventListener to detect new processes (v5.4.0)

cold gin do-not-reply at jboss.com
Wed May 29 11:43:41 EDT 2013

cold gin [https://community.jboss.org/people/cold_gin] created the discussion

"Using KnowledgeAgentEventListener to detect new processes (v5.4.0)"

To view the discussion, visit: https://community.jboss.org/message/820162#820162

I am trying to implement a KnowledgeAgentEventListener so that I can detect *newly added* processes, and I would also like to be able to detect the creation of new packages somehow. So far, I have created a KnowledgeAgent, added a KnowledgeAgentEventListener, but all that comes back on the beforeResourceAdded() and afterResourceAdded() event handlers are RESOURCE_DELETED statuses. When I print out the event source object type, it's ChangeSetImpl, which I guess makes sense since I am adding the change set as the resource type to the knowledge agent, and I guess that the change set is being replaced, so I get the RESOURCE_DELETED status. But I am trying to figure out how to configure the knowledge agent so that I can receive more fine-grained change events such as PROCESS_ADDED and PROCESS_UPDATED statuses (if such jbpm statuses exist).

Here is the relevant code so far:

KnowledgeAgentConfiguration agentConfig = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
// Tells jBPM to update existing knowledge base and not to create a new one each time: 
agentConfig.setProperty("drools.agent.newInstance", "false");
// Tells jBPM what to scan: 
agentConfig.setProperty("drools.agent.monitorChangeSetEvents", "true");        
agentConfig.setProperty("drools.agent.scanDirectories", "true");
agentConfig.setProperty("drools.agent.scanResources", "true");
KnowledgeAgent knowledgeAgent = KnowledgeAgentFactory.newKnowledgeAgent(knowledgeAgentName,agentConfig);
org.drools.io.Resource changeSetResource = createChangeSetResouceUsing(changeSetXmlAsString);
            knowledgeAgent.addEventListener(new KnowledgeAgentEventListener() {
                public void beforeChangeSetApplied(BeforeChangeSetAppliedEvent event) {
                    logger.debug("beforeChangeSetApplied() event handler called");                    
                public void afterChangeSetApplied(AfterChangeSetAppliedEvent eve nt) {
                    logger.debug("afterChangeSetApplied() event handler called");                    
                public void beforeChangeSetProcessed(BeforeChangeSetProcessedEvent event) {
                    logger.debug("beforeChangeSetProcessed() event handler called");    
                public void afterChangeSetProcessed(AfterChangeSetProcessedEvent event) {
                    logger.debug("afterChangeSetProcessed() event handler called");
                public void beforeResourceProcessed(BeforeResourceProcessedEvent event) {
                    logger.debug("beforeResourceProcessed() event handler called");
                    logger.debug("beforeResourceProcessed() - event.getResource(): " + event.getResource().toString());
                    logger.debug("beforeResourceProcessed() - event.getResourceType(): [ " + event.getResourceType().getName() + ", " + event.getResourceType().getDescription() + " ]");
                    logger.debug("beforeResourceProcessed() - event.getStatus().name(): " + event.getStatus().name());
                    logger.debug("beforeResourceProcessed() - event.getStatus(): " + event.getStatus());
                    logger.debug("beforeResourceProcessed() - event.getSource().getClass().getCanonicalName(): " + event.getSource().getClass().getCanonicalName());
                public void afterResourceProcessed(AfterResourceProcessedEvent event) {
                    logger.debug("afterResourceProcessed() event handler called");
                    logger.debug("afterResourceProcessed() - event.getResource(): " + event.getResource().toString());
                    logger.debug("afterResourceProcessed() - event.getResourceType(): [ " + event.getResourceType().getName() + ", " + event.getResourceType().getDescription() + " ]");
                    logger.debug("afterResourceProcessed() - event.getStatus().name(): " + event.getStatus().name());
                    logger.debug("afterResourceProcessed() - event.getStatus(): " + event.getStatus());
                    logger.debug("afterResourceProcessed() - event.getSource().getClass().getCanonicalName(): " + event.getSource().getClass().getCanonicalName());
                public void knowledgeBaseUpdated(KnowledgeBaseUpdatedEvent event) {
                    logger.debug("knowledgeBaseUpdated() event handler called");
                    logger.debug("knowledgeBaseUpdated(): Logging kbase process names after update...");
                    for(org.drools.definition.process.Process process:event.getKnowledgeBase().getProcesses())
                        logger.debug("knowledgeBaseUpdated(): process name == " + process.getName());
                public void resourceCompilationFailed(ResourceCompilationFailedEvent event) {
                    logger.debug("resourceCompilationFailed() event handler called");


1.) Is it possible to configure the knowledge agent in such a way that I could receive specific "PROCESS_ADDED / PROCESS_UPDATED" events when a process is added/updated in a package?
    (currently I only see the new process logged in the code above inside of the knowledgeBaseUpdated() handler, but I am not receiving a specific event as to *what caused* the knowledge base to be updated

2.) I have seen a few references warning that the change notifier and change scanner services must be stopped or a memory leak can occur, but where would you stop them? The
    knowledge base needs to "live on" and be updated as new knowledge sessions are created from it. So the only place I would be stopping these services and disposing of the
    knowledge agent, would be when the server is rebooted. Is this incorrect? If so, *when* would be an appropriate time to stop these services?

3.) Is it possible to detect newly created packages using KnowledgeAgentEventListener? If not, is there some other listener that can be implemented to detect new packages?

Just as some additional info, the change set resource is created with change set xml that specifies resource type = PKG for all of the packages that I want to be monitored for adds/updates (inside of 'add' xml tags), and then:

private org.drools.io.Resource createChangeSetResouceUsing(String changeSetXmlAsString) {
org.drools.io.Resource changeSetResource = new ByteArrayResource(changeSetXmlAsString.getBytes());
((InternalResource) changeSetResource).setResourceType(ResourceType.CHANGE_SET);
return changeSetResource;

Thanks in advance for any insights.

Reply to this message by going to Community

Start a new discussion in jBPM at Community

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20130529/510fa273/attachment-0001.html 

More information about the jboss-user mailing list