Proper way to set or update JBPM6 Process Variables

蓝咒 提交于 2019-12-02 14:31:03

问题


I am trying to set the process variable but getting below error

java.lang.IllegalStateException: Process instance 10[SampleBusinessProcess] is disconnected.
at org.jbpm.process.instance.impl.ProcessInstanceImpl.getProcess(ProcessInstanceImpl.java:100)
at org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl.setVariable(WorkflowProcessInstanceImpl.java:343)

Here is my code -

ProcessInstance processInstance = runtimeEngine.getKieSession().getProcessInstance(processInstanceId);
((WorkflowProcessInstanceImpl) processInstance).setVariable("myvariable", "myvalue");

Please help me with the best way to set the process variable.

Thanks.


回答1:


I would recommend to use the jbpm-services for interacting with the engine, this offers an operation to set a variable: https://github.com/kiegroup/jbpm/blob/7.7.0.Final/jbpm-services/jbpm-services-api/src/main/java/org/jbpm/services/api/ProcessService.java#L223

Alternatively, if you make sure that your code is executed in the same transaction (by for example starting and committing the transaction yourself, around the code you currently have), you will be able to set the variable this way, as the process instance will only disconnect as soon as the transaction commits.




回答2:


I can't comment on the previous answer so I'm posting this answer.

The quick and dirty way I found (inspired from previous answer) is:

public void setProcessInstanceVariables(long processInstanceId, Map<String, Object> variables) {
    kieSession.execute(new SetProcessInstanceVariablesCommand(processInstanceId, variables));
}

Thanks a lot Kris Verlaenen!



来源:https://stackoverflow.com/questions/50613721/proper-way-to-set-or-update-jbpm6-process-variables

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