public class TestDB {public static void main(String[] args) {//1。 创建Activiti配置对象的实例ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();//2. 设置数据库连接信息// 设置数据库地址configuration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti_test?createDatabaseIfNotExist=true");// 数据库驱动configuration.setJdbcDriver("com.mysql.jdbc.Driver");// 用户名configuration.setJdbcUsername("root");// 密码configuration.setJdbcPassword("123456");// 设置数据库建表策略(默认不会建表) configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);//3. 使用配置对象创建流程引擎实例(检查数据库连接等环境信息是否正确)ProcessEngine processEngine = configuration.buildProcessEngine();System.out.println(processEngine);}
public void testDBByProperties() throws Exception {// 1。 加载classpath下名为activiti.cfg.xml文件,创建核心流程引擎对象ProcessEngine processEngine = ProcessEngineConfiguration.
createProcessEngineConfigurationFromResource("activiti.cfg.xml").
buildProcessEngine();System.out.println(processEngine);//或者 private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();此方法默认和上面一样 } }
activiti.cfg.xml
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"><!-- 数据库连接配置 --><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti_test?createDatabaseIfNotExist=true"></property><property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property><property name="jdbcUsername" value="root"></property><property name="jdbcPassword" value="mikesirius"></property><!-- 建表策略 --><property name="databaseSchemaUpdate" value="true"></property></bean></beans>
Spring集成
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"><!-- 数据源 --><property name="dataSource" ref="dataSource" /><!-- 配置事务管理器,统一事务 --><property name="transactionManager" ref="transManager" /><!-- 设置建表策略 --><property name="databaseSchemaUpdate" value="true" /></bean><bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"><property name="processEngineConfiguration" ref="processEngineConfiguration" /></bean><!-- bean id repositoryServiceRepositoryServicie repositoryService = processEngine.getRepositoryService();--><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="formService" factory-bean="processEngine" factory-method="getFormService" /><!-- 流程相关的Service --><bean id="workflowService" class="activitiweb.service.impl.WorkflowServiceImpl"><property name="repositoryService" ref="repositoryService"></property><property name="runtimeService" ref="runtimeService"></property><property name="taskService" ref="taskService"></property><property name="formService" ref="formService"></property><property name="historyService" ref="historyService"></property></bean></beans>
核心API介绍
1、ProcessEngine
在Activiti中最核心的类,其他的类都是由他而来。
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
可以通过processEngine 的get产生以下Service
RepositoryService | 管理流程定义 |
RuntimeService | 执行管理,包括启动、推进、删除流程实例等操作 |
TaskService | 任务管理 |
HistoryService | 历史管理(执行完的数据的管理) |
IdentityService | 组织机构管理 |
FormService | 一个可选服务,任务表单管理 |
ManagerService |
|
2、RepositoryService
是Activiti的仓库服务类。所谓的仓库指流程定义文档的两个文件:bpmn文件和流程图片。
3、RuntimeService
是activiti的流程执行服务类。可以从这个服务类中获取很多关于流程执行相关的信息。
4、TaskService
是activiti的任务服务类。可以从这个类中获取任务的信息。
5、ProcessDefinition
流程定义类。可以从这里获得资源文件等。
6、ProcessInstance
代表流程定义的执行实例。如范冰冰请了一天的假,她就必须发出一个流程实例的申请。一个流程实例包括了所有的运行节点。我们可以利用这个对象来了解当前流程实例的进度等信息。
从源代码中可以看出ProcessInstance就是Execution,ProcessInstance继承Execution
7、Execution
Activiti用这个对象去描述流程执行的每一个节点。在没有并发的情况下,同ProcessInstance。
Activiti核心配置文件,配置流程引擎创建工具的基本参数和数据库连接池参数。
定义数据库配置参数:
jdbcUrl: 数据库的JDBC URL。
jdbcDriver: 对应不同数据库类型的驱动。
jdbcUsername: 连接数据库的用户名。
jdbcPassword: 连接数据库的密码。
基于JDBC参数配置的数据库连接 会使用默认的MyBatis连接池。 下面的参数可以用来配置连接池(来自MyBatis参数):
jdbcMaxActiveConnections: 连接池中处于被使用状态的连接的最大值。默认为10。
jdbcMaxIdleConnections: 连接池中处于空闲状态的连接的最大值。
jdbcMaxCheckoutTime: 连接被取出使用的最长时间,超过时间会被强制回收。 默认为20000(20秒)。
jdbcMaxWaitTime: 这是一个底层配置,让连接池可以在长时间无法获得连接时, 打印一条日志,并重新尝试获取一个连接。(避免因为错误配置导致沉默的操作失败)。 默认为20000(20秒)。