0 Replies Latest reply on May 29, 2013 11:47 AM by cold gin

    Using KnowledgeAgentEventListener to detect new processes (v5.4.0)

    cold gin Newbie

      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 beforeResourceProcessed() and afterResourceProcesssed() 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();
      agentConfig.setProperty("drools.agent.newInstance", "false");
      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.applyChangeSet(changeSetResource);
      
      ResourceFactory.getResourceChangeNotifierService().start();
      ResourceFactory.getResourceChangeScannerService().start();
      
      
                  knowledgeAgent.addEventListener(new KnowledgeAgentEventListener() {
      
                      @Override
                      public void beforeChangeSetApplied(BeforeChangeSetAppliedEvent event) {
                          logger.debug("beforeChangeSetApplied() event handler called");                    
                      }
      
                      @Override
                      public void afterChangeSetApplied(AfterChangeSetAppliedEvent eve nt) {
                          logger.debug("afterChangeSetApplied() event handler called");                    
                      }
      
                      @Override
                      public void beforeChangeSetProcessed(BeforeChangeSetProcessedEvent event) {
                          logger.debug("beforeChangeSetProcessed() event handler called");    
                      }
      
                      @Override
                      public void afterChangeSetProcessed(AfterChangeSetProcessedEvent event) {
                          logger.debug("afterChangeSetProcessed() event handler called");
                      }
      
                      @Override
                      public void beforeResourceProcessed(BeforeResourceProcessedEvent event) {
                          logger.debug("=======================================================");
                          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());
                          logger.debug("=======================================================");
                      }
      
                      @Override
                      public void afterResourceProcessed(AfterResourceProcessedEvent event) {
                          logger.debug("=======================================================");
                          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());
                          logger.debug("=======================================================");
                      }
      
                      @Override
                      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());
                          }
                      }
      
                      @Override
                      public void resourceCompilationFailed(ResourceCompilationFailedEvent event) {
                          logger.debug("resourceCompilationFailed() event handler called");
      
                      }
      
                  });
      

       

      Questions:

       

      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 I 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 restarted. 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.