如何为BPMN2 Exclusive Gateway设置条件

时间:2014-11-20 12:32:27

标签: java spring bpmn camunda

我在我的春季项目中第一次使用Camunda BPMN2并试图了解一些事情......

在我的applicationContext中,我有以下块来设置Camunda:

    <!-- Setup BPMN Process Engine -->
    <bean id="processEngineConfiguration" class="org.camunda.bpm.engine.spring.SpringProcessEngineConfiguration">
        <property name="processEngineName" value="engine" />
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true" />
        <property name="jobExecutorActivate" value="false" />
        <property name="deploymentResources" value="classpath*:*.bpmn" />
    </bean>
    <bean id="processEngine" class="org.camunda.bpm.engine.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>
    <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
    <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
    <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
    <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
    <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
    <context:annotation-config />

我已经设置了两项服务:

@Component(value="service1")
public class Service1 implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) throws Exception {     
        System.out.println(">>>>>>>>>>>>>>>>>>>");
        System.out.println("SERVICE1");     
        System.out.println(">>>>>>>>>>>>>>>>>>>");      
    }
}

@Component(value="service2")
public class Service2 implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) throws Exception {
        System.out.println(">>>>>>>>>>>>>>>>>>>");
        System.out.println("SERVICE2");     
        System.out.println(">>>>>>>>>>>>>>>>>>>");
    }
}

在方案1中,我有一个并行网关,它同时调用Service1和Service2(我在eclipse中使用BPMN2编辑器构建了这些图表):

scenario 1

scenario 1 - service 1

scenario 1 - service 2

运行这行代码:

runtimeService.startProcessInstanceByKey("ConnectorSwitch");

打印

>>>>>>>>>>>>>>>>>>>
SERVICE1
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>
SERVICE2
>>>>>>>>>>>>>>>>>>>

正如所料。

现在我正试图建立一个独家网关:

enter image description here

运行它会给我以下异常:

SEVERE: Error while closing command context                                                                                                                                                                                                 
org.camunda.bpm.engine.ProcessEngineException: Exclusive Gateway 'ExclusiveGateway_3' has outgoing sequence flow 'SequenceFlow_39' without condition which is not the default flow. | ..../ConnectorSwitch.bpmn | line 0 | column 0                                                                                                                                                                                                              
Exclusive Gateway 'ExclusiveGateway_3' has outgoing sequence flow 'SequenceFlow_40' without condition which is not the default flow. | ..../ConnectorSwitch.bpmn | line 0 | column 0                 

        at org.camunda.bpm.engine.impl.util.xml.Parse.throwExceptionForErrors(Parse.java:183)
        at org.camunda.bpm.engine.impl.bpmn.parser.BpmnParse.execute(BpmnParse.java:177)     
        at org.camunda.bpm.engine.impl.bpmn.deployer.BpmnDeployer.deploy(BpmnDeployer.java:106)
        at org.camunda.bpm.engine.impl.persistence.deploy.DeploymentCache.deploy(DeploymentCache.java:50)
        at org.camunda.bpm.engine.impl.persistence.entity.DeploymentManager.insertDeployment(DeploymentManager.java:42)
        at org.camunda.bpm.engine.impl.cmd.DeployCmd.execute(DeployCmd.java:81)                                        
        at org.camunda.bpm.engine.impl.cmd.DeployCmd.execute(DeployCmd.java:50)                                        
        at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24)            
        at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:90)
        at org.camunda.bpm.engine.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:42)
        at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130) 
    ......

异常非常清楚,我在专属网关上缺少一个条件。 所以我的问题是,我如何为独占网关分配一个条件,如何调用某个类中的方法并评估true / false,如果我想调用其他不是service1 / service2的JavaDelegate的东西(换句话说,MyClass.doSomethingWithParams(someparam)),我该怎么做呢?

XML中的答案也很好,我更愿意学习如何在XML中使用BPMN2,而不是依赖于BPMN2视觉效果。

1 个答案:

答案 0 :(得分:12)

Camunda Modeler 中,单击来自专用网关的其中一个序列流。然后选择Properties窗格和General子窗格。您会看到属性Condition。此属性可以使用JUEL表达式填充,就像您用于委托的表达式一样。你可以在那里使用你的Spring bean,例如在他们身上调用一个方法。因此,如果myClass是一个Spring bean名称,那么只需编写${myClass.doSomethingWithParams(someparam)}。或者,您可以访问已附加到流程实例的流程变量。 someparam可能是一个变量,例如 - &GT;只需确保所有传出序列流都附加了这样的条件。然后您的流程将再次运行。也许首先使用${true}${false}之类的简单条件开始。如果这样可行,继续前进并做一些更复杂的事情。玩得开心! : - )