terça-feira, 16 de julho de 2013

Using the Knowledge Agent with a ChangeSet.xml file with JBoss BRMS

This is a really simple example of how to use the Knowledge Agent api (aka kagent) to retrieve a compiled package from BRMS (Guvnor) repository.
First of all you need to create your process, in my sample it just has a script task which prints "Hello" in the console by using System.out.println("Hello").
After it, you can built the package:


And then, download the ChangeSet.xml file descriptor containing the repository access info to authenticate and retrieve the package built. In this example, I'm getting the LATEST version of it, as following:


Copy the ChangeSet.xml to your classpath, usually in the src/main/resources of your JBoss Developer Studio project.

Now, you just have to use the KnowledgeAgent to read the ChangeSet.xml, get your KnowledgeBase instance and start the process. See a example below:

public static final void main(String[] args) throws Exception {
    // load up the knowledge base
    KnowledgeBase kbase = readKnowledgeBase();
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    // start a new process instance
    ksession.startProcess("brm.process");
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
    final KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("kagent");    
    ResourceFactory.getResourceChangeNotifierService().start();
    ResourceFactory.getResourceChangeScannerService().start();      
    kagent.applyChangeSet(ResourceFactory.newClassPathResource("ChangeSet.xml"));
    KnowledgeBase kbase = kagent.getKnowledgeBase();
    return kbase;
}

I'm going to upload it to a git as soon as I create one, but in the meanwhile download the MyProject.zip from this post.

Some things you should remember
Add the correct authentication to the ChangeSet.xml. The ChangeSet.xml created by BRMS doesn't come with the  basicAuthentication, username and password attributes. Add them to avoid issues.
  
basicAuthentication="enabled" username="admin" password="admin"
Something that is really common to happen is to thrown a NPE in the client:

Exception in thread "main" java.lang.NullPointerException
 at java.util.AbstractCollection.addAll(AbstractCollection.java:333)
 at org.drools.agent.impl.KnowledgeAgentImpl.addResourcesToKnowledgeBase(KnowledgeAgentImpl.java:1075)
 at org.drools.agent.impl.KnowledgeAgentImpl.rebuildResources(KnowledgeAgentImpl.java:822)
 at org.drools.agent.impl.KnowledgeAgentImpl.buildKnowledgeBase(KnowledgeAgentImpl.java:671)
 at org.drools.agent.impl.KnowledgeAgentImpl.applyChangeSet(KnowledgeAgentImpl.java:201)
 at org.drools.agent.impl.KnowledgeAgentImpl.applyChangeSet(KnowledgeAgentImpl.java:180)
 at com.sample.ProcessMain.readKnowledgeBase(ProcessMain.java:26)
 at com.sample.ProcessMain.main(ProcessMain.java:16)

Followed by 404 in the server:

22:27:41,591 INFO  [TransformerServlet] check connection response code: 404
22:27:46,242 WARN  [ValueConstraint] validation of reference constraint is not yet implemented
22:27:46,500 INFO  [TransformerServlet] create connection response code: 200
22:28:29,448 INFO  [DocNumberCache] size=10/1024, #accesses=1001, #hits=1001, #misses=0, cacheRatio=100%
22:36:59,644 WARN  [RepositoryServlet] Unable to authenticate for rest api: null
If you've already added the correct authentication but you are still seeing the NPE, but without the 404 code, ensure you have the same BRMS jar files in both client and server sides.

Refer to the Knowledge Agent  official documentation for further details.

Nenhum comentário: