如何使用骆驼从 jbpm 取回流程变量?

How to get the process variable back from jbpm using camel?

我通过传递变量映射在 jbpm 中通过 camel 启动一个进程。 在 jbpm 中,我正在更改该变量的值(此处 "name"),但我无法将变量恢复为骆驼。以下是代码:

        final Map map = new HashMap();
        JBPMConfiguration bPMConfiguration = new JBPMConfiguration();
        bPMConfiguration.setUserName("admin");
        bPMConfiguration.setPassword("***");
         bPMConfiguration.setProcessId("HelloWorld.helloworldBusinessProcess");
        bPMConfiguration.setDeploymentId("SAN:HelloWorld:1.0");

        bPMConfiguration.setConnectionURL(new URL("http://127.0.0.1:8080/kie-wb62"));
        JBPMEndpoint bPMEndpoint = new JBPMEndpoint("jbpm:http", new JBPMComponent(), bPMConfiguration);
         JBPMProducer bPMProducer=(JBPMProducer) bPMEndpoint.createProducer();
        if (bPMProducer instanceof JBPMProducer) {
         Exchange exchange = ((JBPMProducer) bPMProducer).createExchange();
            map.put("name", "SAntanu");
            bPMConfiguration.setParameters(map);
            exchange.setPattern(ExchangePattern.OutIn);
            exchange.getOut().setHeader("CamelJBPMParameters",map);
            bPMProducer.start();
            bPMProducer.process(exchange);

           }

这是一个示例(代码在 scala 中,但我相信你会明白的)

它使用 camel-jbpm 启动流程,并为自定义代码获取流程变量(纯 kie)。

在此演示中,我使用标准 camel-jbpm 组件来启动该过程。要获取值,您需要第二次休息请求。在撰写本文时(2016 年 7 月),camel-jbpm 组件尚不支持此功能。我正在使用自定义处理器发送 FindVariableInstancesCommandname 流程变量的请求。 我还列出了 import 语句,因为它们可能不像你想象的那么明显(注意这都是 kie)

我正在使用来自 jbpm-workbench *

hiring 流程
package demo.http

import java.net.URL

import org.apache.camel.Exchange
import org.apache.camel.component.jbpm.JBPMConstants
import org.apache.camel.scala.dsl.builder.RouteBuilder
import org.kie.api.runtime.manager.RuntimeEngine
import org.kie.api.runtime.process.ProcessInstance
import org.kie.remote.client.api.RemoteRuntimeEngineFactory
import org.kie.remote.jaxb.gen.FindVariableInstancesCommand
import org.kie.services.client.serialization.jaxb.impl.audit.JaxbVariableInstanceLog

import scala.collection.JavaConverters._

/**
  * todo
  */
class JBpmnRoute extends RouteBuilder {

    "direct:jbpmRoute" ==> {
      // set the process id
      setHeader(JBPMConstants.PROCESS_ID, constant("hiring"))

      // in this example: copy map from in.body to header[JBPMConstants.PARAMETERS]
      process((e: Exchange) => {
        e.getIn().setHeader(JBPMConstants.PARAMETERS, e.in[Map[String, AnyRef]].asJava)
      })

      // Start the process
      to("jbpm:http://localhost:8080/jbpm-console?userName=admin&password=admin"
        + "&deploymentId=org.jbpm:HR:1.0&operation=startProcess")

      // obtain process variable (in this example "name") 
      process((e: Exchange) => {
        val rte: RuntimeEngine = RemoteRuntimeEngineFactory.newRestBuilder()
          .addUrl(new URL("http://localhost:8080/jbpm-console/")).addUserName("admin").addPassword("admin")
          .addDeploymentId("org.jbpm:HR:1.0").build()

        val cmd: FindVariableInstancesCommand = new FindVariableInstancesCommand()
        cmd.setProcessInstanceId(e.in[ProcessInstance].getId)
        cmd.setVariableId("name")
        val result = rte.getKieSession.execute(cmd).asInstanceOf[java.util.List[AnyRef]].asScala
        e.in  = result.head.asInstanceOf[JaxbVariableInstanceLog].getValue
      })
      log("value of name ${body}")
    }

}

使用的测试用例

@Test
def exampleTest(): Unit = {
  val param: Map[String, AnyRef] = Map("name" -> "Mike Wheeler")
  val response = template.requestBody("direct:jbpmRoute",
    param, classOf[String])
  org.junit.Assert.assertThat(response, Is.is("Mike Wheeler"))

}

*)如果想快速测试一下,运行下面的docker容器

docker run -Pd -p 8080:8080 -p 8001:8001 --name jbpm-workbench docker.io/jboss/jbpm-workbench-showcase