javajbpmkiekie-workbench

How to get global variables from jbpm-process within WorkItem


I can successfully retrieve global variables (such as NodeInstance, ProcessInstance and so on) from within the process with help of Script-Task and kcontext-instance; But what if I need retrieve those global variables within WorkItem? I could of course put Script-Task before my WorkItem within Process and then assign a global variable to one of Variable Definitions, in order to use it within WorkItem with getParameter()-method;

But we do understand, that it is double made work, doesn't we? So how could I get global variables within WorkItem without additional Script-Task? I thought firstly, that I could use the same name of the session within WorkItem, such we define by registering WorkItem in KieWorkBench, that's why I created a ProcessContext within my WI as follows:

KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-process");
ProcessContext context = new ProcessContext(kSession);
Map<String, Object> params = new HashMap<>();

public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {

    params.put("lane", context.getNodeInstance().getNode().getMetaData().get("Lane").toString());
    manager.completeWorkItem(workItem.getId(), params);

}

With this code snippet within executeWorkItem(), but rather with kcontext in Script-Task I can retrieve successfuly the name of the Lane and assign it to variable, but when I execute the process with this WorkItem I get an Error - WorkItem -- null not by deploying, rather by starting the process; Of course I also registry the same ksession - name as within WorkItem (namely "ksession-process") in KieWorkBench, but apparently I cannot use the context this way, as I thought.

So I really appreciate your help! It doesn't really bother, but to make a code resp. BProcess a little bit concisely and without any redundant variables, it pays to ask.

jBPM 6.4, wildfly 8.2

UPDATE: as user livthomas has suggested, instead of WorkItemHandler interface I've tried to extend my class to AbstractWorkiItemHandler as follows:

public class WI extends AbstractWorkItemHandler {

public WI(StatefulKnowledgeSession ksession) {
    super(ksession);
}

KieSession kSession = getSession(); 
ProcessContext context = new ProcessContext(kSession);
Map<String, Object> params = new HashMap<>();

public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    params.put("lane", context.getNodeInstance().getNode().getMetaData().get("Lane"));
    manager.completeWorkItem(workItem.getId(), params);
}
...

And then register it in Project's descriptor.xml as new my_package.WI(ksession); Unfortunately I get the RuntimeException: cannot instance WorkItem...

UPDATE2: STACK TRACE

2016-09-13 11:49:59,355 ERROR [org.jbpm.console.ng.bd.backend.server.DeploymentManagerEntryPointImpl] (default task-24) Deployment of unit testing:testing-work-item-handler:1.0 failed: java.lang.RuntimeException: java.lang.RuntimeException: Cannot instance WorkItemHandler bpmstest.test.WI: org.jbpm.console.ng.bd.exception.DeploymentException: java.lang.RuntimeException: java.lang.RuntimeException: Cannot instance WorkItemHandler bpmstest.test.WI
    at org.jbpm.console.ng.bd.backend.server.DeploymentManagerEntryPointImpl.deploy(DeploymentManagerEntryPointImpl.java:203) [jbpm-console-ng-business-domain-backend-6.4.0.Final.jar:6.4.0.Final]
    at org.jbpm.console.ng.bd.backend.server.DeploymentManagerEntryPointImpl.deploy(DeploymentManagerEntryPointImpl.java:158) [jbpm-console-ng-business-domain-backend-6.4.0.Final.jar:6.4.0.Final]
    at org.jbpm.console.ng.bd.backend.server.DeploymentManagerEntryPointImpl.process(DeploymentManagerEntryPointImpl.java:517) [jbpm-console-ng-business-domain-backend-6.4.0.Final.jar:6.4.0.Final]
    at org.jbpm.console.ng.bd.backend.server.DeploymentManagerEntryPointImpl$Proxy$_$$_WeldClientProxy.process(Unknown Source) [jbpm-console-ng-business-domain-backend-6.4.0.Final.jar:6.4.0.Final]
    at org.kie.workbench.common.services.backend.builder.BuildServiceImpl.doBuildAndDeploy(BuildServiceImpl.java:178) [kie-wb-common-services-backend-6.4.0.Final.jar:6.4.0.Final]
    at org.kie.workbench.common.services.backend.builder.BuildServiceImpl.buildAndDeploy(BuildServiceImpl.java:134) [kie-wb-common-services-backend-6.4.0.Final.jar:6.4.0.Final]
    at org.kie.workbench.common.services.backend.builder.BuildServiceImpl$Proxy$_$$_WeldClientProxy.buildAndDeploy(Unknown Source) [kie-wb-common-services-backend-6.4.0.Final.jar:6.4.0.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_72]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_72]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_72]
    at java.lang.reflect.Method.invoke(Method.java:498) [rt.jar:1.8.0_72]
    at org.jboss.errai.bus.server.io.AbstractRPCMethodCallback.invokeMethodFromMessage(AbstractRPCMethodCallback.java:48) [errai-bus-3.2.3.Final.jar:3.2.3.Final]
    at org.jboss.errai.bus.server.io.ValueReplyRPCEndpointCallback.callback(ValueReplyRPCEndpointCallback.java:22) [errai-bus-3.2.3.Final.jar:3.2.3.Final]
    at org.jboss.errai.bus.server.io.RemoteServiceCallback.callback(RemoteServiceCallback.java:54) [errai-bus-3.2.3.Final.jar:3.2.3.Final]
    at org.jboss.errai.cdi.server.CDIExtensionPoints$2.callback(CDIExtensionPoints.java:410) [errai-weld-integration-3.2.3.Final.jar:3.2.3.Final]
    at org.jboss.errai.bus.server.DeliveryPlan.deliver(DeliveryPlan.java:47) [errai-bus-3.2.3.Final.jar:3.2.3.Final]
    at org.jboss.errai.bus.server.ServerMessageBusImpl.sendGlobal(ServerMessageBusImpl.java:296) [errai-bus-3.2.3.Final.jar:3.2.3.Final]
    at org.jboss.errai.bus.server.SimpleDispatcher.dispatchGlobal(SimpleDispatcher.java:46) [errai-bus-3.2.3.Final.jar:3.2.3.Final]
    at org.jboss.errai.bus.server.service.ErraiServiceImpl.store(ErraiServiceImpl.java:97) [errai-bus-3.2.3.Final.jar:3.2.3.Final]
    at org.jboss.errai.bus.server.service.ErraiServiceImpl.store(ErraiServiceImpl.java:114) [errai-bus-3.2.3.Final.jar:3.2.3.Final]
    at org.jboss.errai.bus.server.servlet.DefaultBlockingServlet.doPost(DefaultBlockingServlet.java:142) [errai-bus-3.2.3.Final.jar:3.2.3.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at org.uberfire.ext.security.server.SecureHeadersFilter.doFilter(SecureHeadersFilter.java:69) [uberfire-servlet-security-0.8.0.Final.jar:0.8.0.Final]
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at org.uberfire.ext.security.server.SecurityIntegrationFilter.doFilter(SecurityIntegrationFilter.java:61) [uberfire-servlet-security-0.8.0.Final.jar:0.8.0.Final]
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.DisableCacheHandler.handleRequest(DisableCacheHandler.java:33) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:56) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:261) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:248) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:77) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:167) [undertow-servlet-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:761) [undertow-core-1.1.8.Final.jar:1.1.8.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_72]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_72]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_72]
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Cannot instance WorkItemHandler bpmstest.test.WI
    at org.jbpm.kie.services.impl.KModuleDeploymentService.deploy(KModuleDeploymentService.java:201) [jbpm-kie-services-6.4.0.Final.jar:6.4.0.Final]
    at org.jbpm.services.cdi.impl.DeploymentServiceCDIImpl$Proxy$_$$_WeldClientProxy.deploy(Unknown Source) [jbpm-services-cdi-6.4.0.Final.jar:6.4.0.Final]
    at org.jbpm.console.ng.bd.backend.server.DeploymentManagerEntryPointImpl.deploy(DeploymentManagerEntryPointImpl.java:194) [jbpm-console-ng-business-domain-backend-6.4.0.Final.jar:6.4.0.Final]
    ... 59 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Cannot instance WorkItemHandler bpmstest.test.WI
    at org.jbpm.kie.services.impl.AbstractDeploymentService.commonDeploy(AbstractDeploymentService.java:161) [jbpm-kie-services-6.4.0.Final.jar:6.4.0.Final]
    at org.jbpm.kie.services.impl.KModuleDeploymentService.deploy(KModuleDeploymentService.java:196) [jbpm-kie-services-6.4.0.Final.jar:6.4.0.Final]
    ... 61 more
Caused by: java.lang.RuntimeException: Cannot instance WorkItemHandler bpmstest.test.WI

Solution

  • You need to either extend AbstractWorkItemHandler or implement your work item handler in a similar way. Then just pass ksession instance as a parameter of its constructor when you register it. Once you have a reference to KIE session, you can get global variables calling its getGlobals() method.