在Activiti中,如何检查是否设置了变量?

时间:2016-05-31 13:37:47

标签: java workflow activiti

例如,我有一个可以立即启动或延迟(startTime变量)的工作流程。

在我startEvent exclusiveGateway之后我检查流程是否应该继续或等到startTime

<exclusiveGateway id="startGateway" default="startSequenceFlow3"/>
<sequenceFlow id="startSequenceFlow1" sourceRef="startGateway" targetRef="startTimer">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${startTime != null}]]></conditionExpression>
</sequenceFlow>

启动传递变量startTime的工作流程正常,但传递no startTime会引发异常:

  

无法解析标识符'startTime'

检查startTime是否设置的最佳方式是什么,因为startTime != null无效?我宁愿不传递startTime(不是startTime=null)。

我使用的代码包括变量:

variables.put("startTime", startTime);
ProcessInstance instance = runtimeService.startProcessInstanceByKey(processKey, variables);

或没有:

ProcessInstance instance = runtimeService.startProcessInstanceByKey(processKey, variables);

3 个答案:

答案 0 :(得分:9)

使用以下表达式:

${execution.getVariable('startTime') != null}

答案 1 :(得分:3)

在这两种情况下都必须设置 startTime 变量;

variables.put("startTime", startTime);
ProcessInstance instance = runtimeService.startProcessInstanceByKey(processKey, variables);

variables.put("startTime", null);
ProcessInstance instance = runtimeService.startProcessInstanceByKey(processKey, variables);

然后检查网关中的变量

<exclusiveGateway id="startGateway" default="waitSequenceFlow"/>
<sequenceFlow id="startSequenceFlow" sourceRef="startGateway" targetRef="firstTask">
    <conditionExpression xsi:type="tFormalExpression"><![CDATA[${empty startTime}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="waitSequenceFlow" sourceRef="startGateway" targetRef="startTimer"/>

您可以使用http://www.activiti.org/userguide/#bpmnTimerStartEvent

答案 2 :(得分:0)

我正在尝试设置如下变量,但这不起作用

public void execute(DelegateExecution arg0) 抛出异常 {

String keyword = (String) arg0.getVariable("Service2Status");

if (keyword.equals("STARTED")) {
    System.out.println("service task2 started");
    System.out.println("service task2 complete");
    keyword = "DONE";
    arg0.setVariable("Service2Status", keyword);
    System.out.println("888888" + arg0.getVariable("Service2Status"));
}