第 66 章 进程引擎的核心引擎 API


流程引擎执行业务流程。要定义流程,您可以创建 业务资产,包括流程定义和自定义任务。

您可以使用 Core Engine API 来加载、执行和管理进程引擎中的进程。

有几种级别的控制:

  • 在最低级别,您可以直接创建一个 KIE 基础KIE 会话。KIE 基础代表业务流程中的所有资产。KIE 会话是进程引擎中运行业务流程实例的实体。此级别提供精细的控制,但需要明确声明和配置进程实例、任务处理程序、事件处理程序以及您的代码中的其他进程引擎实体。
  • 您可以使用 RuntimeManager 类来管理会话和进程。此类使用可配置策略为所需的进程实例提供会话。它自动配置 KIE 会话和任务服务间的交互。它推断了不再需要的进程引擎实体,确保资源的优化使用。您可以使用流畅的 API 将 RuntimeManager 与必要的业务资产实例化,并配置其环境。
  • 您可以使用 Services API 来管理进程的执行。例如,部署服务将业务资产部署到引擎,形成 部署单元。进程服务从这个部署单元运行进程。

    如果要在应用程序中嵌入进程引擎,Services API 是最方便的选项,因为它隐藏了配置和管理引擎的内部详情。

  • 最后,您可以部署一个 KIE 服务器,从 KJAR 文件并运行进程中加载业务资产。KIE 服务器为加载和管理进程提供了 REST API。您还可以使用 Business Central 管理 KIE 服务器。

    如果您使用 KIE 服务器,则不需要使用内核引擎 API。有关在 KIE 服务器上部署和管理流程的详情,请参考打包和部署 Red Hat Process Automation Manager 项目

有关所有公共流程引擎 API 调用的完整参考信息,请参阅 Java 文档。其他 API 类也存在于代码中,但它们是可以在以后的版本中更改的内部 API。在您开发和维护的应用程序中使用公共 API。

66.1. KIE 基础和 KIE 会话

KIE 基础 包含对进程相关的所有进程定义和其他资产的引用。该引擎将使用此 KIE 基础查找流程的所有信息,或根据需要查找多个进程。

您可以从各种源(如类路径、文件系统或进程存储库)将资产加载到 KIE 库中。创建 KIE 基础是一个资源中心操作,因为它涉及从各种源加载和解析资产。您可以动态修改 KIE 基础,在运行时添加或删除进程定义和其他资产。

创建 KIE 基础后,您可以根据这个 KIE 基础实例化 KIE 会话。使用此 KIE 会话,根据 KIE 库中的定义运行进程。

当您使用 KIE 会话启动进程时,会创建一个新 进程实例。此实例维护了一个特定的进程状态。同一 KIE 会话中的不同实例可以使用相同的进程定义,但具有不同的状态。

图 66.1. 进程引擎中的 KIE 基础和 KIE 会话

例如,如果您开发一个应用程序来处理销售订购,您可以创建一个或多个进程定义来确定如何处理订购。启动应用程序时,首先需要创建一个包含这些进程定义的 KIE 基础。然后,您可以基于这个 KIE 基础创建一个会话。当新的销售订单进入时,按顺序启动新的进程实例。这个过程实例包含特定销售请求的进程状态。

您可以为相同的 KIE 基础创建多个 KIE 会话,并可在同一 KIE 会话中创建进程的多个实例。创建 KIE 会话,并在 KIE 会话中创建进程实例使用的资源比创建 KIE 基础少。如果您修改 KIE 基础,则使用它的所有 KIE 会话都可以自动使用修改。

在大多数简单的用例中,您可以使用单个 KIE 会话来执行所有进程。如果需要,也可以使用多个会话。例如,如果想要让不同客户订购处理完全独立,则可以为每个客户创建一个 KIE 会话。出于可扩展性的原因,您还可以使用多个会话。

在典型的应用程序中,您不需要直接创建 KIE 基础或 KIE 会话。但是,在使用进程引擎 API 的其他级别时,您可以和此级别定义的 API 元素进行交互。

66.1.1. KIE 基础

KIE 基础包括应用程序执行业务流程可能需要的所有流程定义和其他资产。

要创建 KIE 基础,请使用 KieHelper 实例从各种资源(如类路径或文件系统)加载进程,并创建新的 KIE 基础。

以下代码片段演示了如何创建只包含一个进程定义(从类路径加载)的 KIE 基础。

创建包含进程定义的 KIE 基础

  KieHelper kieHelper = new KieHelper();
  KieBase kieBase = kieHelper
    .addResource(ResourceFactory.newClassPathResource("MyProcess.bpmn"))
    .build();
Copy to Clipboard Toggle word wrap

ResourceFactory 类与从文件加载资源、URL、InputStream、Reader 和其它来源的方法类似。

注意

创建 KIE 基础的这种"手动"流程比其它其它替代方案简单,但会使应用程序难以维护。对于您希望在长时间内开发和维护的应用程序,使用其他创建 KIE 基础(如 RuntimeManager 类或 Services API)的方法。

66.1.2. KIE 会话

创建并加载 KIE 基础后,您可以创建一个 KIE 会话与进程引擎交互。您可以使用此会话启动和管理进程以及信号事件。

以下代码片段基于之前创建的 KIE 基础创建一个会话,然后启动进程实例,并引用进程定义中的 ID。

创建 KIE 会话并启动进程实例

KieSession ksession = kbase.newKieSession();
ProcessInstance processInstance = ksession.startProcess("com.sample.MyProcess");
Copy to Clipboard Toggle word wrap

66.1.3. ProcessRuntime 接口

KieSession 类会公开 ProcessRuntime 接口,它定义与进程交互的所有会话方法,如下所示:

ProcessRuntime 接口的定义

  /**
	 * Start a new process instance.  Use the process (definition) that
	 * is referenced by the given process ID.
	 *
	 * @param processId  The ID of the process to start
	 * @return the ProcessInstance that represents the instance of the process that was started
	 */
    ProcessInstance startProcess(String processId);

    /**
	 * Start a new process instance.  Use the process (definition) that
	 * is referenced by the given process ID.  You can pass parameters
	 * to the process instance as name-value pairs, and these parameters set
	 * variables of the process instance.
   *
	 * @param processId  the ID of the process to start
   * @param parameters  the process variables to set when starting the process instance
	 * @return the ProcessInstance that represents the instance of the process that was started
     */
    ProcessInstance startProcess(String processId,
                                 Map<String, Object> parameters);

    /**
     * Signals the process engine that an event has occurred. The type parameter defines
     * the type of event and the event parameter can contain additional information
     * related to the event.  All process instances that are listening to this type
     * of (external) event will be notified.  For performance reasons, use this type of
     * event signaling only if one process instance must be able to notify
     * other process instances. For internal events within one process instance, use the
     * signalEvent method that also include the processInstanceId of the process instance
     * in question.
     *
     * @param type the type of event
     * @param event the data associated with this event
     */
    void signalEvent(String type,
                     Object event);

    /**
     * Signals the process instance that an event has occurred. The type parameter defines
     * the type of event and the event parameter can contain additional information
     * related to the event.  All node instances inside the given process instance that
     * are listening to this type of (internal) event will be notified.  Note that the event
     * will only be processed inside the given process instance.  All other process instances
     * waiting for this type of event will not be notified.
     *
     * @param type the type of event
     * @param event the data associated with this event
     * @param processInstanceId the id of the process instance that should be signaled
     */
    void signalEvent(String type,
                     Object event,
                     long processInstanceId);

    /**
     * Returns a collection of currently active process instances.  Note that only process
     * instances that are currently loaded and active inside the process engine are returned.
     * When using persistence, it is likely not all running process instances are loaded
     * as their state is stored persistently.  It is best practice not to use this
     * method to collect information about the state of your process instances but to use
     * a history log for that purpose.
     *
     * @return a collection of process instances currently active in the session
     */
    Collection<ProcessInstance> getProcessInstances();

    /**
     * Returns the process instance with the given ID.  Note that only active process instances
     * are returned. If a process instance has been completed already, this method returns
     * null.
     *
     * @param id the ID of the process instance
     * @return the process instance with the given ID, or null if it cannot be found
     */
    ProcessInstance getProcessInstance(long processInstanceId);

    /**
     * Aborts the process instance with the given ID. If the process instance has been completed
     * (or aborted), or if the process instance cannot be found, this method will throw an
     * IllegalArgumentException.
     *
     * @param id the ID of the process instance
     */
    void abortProcessInstance(long processInstanceId);

    /**
     * Returns the WorkItemManager related to this session. This object can be used to
     * register new WorkItemHandlers or to complete (or abort) WorkItems.
     *
     * @return the WorkItemManager related to this session
     */
    WorkItemManager getWorkItemManager();
Copy to Clipboard Toggle word wrap

66.1.4. 关联密钥

在使用进程时,您可能需要将业务标识符分配给进程实例,然后使用标识符来引用实例,而不存储生成的实例 ID。

为提供这样的功能,流程引擎使用 CorrelationKey 接口,它可定义 CorrelationProperties。实施 CorrelationKey 的类可以具有描述它的单个属性,也可以是多功能集。属性的值或多个属性的值组合指的是唯一的实例。

KieSession 类实施 CorrelationAwareProcessRuntime 接口,以支持关联功能。这个接口会公开以下方法:

CorrelationAwareProcessRuntime 接口方法

      /**
      * Start a new process instance.  Use the process (definition) that
      * is referenced by the given process ID.  You can pass parameters
      * to the process instance (as name-value pairs), and these parameters set
      * variables of the process instance.
      *
      * @param processId  the ID of the process to start
      * @param correlationKey custom correlation key that can be used to identify the process instance
      * @param parameters  the process variables to set when starting the process instance
      * @return the ProcessInstance that represents the instance of the process that was started
      */
      ProcessInstance startProcess(String processId, CorrelationKey correlationKey, Map<String, Object> parameters);

      /**
      * Create a new process instance (but do not yet start it).  Use the process
      * (definition) that is referenced by the given process ID.
      * You can pass to the process instance (as name-value pairs),
      * and these parameters set variables of the process instance.
      * Use this method if you need a reference to the process instance before actually
      * starting it.  Otherwise, use startProcess.
      *
      * @param processId  the ID of the process to start
      * @param correlationKey custom correlation key that can be used to identify the process instance
      * @param parameters  the process variables to set when creating the process instance
      * @return the ProcessInstance that represents the instance of the process that was created (but not yet started)
      */
      ProcessInstance createProcessInstance(String processId, CorrelationKey correlationKey, Map<String, Object> parameters);

      /**
      * Returns the process instance with the given correlationKey.  Note that only active process instances
      * are returned.  If a process instance has been completed already, this method will return
      * null.
      *
      * @param correlationKey the custom correlation key assigned when the process instance was created
      * @return the process instance identified by the key or null if it cannot be found
      */
      ProcessInstance getProcessInstance(CorrelationKey correlationKey);
Copy to Clipboard Toggle word wrap

关联通常用于长时间运行的进程。如果要永久存储关联信息,您必须启用持久性。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat