第4章 Activiti 服务架构图

目录

第4章 Activiti 服务架构图

4.1 activiti.cfg.xml

4.2 ProcessEngineConfiguration:

4.2.1 StandaloneProcessEngineConfiguration

4.2.2 SpringProcessEngineConfiguration

4.2.3 创建 processEngineConfiguration

4.3 ProcessEngine

4.3.1一般创建方式

4.3.2简单创建方式

4.4 Service

4.4.1 Service 创建方式

4.4.2 Service 总览

4.4.3 RepositoryService

4.4.4 RuntimeService

4.4.5 TaskService

4.4.6 HistoryService

4.4.7 ManagementService


第4章 Activiti 服务架构图

在新版本中,我们通过实验可以发现 IdentityService, FormService 两个 Serivce 都已经删除了。所以后面我们对于这两个 Service 也不讲解了,但老版本中还是有这两个 Service。

4.1 activiti.cfg.xml

activiti 的引擎配置文件,包括: ProcessEngineConfiguration 的定义、数据源定义、事务管理器等,此文件其实就是一个 spring 配置文件,下面是一个基本的配置只配置了 ProcessEngineConfiguration和数据源:

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

    <!--数据源配置-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/activiti" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
        <property name="maxActive" value="3" />
        <property name="maxIdle" value="1" />
    </bean>

    <!--processEngineConfiguration 用来创建 ProcessEngine,在创建 ProcessEngine 时会执行数据库的操作-->
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!--代表数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--activiti数据库表处理策略:代表是否生成表结构,true代表生成-->
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>

    <!--在 activiti.cfg.xml 配置文件中的 dataSource 和 processEngineConfiguration 也可以使用一次性配置出来。-->
    <!--<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti"/>
        <property name="jdbcUsername" value="root"/>
        <property name="jdbcPassword" value="123456"/>
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>-->
</beans>

4.2 ProcessEngineConfiguration:

流程引擎的配置类,通过 ProcessEngineConfiguration 可以创建工作流引擎 ProceccEngine,常用的两种方法如下:

4.2.1 StandaloneProcessEngineConfiguration

通过 org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration

Activiti 可以单独运行,使用它创建的 ProcessEngine, Activiti 会自己处理事务。

配置文件方式:通常在 activiti.cfg.xml 配置文件中定义一个 id 为 processEngineConfiguration 的 bean,这里会使用 spring 的依赖注入来构建引擎。方法如下:
 

    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 数据库策略 -->
        <property name="databaseSchemaUpdate" value="true"/>
    </bean>

4.2.2 SpringProcessEngineConfiguration

通过 org.activiti.spring.SpringProcessEngineConfiguration 与 Spring 整合。

创建 spring 与 activiti 的整合配置文件:activity-spring.cfg.xml(名称不固定)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
    <!-- 工作流引擎配置bean -->
    <bean id="processEngineConfiguration"
          class="org.activiti.spring.SpringProcessEngineConfiguration">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 使用spring事务管理器 -->
        <property name="transactionManager" ref="transactionManager" />
        <!-- 数据库策略 -->
        <property name="databaseSchemaUpdate" value="drop-create" />
        <!-- activiti的定时任务关闭 -->
        <property name="jobExecutorActivate" value="false" />
    </bean>
    <!-- 流程引擎 -->
    <bean id="processEngine"
          class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration"
                  ref="processEngineConfiguration" />
    </bean>
    <!-- 资源服务service -->
    <bean id="repositoryService" factory-bean="processEngine"
          factory-method="getRepositoryService" />
    <!-- 流程运行service -->
    <bean id="runtimeService" factory-bean="processEngine"
          factory-method="getRuntimeService" />
    <!-- 任务管理service -->
    <bean id="taskService" factory-bean="processEngine"
          factory-method="getTaskService" />
    <!-- 历史管理service -->
    <bean id="historyService" factory-bean="processEngine"
          factory-method="getHistoryService" />
    <!-- 用户管理service -->
    <bean id="identityService" factory-bean="processEngine"
          factory-method="getIdentityService" />
    <!-- 引擎管理service -->
    <bean id="managementService" factory-bean="processEngine"
          factory-method="getManagementService" />
    <!-- 数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/activiti" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
        <property name="maxActive" value="3" />
        <property name="maxIdle" value="1" />
    </bean>
    <!-- 事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 传播行为 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 切面,根据具体项目修改切点配置 -->
    <aop:config proxy-target-class="true">
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.yzn.service.impl.*.*(..))" />
    </aop:config>
</beans>

4.2.3 创建 processEngineConfiguration

ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                        .createProcessEngineConfigurationFromResource("activiti.cfg.xml");

上边的代码要求 activiti.cfg.xml 中必须有一个 processEngineConfiguration 的 bean也可以使用下边的方法,更改 bean 的名字:

ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(String resource, String beanName);

4.3 ProcessEngine

工作流引擎,相当于一个门面接口,通过 ProcessEngineConfiguration 创建 processEngine,通过ProcessEngine 创建各个 service 接口。

4.3.1一般创建方式

//通过ProcessEngineConfiguration创建ProcessEngine
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();

4.3.2简单创建方式

将 activiti.cfg.xml 文件名及路径固定,且 activiti.cfg.xml 文件中有 processEngineConfiguration 的配置,可以使用如下代码创建 processEngine:

//使用classpath下的activiti.cfg.xml中的配置创建processEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

4.4 Service

4.4.1 Service 创建方式

通过 ProcessEngine 创建 Service, Service 是工作流引擎提供用于进行工作流部署、执行、管理的服务接口。方式如下:

RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();
TaskService taskService = processEngine.getTaskService();
……

4.4.2 Service 总览

RepositoryServiceactiviti 的资源管理类
RuntimeServiceactiviti 的流程运行管理类
TaskServiceactiviti 的任务管理类
HistoryServiceactiviti 的历史管理类
ManagerServiceactiviti 的引擎管理类

注: 红色标注为常用 service。

4.4.3 RepositoryService

是 activiti 的资源管理类, 提供了管理和控制流程发布包和流程定义的操作。使用工作流建模工具设计的业务流程图需要使用此 service 将流程定义文件的内容部署到计算机。

除了部署流程定义以外还可以:

查询引擎中的发布包和流程定义。

暂停或激活发布包,对应全部和特定流程定义。 暂停意味着它们不能再执行任何操作了,激活是对应的反向操作。

获得多种资源,像是包含在发布包里的文件, 或引擎自动生成的流程图。

获得流程定义的 pojo 版本, 可以用来通过 java 解析流程,而不必通过 xml。

4.4.4 RuntimeService

它是 activiti 的流程运行管理类。可以从这个服务类中获取很多关于流程执行相关的信息。

4.4.5 TaskService

是 activiti 的任务管理类。可以从这个类中获取任务的信息。

4.4.6 HistoryService

是 activiti 的历史管理类,可以查询历史信息, 执行流程时,引擎会保存很多数据(根据配置),比如流程实例启动时间,任务的参与者, 完成任务的时间,每个流程实例的执行路径,等等。 这个服务主要通过查询功能来获得这些数据。

4.4.7 ManagementService

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值