我可以借助Script-Task和kcontext-instance从进程中成功检索全局变量(例如NodeInstance,ProcessInstance等);但是如果我需要在WorkItem中检索那些全局变量呢?我当然可以将Script-Task放在Process中的WorkItem之前,然后将一个全局变量分配给一个Variable Definitions,以便在WorkItem中使用它与getParameter() - 方法;
但我们明白,这是双重工作,不是吗? 那么如何在没有额外脚本任务的情况下在WorkItem中获取全局变量?我首先想到,我可以在WorkItem中使用相同的会话名称,例如我们通过在KieWorkBench中注册WorkItem来定义,这就是我在WI中创建ProcessContext的原因,如下所示:
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);
}
使用executeWorkItem()中的代码片段,而是使用Script-Task中的kcontext,我可以成功检索Lane的名称并将其分配给变量,但是当我使用此WorkItem执行进程时,我得到一个错误 - WorkItem - null不是通过部署,而是通过启动进程;当然,我也在KieWorkBench中注册了与WorkItem中相同的ksession - name(即&#34; ksession-process&#34;),但显然我不能像我想的那样使用这种方式。
所以我非常感谢你的帮助!它并不是真正的麻烦,而是制作代码。简单地进行BP处理并且没有任何冗余变量,这是值得的。
jBPM 6.4,wildfly 8.2
更新 正如用户livthomas所建议的那样,我尝试将我的类扩展到AbstractWorkiItemHandler而不是WorkItemHandler接口,如下所示:
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);
}
...
然后将其作为新的my_package.WI(ksession)注册到Project的descriptor.xml中; 不幸的是我得到了RuntimeException:不能实例WorkItem ...
UPDATE2:堆栈跟踪
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
答案 0 :(得分:1)
您需要扩展AbstractWorkItemHandler或以类似的方式实现您的工作项处理程序。然后在注册时将new CarModel('simba')
实例作为其构造函数的参数传递。一旦引用了KIE会话,就可以获得调用其ksession
方法的全局变量。