Activiti6.0教程 Service用途剖析 (二)

这节我们学习下Activiti的7大对象,首先我们从ProcessEngine接口开始看。

/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.activiti.engine;

import org.activiti.form.api.FormRepositoryService;

/**
 * Provides access to all the services that expose the BPM and workflow operations.
 * 
 * <ul>
 * <li>
 * <b>{@link org.activiti.engine.RuntimeService}: </b> Allows the creation of {@link org.activiti.engine.repository.Deployment}s and the starting of and searching on
 * {@link org.activiti.engine.runtime.ProcessInstance}s.</li>
 * <li>
 * <b>{@link org.activiti.engine.TaskService}: </b> Exposes operations to manage human (standalone) {@link org.activiti.engine.task.Task}s, such as claiming, completing and assigning tasks</li>
 * <li>
 * <b>{@link org.activiti.engine.IdentityService}: </b> Used for managing {@link org.activiti.engine.identity.User}s, {@link org.activiti.engine.identity.Group}s and the relations between them<</li>
 * <li>
 * <b>{@link org.activiti.engine.ManagementService}: </b> Exposes engine admin and maintenance operations</li>
 * <li>
 * <b>{@link org.activiti.engine.HistoryService}: </b> Service exposing information about ongoing and past process instances.</li>
 * </ul>
 * 
 * Typically, there will be only one central ProcessEngine instance needed in a end-user application. Building a ProcessEngine is done through a {@link ProcessEngineConfiguration} instance and is a
 * costly operation which should be avoided. For that purpose, it is advised to store it in a static field or JNDI location (or something similar). This is a thread-safe object, so no special
 * precautions need to be taken.
 * 
 * @author Tom Baeyens
 * @author Joram Barrez
 */
public interface ProcessEngine {

  /** the version of the activiti library */
  public static String VERSION = "6.0.0.4"; // Note the extra .x at the end. To cater for snapshot releases with different database changes

  /**
   * The name as specified in 'process-engine-name' in the activiti.cfg.xml configuration file. The default name for a process engine is 'default
   */
  String getName();

  void close();
  
  RepositoryService getRepositoryService();

  RuntimeService getRuntimeService();

  FormService getFormService();

  TaskService getTaskService();

  HistoryService getHistoryService();

  IdentityService getIdentityService();

  ManagementService getManagementService();
  
  DynamicBpmnService getDynamicBpmnService();

  ProcessEngineConfiguration getProcessEngineConfiguration();
  
  FormRepositoryService getFormEngineRepositoryService();
  
  org.activiti.form.api.FormService getFormEngineFormService();
}

可以看出来ProcessEngine顶级接口定义了一些Service,后面我们会详细说各个Service的作用。

 

 可以看到他的实现类有ProcessEngineImpl,接下来我们看下ProcessEngineImpl类:

  1 /* Licensed under the Apache License, Version 2.0 (the "License");
  2  * you may not use this file except in compliance with the License.
  3  * You may obtain a copy of the License at
  4  * 
  5  *      http://www.apache.org/licenses/LICENSE-2.0
  6  * 
  7  * Unless required by applicable law or agreed to in writing, software
  8  * distributed under the License is distributed on an "AS IS" BASIS,
  9  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 10  * See the License for the specific language governing permissions and
 11  * limitations under the License.
 12  */
 13 package org.activiti.engine.impl;
 14 
 15 import java.util.Map;
 16 
 17 import org.activiti.engine.DynamicBpmnService;
 18 import org.activiti.engine.FormService;
 19 import org.activiti.engine.HistoryService;
 20 import org.activiti.engine.IdentityService;
 21 import org.activiti.engine.ManagementService;
 22 import org.activiti.engine.ProcessEngine;
 23 import org.activiti.engine.ProcessEngines;
 24 import org.activiti.engine.RepositoryService;
 25 import org.activiti.engine.RuntimeService;
 26 import org.activiti.engine.TaskService;
 27 import org.activiti.engine.delegate.event.ActivitiEventType;
 28 import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
 29 import org.activiti.engine.impl.asyncexecutor.AsyncExecutor;
 30 import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
 31 import org.activiti.engine.impl.cfg.TransactionContextFactory;
 32 import org.activiti.engine.impl.interceptor.CommandExecutor;
 33 import org.activiti.engine.impl.interceptor.SessionFactory;
 34 import org.activiti.form.api.FormRepositoryService;
 35 import org.slf4j.Logger;
 36 import org.slf4j.LoggerFactory;
 37 
 38 /**
 39  * @author Tom Baeyens
 40  */
 41 public class ProcessEngineImpl implements ProcessEngine {
 42 
 43   private static Logger log = LoggerFactory.getLogger(ProcessEngineImpl.class);
 44 
 45   protected String name;
 46   protected RepositoryService repositoryService;
 47   protected RuntimeService runtimeService;
 48   protected HistoryService historicDataService;
 49   protected IdentityService identityService;
 50   protected TaskService taskService;
 51   protected FormService formService;
 52   protected ManagementService managementService;
 53   protected DynamicBpmnService dynamicBpmnService;
 54   protected FormRepositoryService formEngineRepositoryService;
 55   protected org.activiti.form.api.FormService formEngineFormService;
 56   protected AsyncExecutor asyncExecutor;
 57   protected CommandExecutor commandExecutor;
 58   protected Map<Class<?>, SessionFactory> sessionFactories;
 59   protected TransactionContextFactory transactionContextFactory;
 60   protected ProcessEngineConfigurationImpl processEngineConfiguration;
 61 
 62   public ProcessEngineImpl(ProcessEngineConfigurationImpl processEngineConfiguration) {
 63     this.processEngineConfiguration = processEngineConfiguration;
 64     this.name = processEngineConfiguration.getProcessEngineName();
 65     this.repositoryService = processEngineConfiguration.getRepositoryService();
 66     this.runtimeService = processEngineConfiguration.getRuntimeService();
 67     this.historicDataService = processEngineConfiguration.getHistoryService();
 68     this.identityService = processEngineConfiguration.getIdentityService();
 69     this.taskService = processEngineConfiguration.getTaskService();
 70     this.formService = processEngineConfiguration.getFormService();
 71     this.managementService = processEngineConfiguration.getManagementService();
 72     this.dynamicBpmnService = processEngineConfiguration.getDynamicBpmnService();
 73     this.asyncExecutor = processEngineConfiguration.getAsyncExecutor();
 74     this.commandExecutor = processEngineConfiguration.getCommandExecutor();
 75     this.sessionFactories = processEngineConfiguration.getSessionFactories();
 76     this.transactionContextFactory = processEngineConfiguration.getTransactionContextFactory();
 77     this.formEngineRepositoryService = processEngineConfiguration.getFormEngineRepositoryService();
 78     this.formEngineFormService = processEngineConfiguration.getFormEngineFormService();
 79 
 80     if (processEngineConfiguration.isUsingRelationalDatabase() && processEngineConfiguration.getDatabaseSchemaUpdate() != null) {
 81       commandExecutor.execute(processEngineConfiguration.getSchemaCommandConfig(), new SchemaOperationsProcessEngineBuild());
 82     }
 83 
 84     if (name == null) {
 85       log.info("default activiti ProcessEngine created");
 86     } else {
 87       log.info("ProcessEngine {} created", name);
 88     }
 89 
 90     ProcessEngines.registerProcessEngine(this);
 91 
 92     if (asyncExecutor != null && asyncExecutor.isAutoActivate()) {
 93       asyncExecutor.start();
 94     }
 95 
 96     if (processEngineConfiguration.getProcessEngineLifecycleListener() != null) {
 97       processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineBuilt(this);
 98     }
 99 
100     processEngineConfiguration.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createGlobalEvent(ActivitiEventType.ENGINE_CREATED));
101   }
102 
103   public void close() {
104     ProcessEngines.unregister(this);
105     if (asyncExecutor != null && asyncExecutor.isActive()) {
106       asyncExecutor.shutdown();
107     }
108 
109     commandExecutor.execute(processEngineConfiguration.getSchemaCommandConfig(), new SchemaOperationProcessEngineClose());
110 
111     if (processEngineConfiguration.getProcessEngineLifecycleListener() != null) {
112       processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineClosed(this);
113     }
114     
115     processEngineConfiguration.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createGlobalEvent(ActivitiEventType.ENGINE_CLOSED));
116   }
117 
118   // getters and setters
119   // //////////////////////////////////////////////////////
120 
121   public String getName() {
122     return name;
123   }
124 
125   public IdentityService getIdentityService() {
126     return identityService;
127   }
128 
129   public ManagementService getManagementService() {
130     return managementService;
131   }
132 
133   public TaskService getTaskService() {
134     return taskService;
135   }
136 
137   public HistoryService getHistoryService() {
138     return historicDataService;
139   }
140 
141   public RuntimeService getRuntimeService() {
142     return runtimeService;
143   }
144 
145   public RepositoryService getRepositoryService() {
146     return repositoryService;
147   }
148 
149   public FormService getFormService() {
150     return formService;
151   }
152   
153   public DynamicBpmnService getDynamicBpmnService() {
154     return dynamicBpmnService;
155   }
156 
157   public ProcessEngineConfigurationImpl getProcessEngineConfiguration() {
158     return processEngineConfiguration;
159   }
160   
161   public FormRepositoryService getFormEngineRepositoryService() {
162     return formEngineRepositoryService;
163   }
164   
165   public org.activiti.form.api.FormService getFormEngineFormService() {
166     return formEngineFormService;
167   }
168 }

可以看到在ProcessEngine的实现类ProcessEngineImpl里边初始化了各个Service。


 

RepositoryService

     Activiti 中每一个不同版本的业务流程的定义都需要使用一些定义文件,部署文件和支持数据 ( 例如 BPMN2.0 XML 文件,表单定义文件,流程定义图像文件等 ),这些文件都存储在 Activiti 内建的 Repository 中。Repository Service 提供了对 repository 的存取服务。

RuntimeService

     Activiti 中,每当一个流程定义被启动一次之后,都会生成一个相应的流程对象实例。Runtime Service 提供了启动流程、查询流程实例、设置获取流程实例变量等功能。此外它还提供了对流程部署,流程定义和流程实例的存取服务。

TaskService

   在 Activiti 中业务流程定义中的每一个执行节点被称为一个 Task,对流程中的数据存取,状态变更等操作均需要在 Task 中完成。Task Service 提供了对用户 Task 和 Form相关的操作。它提供了运行时任务查询、领取、完成、删除以及变量设置等功能。

IdentityService

    Activiti 中内置了用户以及组管理的功能,必须使用这些用户和组的信息才能获取到相应的 Task。Identity Service 提供了对 Activiti 系统中的用户和组的管理功能。

ManagementService

    Management Service 提供了对 Activiti 流程引擎的管理和维护功能,这些功能不在工作流驱动的应用程序中使用,主要用于 Activiti 系统的日常维护。

HistoryService

     History Service 用于获取正在运行或已经完成的流程实例的信息,与 Runtime Service 中获取的流程信息不同,历史信息包含已经持久化存储的永久信息,并已经被针对查询优化。

FormService

    Activiti 中的流程和状态 Task 均可以关联业务相关的数据。通过使用 Form Service 可以存取启动和完成任务所需的表单数据并且根据需要来渲染表单。

DynamicBpmnService

     一个新增的服务,用于动态修改流程中的一些参数信息等,是引擎中的一个辅助的服务

FormRepositoryService

   部署表单等

以上就是比较常用的一些Service,下面我们看看如何获取Service

//创建一个流程引擎
 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
 //获取RepositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
//获取RuntimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
//获取TaskService
TaskService taskService = processEngine.getTaskService();
//获取FormService
FormService formService = processEngine.getFormEngineFormService();
//获取DynamicBpmnService
 DynamicBpmnService dynamicBpmnService = processEngine.getDynamicBpmnService();
//获取ManagementService
 ManagementService managementService = processEngine.getManagementService();
        

以上的代码在创建流程引擎的时候也会把对应的表创建好,下一节我们看Activiti对应的表的含义。最后贴上一个activiti.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_activiti?characterEncoding=utf8" />
    <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUsername" value="root" />
    <property name="jdbcPassword" value="" />
    <property name="databaseSchemaUpdate" value="true" />
  </bean>

</beans>

 

posted @ 2019-06-12 20:18  坐看云起时_雨宣  阅读(805)  评论(0编辑  收藏  举报