如何使用camel从jbpm返回流程变量?

时间:2016-07-19 14:11:37

标签: apache-camel jbpm

我通过传递变量map在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);

           }

1 个答案:

答案 0 :(得分:0)

这是一个例子(代码在scala中,但我相信你会得到这个想法)

它使用from time import sleep import pylab as pl import matplotlib.pyplot as plt temperature = [] x = list() y = list() y1 = list() dT_tol = .5 plt.ion() fig = plt.figure(1) ax1 = fig.add_subplot(211) temp_plot = plt.scatter([],[]) plt.ylabel('Temperature (C)') ax2 = fig.add_subplot(212) delta_temp_plot = plt.scatter([],[],zorder = 2) plt.axhspan(-dT_tol, dT_tol, color='#87CEFA', alpha=1, zorder = 1) plt.ylabel('dT') plt.xlabel('Time (s)') plt.draw() # Read loop for i in range(60): tempC = i #for simplicity, I can get real T later temperature.append(tempC) dT = temperature[i]-temperature[i-1] x.append(i) y.append(temperature[i]) y1.append(dT) if len(x)>10: del x[0] del y[0] del y1[0] xmin = min(x) xmax = max(x) if -dT_tol<dT<dT_tol: print "Temperature:","%.3f"% temperature[i]," " "dT:", "%.3f"% dT, " " "Steady State" sleep(1) else: print "Temperature:","%.3f"% temperature[i]," " "dT:", "%.3f"% dT sleep(1) #Replace old points with new points temp_plot.set_offsets(zip(x,y)) delta_temp_plot.set_offsets(zip(x,y1)) if len(x)<=10: ax1.axis([0,10,0,80]) ax2.axis([0,10,0,80]) else: ax1.axis([xmin,xmax,0,80]) ax2.axis([xmin,xmax,-4,4]) fig.canvas.draw() 启动流程,使用自定义代码获取流程变量(纯 kie )。

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

我正在使用 jbpm-workbench的招聘流程 *

name

使用的测试用例

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}")
    }

}

*)如果要快速测试,请运行以下docker容器

@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"))

}