How to set global process variables in Camunda-BPM?

若如初见. 提交于 2019-12-12 20:03:22

问题


I have a simple bpmn process in which i am using 2 service task,I am executing my process by using processEngine.getRuntimeService().startProcessInstanceByKey("Process_1", variables); where my variables is as follows:

Map variables = new HashMap();
variables.put("a", 2);
variables.put("b", 5);

Service task 1 implements an Addition java class and service task 2 implements a Multiplication class.

Now I want to have 3 variables (constants) c = 5, d = 10, e = 2 so that I can use c for service task 1 such that in Addition I can use this variable, similarly I want to use d in my Multiplication class, and e should be global so that I can use it in both classes.

Can anyone guide me on this?


回答1:


As a quick fix you could include a Setup-Service Task as the first Task of the process which prefills your process-variables. Depending on how you start a process you could either: Set the Variables via the java-object-api

https://docs.camunda.org/manual/7.5/user-guide/process-engine/variables/#java-object-api

or you if you use a REST call you can provide these fixed values within the request body:

https://docs.camunda.org/manual/7.5/reference/rest/process-definition/post-start-process-instance/

Another simple solution would be a class with static values or a enum holding the needed values.

--edit--

if you want to use the inputOutput extension add something like this to your bpmn file:

<bpmn:process id="Process_1" isExecutable="false">
  <bpmn:extensionElements>
    <camunda:inputOutput>
      <camunda:inputParameter name="c">5</camunda:inputParameter>
      <camunda:inputParameter name="d">10</camunda:inputParameter>
      <camunda:inputParameter name="e">2</camunda:inputParameter>
    </camunda:inputOutput>
  </bpmn:extensionElements>

this can't be done in the diagram view of the camunda modeler, just switch to the XML representation of the process and add the extensionElement.




回答2:


The documentation shows two different ways to store the value:

Java object api Typed value api

I think using Java object api requires the java object to implement serializable interface? The following code would break, if Order object does not implement Serializable interface

com.example.Order order = new com.example.Order();
runtimeService.setVariable(execution.getId(), "order", order);

com.example.Order retrievedOrder = (com.example.Order) runtimeService.getVariable(execution.getId(), "order");

==

I would use the following format for java object

ObjectValue customerDataValue = Variables.objectValue(customerData)
  .serializationDataFormat(Variables.SerializationDataFormats.JAVA)
  .create();

execution.setVariable("someVariable", customerDataValue);

customerdata refers to any java object. However if there member variables contains some other references, those references needs to serializable as well. To avoid this you will have declare those references as transient

Further more, use setVariableLocal method if you dont want the data to be persisted in DB



来源:https://stackoverflow.com/questions/38450543/process-variables-in-camunda-bpm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!