天天看點

Flowable 6.6.0 BPMN使用者指南 - 15 CDI內建 - 15.2 使用CDI的上下文流程執行(2-4)《Flowable 6.6.0 BPMN使用者指南》

Flowable 6.6.0 使用者指南相關文檔下載下傳

  • BPMN使用者指南 第一部分 - 中文PDF精編版
  • BPMN使用者指南 第二部分 - 中文PDF精編版
  • BPMN使用者指南 第三部分 - 中文PDF精編版
  • 應用程式指南 - 中文PDF精編版
  • 應用程式指南 - 中英對照PDF精編版
  • 應用程式指南 - Eclipse設計器中文PDF精編版
  • 表單使用者指南 - 中文PDF精編版
  • 事件系統資料庫使用者指南 - 中文PDF精編版

《Flowable 6.6.0 BPMN使用者指南》

1. 入門

2. 配置

3 The Flowable API

4 Flowable 6.6.0 BPMN使用者指南 - (4)Spring內建

5 Spring Boot

6 部署

7 BPMN 2.0簡介

8 BPMN 2.0的構造

9 表單(Forms)

10 流程執行個體遷移

11 JPA

12 曆史(History)

13 身份管理(Identity management)

14 REST API

15 CDI內建

  • 15.1 設定flowable-cdi
  • 15.2 使用CDI的上下文流程執行(Contextual Process Execution with CDI)
    • 15.2.1 實作會話與流程執行個體的關聯(Associating a Conversation with a Process Instance)
    • 15.2.2 通過聲明控制流程(Declaratively controlling the Process)
    • 15.2.3 從流程引用Bean(Referencing Beans from the Process)
    • 15.2.4 使用@BusinessProcessScoped bean
    • 15.2.5 注入流程變量(Injecting Process Variables)
    • 15.2.6 接收流程事件(Receiving Process Events)
    • 15.2.7 其它特征(Additional Features)
  • 15.3 已知限制

有關Flowable的更多文檔,參見:

《Flowable文檔大全》

15.2.2 通過聲明控制流程(Declaratively controlling the Process)

Flowable-cdi allows declaratively starting process instances and completing tasks using annotations. The @org.flowable.cdi.annotation.StartProcess annotation allows to start a process instance either by “key” or by “name”. Note that the process instance is started after the annotated method returns. Example:

Flowable-cdi允許使用注解以聲明方式啟動流程執行個體并完成任務。@org.flowable.cdi.annotation.StartProcess允許通過“key”或“name”啟動流程執行個體。請注意,流程執行個體是在被注解的方法傳回後啟動的。例子:

@StartProcess("authorizeBusinessTripRequest")
public String submitRequest(BusinessTripRequest request) {
    // do some work
    return "success";
}
           

Depending on the configuration of Flowable, the code of the annotated method and the starting of the process instance will be combined in the same transaction. The @org.flowable.cdi.annotation.CompleteTask-annotation works in the same way:

基于Flowable的配置,被注解的方法的代碼和流程執行個體的啟動将合并在同一事務中。@org.flowable.cdi.annotation.CompleteTask注解的工作原理相同:

@CompleteTask(endConversation=false)
public String authorizeBusinessTrip() {
    // do some work
    return "success";
}
           

The @CompleteTask annotation offers the possibility to end the current conversation. The default behavior is to end the conversation after the call to Flowable returns. Ending the conversation can be disabled, as shown in the example above.

@CompleteTask注解提供了結束目前會話的可能性。預設行為是在Flowable調用傳回之後結束會話。可以禁用結束會話,如上面的示例所示。

15.2.3 從流程引用Bean(Referencing Beans from the Process)

Flowable-cdi exposes CDI beans to Flowable El, using a custom resolver. This makes it possible to reference beans from the process:

Where authorizingManager could be a bean provided by a producer method:

Flowable-cdi使用自定義解析器(custom resolver)将CDI bean公開給Flowable El。這使得從流程中引用bean成為可能:

<userTask id="authorizeBusinessTrip" name="Authorize Business Trip"
            flowable:assignee="#{authorizingManager.account.username}" />
           

其中authorizingManager可以是producer(生産者)方法提供的bean:

@Inject    @ProcessVariable Object businessTripRequesterUsername;

@Produces
@Named
public Employee authorizingManager() {
    TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.account.username='"
        + businessTripRequesterUsername + "'", Employee.class);
    Employee employee = query.getSingleResult();
    return employee.getManager();
}
           

We can use the same feature to call a business method of an EJB in a service task, using the flowable:expression=“myEjb.method()”-extension. Note that this requires a @Named-annotation on the MyEjb-class.

我們可以使用相同的特性調用服務任務(service task)中的EJB業務方法(使用flowable:expression="myEjb.method()"擴充)。注意,這需要MyEjb類上有一個@Named注解。

15.2.4 使用@BusinessProcessScoped bean

Using flowable-cdi, the lifecycle of a bean can be bound to a process instance. To this extent, a custom context implementation is provided, namely the BusinessProcessContext. Instances of BusinessProcessScoped beans are stored as process variables in the current process instance. BusinessProcessScoped beans need to be PassivationCapable (for example Serializable). The following is an example of a process scoped bean:

使用flowable-cdi,bean的生命周期可以綁定到流程執行個體。在這種情況下,提供了一個定制的上下文實作,即BusinessProcessContext。BusinessProcessScoped bean的執行個體作為流程變量存儲在目前流程執行個體中。BusinessProcessScoped bean需要是PassivationCapable(例如Serializable)。以下是流程範圍bean(process scoped bean)的示例:

@Named
@BusinessProcessScoped
public class BusinessTripRequest implements Serializable {
    private static final long serialVersionUID = 1L;
    private String startDate;
    private String endDate;
    // ...
}
           

Sometimes, we want to work with process scoped beans, in the absence of an association with a process instance, for example before starting a process. If no process instance is currently active, instances of BusinessProcessScoped beans are temporarily stored in a local scope, i.e. the Conversation or the Request, depending on the context. If this scope is later associated with a business process instance, the bean instances are flushed to the process instance.

有時,我們希望在沒有與流程執行個體關聯的情況下使用流程範圍的bean(process scoped bean),例如在啟動流程之前。如果目前沒有任何流程執行個體處于活動狀态,則BusinessProcessScoped bean的執行個體将臨時存儲在局部範圍(local scope)(即會話或請求)中,具體取決于上下文。如果該範圍稍後與業務流程執行個體關聯,則bean執行個體将重新整理到流程執行個體。

繼續閱讀