Activiti搭建
Posted sucia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activiti搭建相关的知识,希望对你有一定的参考价值。
Activiti搭建
前期准备:
JDK+Eclipse+Tomcat+Maven的安装与配置
参考:http://blog.csdn.net/zhshulin/article/details/30779873
创建一个Maven项目来配置环境
参考:http://blog.csdn.net/zhshulin/article/details/37921705
1,导入activiti包文件
1 <activiti-version>5.18.0</activiti-version>
1 <!-- activiti依赖包 --> 2 <dependency> 3 <groupId>org.activiti</groupId> 4 <artifactId>activiti-engine</artifactId> 5 <version>${activiti-version}</version> 6 </dependency> 7 <dependency> 8 <groupId>org.activiti</groupId> 9 <artifactId>activiti-spring</artifactId> 10 <version>${activiti-version}</version> 11 </dependency> 12 <dependency> 13 <groupId>org.activiti</groupId> 14 <artifactId>activiti-bpmn-converter</artifactId> 15 <version>${activiti-version}</version> 16 </dependency> 17 <dependency> 18 <groupId>org.activiti</groupId> 19 <artifactId>activiti-bpmn-model</artifactId> 20 <version>${activiti-version}</version> 21 </dependency> 22 <dependency> 23 <groupId>org.activiti</groupId> 24 <artifactId>activiti-bpmn-layout</artifactId> 25 <version>${activiti-version}</version> 26 </dependency>
2、 eclipse安装activiti-designer插件
打开eclipse软件,然后点击菜单栏的help选项,选择install New Software,示例如下:
出现如下对话框:
点击添加【Add】按钮,出现如下对话框
输入如下地址:
Location:http://activiti.org/designer/update/
Name:Activiti
点击OK按钮出现如下窗体:
选择Activiti BPMN Desisner,然后一直选择下一步,直到出现如下窗体:
如果出现这个窗体,就表明eclipse正在给您下载相应的插件,但是这个办法是在有网的情况下,而且会比较慢,你得耐心等待,
当然还有一个简单的办法,就是我们将下载好的插件放到指定文件夹下。
如果能出现,就证明我们的工作流插件就算是安装成功了。
3、spring-activiti.xml配置文件
建立spring-activiti.xml配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 6 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 7 <property name="url" value="jdbc:mysql://localhost:3306/数据库名称" /> 8 <property name="username" value="用户名" /> 9 <property name="password" value="密码" /> 10 <property name="defaultAutoCommit" value="false" /> 11 </bean> 12 13 <bean id="processEngineConfiguration" 14 class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> 15 <property name="dataSource" ref="dataSource" /> 16 <property name="databaseType" value="mysql"/> 17 <property name="databaseSchemaUpdate" value="true" /> 18 <property name="jobExecutorActivate" value="false" /> 19 </bean> 20 21 <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> 22 <property name="processEngineConfiguration" ref="processEngineConfiguration" /> 23 </bean> 24 25 <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" /> 26 <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" /> 27 <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" /> 28 <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" /> 29 <bean id="formService" factory-bean="processEngine" factory-method="getFormService" /> 30 <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" /> 31 32 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 33 <property name="dataSource" ref="dataSource" /> 34 </bean> 35 </beans>
4、生成activiti数据表
BaseTestCase.java文件,运行
1 package org.projMgr.TestCase; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.test.context.ContextConfiguration; 6 import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; 7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 import org.springframework.test.context.transaction.TransactionConfiguration; 9 import org.springframework.transaction.annotation.Transactional; 10 11 12 13 @RunWith(SpringJUnit4ClassRunner.class) 14 @ContextConfiguration({"classpath:spring-activiti.xml"}) 15 @TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false) 16 @Transactional 17 public class BaseTestCase extends AbstractTransactionalJUnit4SpringContextTests{ 18 19 @Test 20 public void test(){ 21 System.out.println ("通导test"); 22 } 23 24 }
运行结果:
1 SLF4J: Class path contains multiple SLF4J bindings. 2 SLF4J: Found binding in [jar:file:/D:/developing/eclipse/workspace/ProjectManagement/projectManagement/src/main/webapp/WEB-INF/lib/slf4j-log4j12-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class] 3 SLF4J: Found binding in [jar:file:/C:/Users/Administrator/.m2/repository/org/slf4j/slf4j-log4j12/1.7.7/slf4j-log4j12-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class] 4 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 5 SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] 6 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [spring-activiti.xml] 7 [org.springframework.context.support.GenericApplicationContext] - Refreshing org.springframework.context.support.GenericApplicationContext@5383967b: startup date [Tue Feb 28 18:32:15 CST 2017]; root of context hierarchy 8 [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - JSR-330 \'javax.inject.Inject\' annotation found and supported for autowiring 9 [org.activiti.engine.impl.db.DbSqlSession] - performing create on engine with resource org/activiti/db/create/activiti.mysql.create.engine.sql 10 [org.activiti.engine.impl.db.DbSqlSession] - Found MySQL: majorVersion=5 minorVersion=1 11 [org.activiti.engine.impl.db.DbSqlSession] - performing create on history with resource org/activiti/db/create/activiti.mysql.create.history.sql 12 [org.activiti.engine.impl.db.DbSqlSession] - Found MySQL: majorVersion=5 minorVersion=1 13 [org.activiti.engine.impl.db.DbSqlSession] - performing create on identity with resource org/activiti/db/create/activiti.mysql.create.identity.sql 14 [org.activiti.engine.impl.db.DbSqlSession] - Found MySQL: majorVersion=5 minorVersion=1 15 [org.activiti.engine.impl.ProcessEngineImpl] - ProcessEngine default created 16 [org.springframework.test.context.transaction.TransactionalTestExecutionListener] - Began transaction (1) for test context [DefaultTestContext@15eebbff testClass = BaseTestCase, testInstance = org.projMgr.TestCase.BaseTestCase@22d6f11, testMethod = test@BaseTestCase, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@30990c1b testClass = BaseTestCase, locations = \'{classpath:spring-activiti.xml}\', classes = \'{}\', contextInitializerClasses = \'[]\', activeProfiles = \'{}\', contextLoader = \'org.springframework.test.context.support.DelegatingSmartContextLoader\', parent = [null]]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@2453f95d]; rollback [false] 17 通导test 18 [org.springframework.test.context.transaction.TransactionalTestExecutionListener] - Committed transaction after test execution for test context [DefaultTestContext@15eebbff testClass = BaseTestCase, testInstance = org.projMgr.TestCase.BaseTestCase@22d6f11, testMethod = test@BaseTestCase, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@30990c1b testClass = BaseTestCase, locations = \'{classpath:spring-activiti.xml}\', classes = \'{}\', contextInitializerClasses = \'[]\', activeProfiles = \'{}\', contextLoader = \'org.springframework.test.context.support.DelegatingSmartContextLoader\', parent = [null]]] 19 [org.springframework.context.support.GenericApplicationContext] - Closing org.springframework.context.support.GenericApplicationContext@5383967b: startup date [Tue Feb 28 18:32:15 CST 2017]; root of context hierarchy
数据库自动生成25张表:
5、测试
在需要存放文件的文件夹右键-new-other,选择activiti diagram
设置文件名-next
activiti diagram内置了一些 流程定义模板,但是这里我们选择不使用:
finish之后出现.bpmn文件:
打开这个文件,输入配置信息:
id:流程的唯一标识,建议使用纯英文
在设计区域右边找到StartEven,拖进来:
设置id、name(这里使用默认,没改)
单击左下第二个图标,选择“create user task”
设置id、name
设置节点分配人:
再建两个user task以及EndEvent:
右击文件Leave.bpmn选择OpenWith-XML Editor,可查看XML格式文件,将请假申请、销假的节点分配人设置为proposer(可在上一步设置):
设置自动生成流程图片:
单击window菜单,选择preference,按下图设置:
在原有流程中随意拖动一点,再次保存,即可在同目录下生成png文件:
自动生成测试代码:
在Leave.bpmn文件右键-activiti-generate unit test,之后会在test/java/org/activiti/test包中生成ProcessTestLeave.java文件,
把它拉到holleworld文件夹下,修改包路径
ProcessTestLeave.java文件代码,其中filename路径的\\需要修改为/:
1 package org.holleworld; 2 3 import static org.junit.Assert.*; 4 5 import java.util.HashMap; 6 import java.util.Map; 7 import java.io.FileInputStream; 8 9 import org.activiti.engine.RepositoryService; 10 import org.activiti.engine.RuntimeService; 11 import org.activiti.engine.runtime.ProcessInstance; 12 import org.activiti.engine.test.ActivitiRule; 13 import org.junit.Rule; 14 import org.junit.Test; 15 16 public class ProcessTestLeave { 17 18 private String filename = "D:/developing/eclipse/workspace/ProjectManagement/projectManagement/src/test/java/org/holleworld/Leave.bpmn"; 19 20 @Rule 21 public ActivitiRule activitiRule = new ActivitiRule(); 22 23 @Test 24 public void startProcess() throws Exception { 25 RepositoryService repositoryService = activitiRule.getRepositoryService(); 26 repositoryService.createDeployment().addInputStream("leave.bpmn20.xml", 27 new FileInputStream(filename)).deploy(); 28 RuntimeService runtimeService = activitiRule.getRuntimeService(); 29 Map<String, Object> variableMap = new HashMap<String, Object>(); 30 variableMap.put("name", "Activiti"); 31 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave", variableMap); 32 assertNotNull(processInstance.getId()); 33 System.out.println("id " + processInstance.getId() + " " 34 + processInstance.getProcessDefinitionId()); 35 } 36 }
run as - JUnitTest,出错......
repositoryService.createDeployment().addInputStream("leave.bpmn20.xml",new FileInputStream(filename)).deploy();
这步就跳出,且没有报错信息......尝试
修改leave.bpmn20.xml变成leave.bpmn.xml或者leave.bpmn
修改filename = "org/holleworld/Leave.bpmn";
均无用。
另外找到测试代码:
TestActivitiHelloLeave.java
1 package org.projMgr.TestCase; 2 3 import java.util.Date; 4 import java.util.List; 5 6 import org.activiti.engine.HistoryService; 7 import org.activiti.engine.ProcessEngine; 8 import org.activiti.engine.ProcessEngineConfiguration; 9 import org.activiti.engine.RepositoryService; 10 import org.activiti.engine.RuntimeService; 11 import org.activiti.engine.TaskService; 12 import org.activiti.engine.history.HistoricActivityInstance; 13 import org.activiti.engine.history.HistoricActivityInstanceQuery; 14 import org.activiti.engine.history.HistoricProcessInstance; 15 import org.activiti.engine.history.HistoricProcessInstanceQuery; 16 import org.activiti.engine.history.HistoricTaskInstance; 17 import org.activiti.engine.history.HistoricTaskInstanceQuery; 18 import org.activiti.engine.repository.Deployment; 19 import org.activiti.engine.repository.DeploymentBuilder; 20 import org.activiti.engine.runtime.ProcessInstance; 21 import org.activiti.engine.task.Task; 22 import org.activiti.engine.task.TaskQuery; 23 import org.junit.Before; 24 import org.junit.Test; 25 26 /** 27 * Activiti入门程序 28 * 29 */ 30 public class TestActivitiHello { 31 ProcessEngine processEngine = null; 32 @Before 33 public void init(){ 34 //自动加载classpath下名为activiti.cfg.xml文件 35 processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("spring-activiti.xml").buildProcessEngine(); 36 } 37 38 39 40 /** 部署流程定义 */ 41 @Test 42 public void deploymentProcessDefinitionTest() { 43 /** 44 * RepositoryService是Activiti的仓库服务类,流程定义和部署对象相关的Service 45 * 所谓的仓库指流程定义文档的两个文件:bpmn文件和流程图片 46 */ 47 RepositoryService repositoryService = processEngine.getRepositoryService(); 48 // 创建一个部署对象DeploymentBuilder,用来定义流程部署的相关参数 49 DeploymentBuilder deploymentBuilder = repositoryService.createDeployment(); 50 // 添加部署的名称 51 deploymentBuilder.name("activiti"); 52 // 添加hello.bpmn和hello.png 53 deploymentBuilder.addClasspathResource("org/projMgr/TestCase/hello.bpmn"); 54 deploymentBuilder.addClasspathResource("org/projMgr/TestCase/hello.png"); 55 // 部署流程定义 56 Deployment deployment = deploymentBuilder.deploy(); 57 58 System.out.println("部署ID:" + deployment.getId());//1 59 System.out.println("部署名称:" + deployment.getName());//activiti入门程序 60 } 61 62 // /** 启动流程实例 */ 63 // @Test 64 // public void startHelloProcessInstanceTest() { 65 // // 流程定义的key 66 // String processDefinitionKey = "hello"; 67 // RuntimeService service = processEngine.getRuntimeService(); 68 // /** 69 // * 使用流程定义的key启动流程实例,key对应hello.bpmn文件中id的属性值, 70 // * 使用key值启动,默认是按照最新版本的流程定义启动 71 // */ 72 // ProcessInstance pi = service.startProcessInstanceByKey(processDefinitionKey); 73 // System.out.println("流程实例ID:" + pi.getId());// 流程实例ID:2501 74 // // 流程定义ID:hello:1:4 75 // System.out.println("流程定义ID:" + pi.getProcessDefinitionId()); 76 // } 77 78 // @Test 79 // public void setVariables(){ 80 // String taskId = "342504"; 81 // TaskService taskService = processEngine.getTaskService(); 82 // taskService.setVariableLocal(taskId, "请假天数", 3); //Activiti6系列- 核心API