activiti helloworld
activiti的入门实践文章,重点在于动手做,要解决的是怎么做的问题。只有知道了怎么做后,才具有实际动手能力,才算对这门技术有一个初步掌握;至于更深入细化的知道它的设计理念、实现细节等,那是有时间深入时候再进行的工作了。
任务目标:
- 会用eclipse的activiti designer设计流程图;
- 会用java代码将设计好的流程图部署到数据库并进行简单测试;
- 将activiti与spring进行整合;
- 前端页面可实现任务待办、签收、办理、流程跟踪等事项;
环境核心部件版本:
- 数据库:oracle11g;
- activiti:5.14
- spring:3.2.3.RELEASE
相关资源
各步骤罗列
- 安装eclipse的activiti designer插件;
- 执行初始化数据库脚本;
- 搭建maven工程,添加activiti依赖;
- 配置activiti.cfg.xml文件;
- 使用activiti designer设计器定义一个简单流程;
- 代码部署流程、启动流程、签收任务、办理任务;
- 将activiti与spring进行整合,并使用springmvc来做Action层框架;
- 开发流程部署功能;
- 开发简单任务待办功能;
- 开发简单任务办理功能;
- 开发页面activiti流程跟踪图形展现功能;
- 集成网页流程设计器;
备注:
- 步骤1-6,在后台java工程就可完成,主要是后台功能的实现;
- 步骤7-11,是前端流程常用功能;
- 步骤12,属于高级功能,实际生产中流程图通常不太会变动,即使变动那也是专业维护人员来做;
1.安装eclipse的activiti desginer插件
Eclipse菜单栏 Help ---> Install new Software...--->add…
Location输入:http://www.activiti.org/designer/update/
勾选插件的最后一项
安装完Eclipse的流程设计器插件后,需要进行简单的设置,主要是为了在保存流程设计图的时候让插件自动帮助我们生产流程定义对应的图片文件,该文件对于后续的流程跟踪有用。
Eclipse菜单栏:window--->apreferences--->activiti--->Editor 按如下图勾选即可。
2.执行初始化数据库脚本
在源码或者war包中都有相应的建库脚本:
- war包中,有个database目录,里面包含相关脚本;
- 源码activiti-engine中,在resources/org/activity/db/中,也包含了同样的脚本;
找到对应数据库的脚本,初始化;
在我的测试项目中,用到的数据库是oracle,通过以下命令完成用户创建与授权:
create user activiti identified by activiti ; grant resources to activiti ; grant connect to activiti ;
3.搭建maven工程,添加activiti依赖
<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.x</version> </dependency>
上述步骤完成activiti依赖的添加;
4.配置activiti.cfg.xml文件
在resources目录下,添加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:oracle:thin:@192.168.100.21:1521/ora11g" /> <property name="jdbcDriver" value="oracle.jdbc.driver.OracleDriver" /> <property name="jdbcUsername" value="activiti" /> <property name="jdbcPassword" value="activiti" /> <!-- Database configurations --> <property name="databaseSchemaUpdate" value="false" /> <!-- job executor configurations --> <property name="jobExecutorActivate" value="false" /> <!-- mail server configurations --> <property name="mailServerPort" value="5025" /> <property name="history" value="full" /> </bean> </beans>
5.使用activiti designer设计器定义一个简单流程
设计一个简单的请假审批流程:
备注:
此处学习时最好采用英文来描述任务名称,避免入库时候的乱码问题;
6.代码部署流程、启动流程、签收任务、办理任务
/** * ClassName:HelloWorldTest <br/> * Function: 单元测试工作流 <br/> * Date: 2014年3月1日 下午5:11:50 <br/> * @author lt * @version * @since JDK 1.6 * @see */ public class HelloWorldTest { @Test public void test(){ //1.加载Activiti.cfg.xml配置文件对应的配置对象 ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault(); //2.通过processEngineConfiguratoin对象获取ProcessEngine实例 ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); Assert.assertNotNull(processEngine); //3.通过processEngine对象获取activiti的service RepositoryService repositoryService = processEngine.getRepositoryService(); //4.通过RepositoryService发布流程到数据库 repositoryService.createDeployment().addClasspathResource("process/helloworld/helloworld.bpmn20.xml").deploy(); //5.通过processEngine对象获取activiti的RuntimeService RuntimeService runtimeService = processEngine.getRuntimeService(); //6.通过RuntimeService开启流程 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess"); //7.流程往下走 通过TaskService获取流程任务 TaskService taskService = processEngine.getTaskService(); //8.查询当前任务 Task task = taskService.createTaskQuery().singleResult(); Assert.assertEquals("helloworld Task", task.getName()); //完成任务 流程往下走 taskService.complete(task.getId()); task = taskService.createTaskQuery().singleResult(); Assert.assertEquals("world Task", task.getName()); } }
备注:
由于任务签收需要设置签收人、角色等等,此处省略了该步骤;
7.将activiti与spring进行整合
- 添加maven依赖:
<dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>5.14</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>5.14</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>oracle.jdbc.driver.OracleDriver</groupId> <artifactId>OracleDriver</artifactId> <version>10.2.0</version> </dependency>
- 添加bean声明:
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@192.168.100.21:1521/ora11g" /> <property name="username" value="activiti" /> <property name="password" value="activiti" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="dataSource" ref="dataSource" /> <property name="transactionManager" ref="transactionManager" /> </bean> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration" /> </bean>
- 使用spring-test来测试是否能够顺利获取到实例
package com.xiaofan.hello.activiti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.task.Task; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Unit test for simple App. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/spring-config.xml") public class AppTest{ @Autowired ProcessEngine processEngine ; @Test public void testApp() { Assert.assertNotNull(processEngine); } @Test public void testDb(){ long count = processEngine.getRepositoryService().createDeploymentQuery().count(); Assert.assertTrue(count > 0); } }
关于springmvc,参见整理的springmvc helloworld