简体   繁体   中英

Accessing KieSession from jbpm6 WorkItemHandler

I'm using jbpm-console (6.0.0.Final) with custom work item handlers. I've embedded a custom work item handlers JAR in my jbpm-console WAR as described in post #7 here:

https://community.jboss.org/thread/221748

This is all fine so far, as I can successful start a process definition in jbpm-console, and it kicks off my custom WorkItemHandler code.

However, in my WorkItemHandler, I want to set some variables on the ProcessInstance. When I try something like this:

public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    KieServices ks = KieServices.Factory.get();
    KieContainer kContainer = ks.getKieClasspathContainer();

    // BLOWS UP HERE
    //
    KieSession session = kContainer.newKieSession();

    WorkflowProcessInstance processInstance = 
      session.getProcessInstance(workItem.getProcessInstanceId());

    // SET VARIABLE
    processInstance.setVariable("foo", "bar");
}

It "Cannot find a default KieSession":

11:21:03,177 ERROR Exception: java.lang.RuntimeException: Cannot find a default KieSession
at org.drools.compiler.kie.builder.impl.KieContainerImpl.findKieSessionModel(KieContainerImpl.java:302) [drools-compiler-6.0.0.Final.jar:6.0.0.Final]
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:295) [drools-compiler-6.0.0.Final.jar:6.0.0.Final]
at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:278) [drools-compiler-6.0.0.Final.jar:6.0.0.Final]

What am I missing here? Do I have to set something additional up? I'm just using the out-of-the-box (with the exception of the custom work item handler embedded jar) "demo" install from here:

http://sourceforge.net/projects/jbpm/files/jBPM%206/jbpm-6.0.0.Final

Thanks!

Do you have a kmodule.xml in your resources folder? If not maybe that's the problem. You can have a pretty simple xml file like:

<kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://jboss.org/kie/6.0.0/kmodule">
</kmodule>

Pass your kieSession into your workItemHandler as a constructor parameter. Then your kieSession is available to your workItemHandler.

You can register your workItemHandler when you need it with the appropriate kieSession, you do not have to do it only once.

Or you pass the rulesManager at the start and generate a kieSession on demand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM