1、ProcessEngineConfiguration
ProcessEngineConfiguration对象代表一个Activiti流程引擎的全部配置,该类下面有一系列的静态方法,用于读取和解析相应的配置文件,并且返回ProcessEngineConfiguration的对象实例。
1.1、createProcessEngineConfigurationFromResourceDefault方法
该方法会从classpath下默认读取activiti.cfg.xml文件,该文件是一个spring的bean配置文件,利用spring的依赖注入,获取processEngineConfiguration的bean实例。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-4.1.xsd">
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/act"/>
<property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUsername" value="root"/>
<property name="jdbcPassword" value="root"/>
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
1.2,createProcessEngineConfigurationFromResource方法
该方法就是不会默认的加载activiti.cfg.xml文件,而是会使用开发者自己传入的文件。这个方法接收两个参数,第一个是文件名称的字符串参数,第二个是通过这个文件完成依赖注入的对象的名称字符串,例如我在classpath创建了config.xml,里面利用定义了一个process的bean实例,所以我们在使用这个方法的时候应该是ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(“config.xml”,”process”)。
1.3,createProcessEngineConfigurationFromInputStream方法
这个方法的使用就不会限制我们一定要把文件放在classpath下,利用输入流我们可以读取任意的文件。
1.4,createStandaloneInMemProcessEngineConfiguration方法
这个方法不会读取任何的配置文件,所有的数据属性都是硬编码在代码中的,我们在使用的时候只需要指定databaseSchemaUpdate属性和jdbcUrl属性,而且这个使用的内存数据库,例如h2。如果在代码编写中需要改变某个属性的值,我们只需要调用这个类的set方法。
2,数据源的配置
当前是使用jdbc的形式,将数据库的配置信息写在了配置文件中,在activiti中ProcessEngineConfiguration引擎类中的方法也是可以接受数据源实例参数的,例如可以使用C3P0,DBCP的数据源。
2.1,C3P0数据源配置
<?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-4.1.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/act"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
2.2,DBCP数据源配置
<?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-4.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/act"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
测试java代码
package first;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
public class First {
public static void main(String[] args) {
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = engine.getRepositoryService();
RuntimeService runtimeService = engine.getRuntimeService();
TaskService taskService = engine.getTaskService();
repositoryService.createDeployment()
.addClasspathResource("bpmn/First.bpmn").deploy();
runtimeService.startProcessInstanceByKey("myProcess");
Task task = taskService.createTaskQuery().singleResult();
System.out.println("第一个任务完成前,当前任务名称是:" + task.getName());
taskService.complete(task.getId());
task = taskService.createTaskQuery().singleResult();
System.out.println("第二个任务完成前,当前任务名称是:" + task.getName());
taskService.complete(task.getId());
task = taskService.createTaskQuery().singleResult();
System.out.println("流程结束后,查找任务:" + task);
}
}