Activiti学习笔记2 — HelloWorld
一、 Maven的POM配置文件
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>com.mcs</groupId> 4 <artifactId>ativiti01</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 8 <name>activiti</name> 9 <url>http://maven.apache.org</url> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 </properties> 14 15 <dependencies> 16 17 <!-- Junit 4.12 --> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <scope>test</scope> 22 </dependency> 23 <!-- javax.servlet --> 24 <dependency> 25 <groupId>javax.servlet</groupId> 26 <artifactId>javax.servlet-api</artifactId> 27 <scope>provided</scope> 28 </dependency> 29 <dependency> 30 <groupId>javax.servlet.jsp</groupId> 31 <artifactId>javax.servlet.jsp-api</artifactId> 32 <scope>provided</scope> 33 </dependency> 34 <dependency> 35 <groupId>javax.servlet</groupId> 36 <artifactId>jstl</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>javax.servlet.jsp.jstl</groupId> 40 <artifactId>javax.servlet.jsp.jstl-api</artifactId> 41 </dependency> 42 43 <!-- Log4j 1.2.17 --> 44 <dependency> 45 <groupId>log4j</groupId> 46 <artifactId>log4j</artifactId> 47 </dependency> 48 49 <!-- Spring 4.2.3.RELEASE --> 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-core</artifactId> 53 </dependency> 54 <dependency> 55 <groupId>org.springframework</groupId> 56 <artifactId>spring-context</artifactId> 57 </dependency> 58 <dependency> 59 <groupId>org.springframework</groupId> 60 <artifactId>spring-context-support</artifactId> 61 </dependency> 62 <dependency> 63 <groupId>org.springframework</groupId> 64 <artifactId>spring-orm</artifactId> 65 </dependency> 66 <dependency> 67 <groupId>org.springframework</groupId> 68 <artifactId>spring-webmvc</artifactId> 69 </dependency> 70 <dependency> 71 <groupId>org.springframework</groupId> 72 <artifactId>spring-tx</artifactId> 73 </dependency> 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-jdbc</artifactId> 77 </dependency> 78 <dependency> 79 <groupId>org.springframework</groupId> 80 <artifactId>spring-aspects</artifactId> 81 </dependency> 82 <dependency> 83 <groupId>org.springframework</groupId> 84 <artifactId>spring-messaging</artifactId> 85 </dependency> 86 <dependency> 87 <groupId>org.springframework</groupId> 88 <artifactId>spring-test</artifactId> 89 </dependency> 90 91 <!-- Mysql --> 92 <dependency> 93 <groupId>mysql</groupId> 94 <artifactId>mysql-connector-java</artifactId> 95 </dependency> 96 97 <!-- Commons --> 98 <dependency> 99 <groupId>commons-io</groupId> 100 <artifactId>commons-io</artifactId> 101 </dependency> 102 <dependency> 103 <groupId>commons-fileupload</groupId> 104 <artifactId>commons-fileupload</artifactId> 105 </dependency> 106 <dependency> 107 <groupId>commons-logging</groupId> 108 <artifactId>commons-logging</artifactId> 109 </dependency> 110 111 <!-- Activiti --> 112 <dependency> 113 <groupId>org.activiti</groupId> 114 <artifactId>activiti-engine</artifactId> 115 <version>5.18.0</version> 116 </dependency> 117 <dependency> 118 <groupId>org.activiti</groupId> 119 <artifactId>activiti-bpmn-model</artifactId> 120 <version>5.18.0</version> 121 </dependency> 122 <dependency> 123 <groupId>org.activiti</groupId> 124 <artifactId>activiti-spring</artifactId> 125 <version>5.18.0</version> 126 </dependency> 127 128 <!-- Mybatis --> 129 <dependency> 130 <groupId>org.mybatis</groupId> 131 <artifactId>mybatis</artifactId> 132 <version>3.2.5</version> 133 </dependency> 134 <dependency> 135 <groupId>org.mybatis</groupId> 136 <artifactId>mybatis-spring</artifactId> 137 <version>1.2.3</version> 138 </dependency> 139 <dependency> 140 <groupId>org.mybatis.generator</groupId> 141 <artifactId>mybatis-generator-core</artifactId> 142 <version>1.3.2</version> 143 </dependency> 144 145 </dependencies> 146 147 <dependencyManagement> 148 <dependencies> 149 <dependency> 150 <groupId>io.spring.platform</groupId> 151 <artifactId>platform-bom</artifactId> 152 <version>2.0.0.RELEASE</version> 153 <type>pom</type> 154 <scope>import</scope> 155 </dependency> 156 </dependencies> 157 </dependencyManagement> 158 159 160 <build> 161 <finalName>activiti</finalName> 162 </build> 163 164 </project>
二、activiti.cfg.xml配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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 3 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 4 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> 5 6 <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> 7 <!-- 数据库连接属性 --> 8 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?characterEncoding=utf-8"></property> 9 <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property> 10 <property name="jdbcUsername" value="root"></property> 11 <property name="jdbcPassword" value="root"></property> 12 13 <!-- 数据库更新策略 --> 14 <property name="databaseSchemaUpdate" value="true"></property> 15 16 <property name="jobExecutorActivate" value="false"></property> 17 18 <!-- 邮件服务地址 --> 19 <property name="mailServerHost" value="mail.wxintl.com"></property> 20 <property name="mailServerPort" value="5025"></property> 21 </bean> 22 23 </beans>
三、log4j.properties配置文件
1 # Global logging configuration 2 log4j.rootLogger=INFO, Console, OneFile,HtmlFile 3 4 #log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE 5 #log4j.logger.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE 6 #log4j.logger.org.hibernate.tool.hbm2ddl=DEBUG 7 #log4j.logger.org.hibernate.SQL=DEBUG 8 9 log4j.appender.Console=org.apache.log4j.ConsoleAppender 10 log4j.appender.Console.Target=System.out 11 log4j.appender.Console.layout=org.apache.log4j.PatternLayout 12 log4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n 13 14 log4j.appender.OneFile=org.apache.log4j.RollingFileAppender 15 log4j.appender.OneFile.File=../activiti.log 16 log4j.appender.OneFile.MaxFileSize=10MB 17 log4j.appender.OneFile.Threshold=ALL 18 log4j.appender.OneFile.layout=org.apache.log4j.PatternLayout 19 log4j.appender.OneFile.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n 20 21 log4j.appender.HtmlFile=org.apache.log4j.DailyRollingFileAppender 22 log4j.appender.HtmlFile.file=../activiti_log 23 log4j.appender.HtmlFile.DatePattern='_'yyyy-MM-dd'.html' 24 log4j.appender.HtmlFile.layout=org.apache.log4j.HTMLLayout 25 log4j.appender.HtmlFile.Threshold=ALL
四、创建核心对象 ProcessEngine
private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
五、发布流程
1 @Test 2 public void testDeployProcess() { 3 RepositoryService repositoryService = processEngine.getRepositoryService(); 4 // 创建发布对象 5 DeploymentBuilder builder = repositoryService.createDeployment(); 6 // 加载发布资源 7 builder.addClasspathResource("leave.bpmn").addClasspathResource("leave.png"); 8 // 发布资源 9 builder.deploy(); 10 }
六、启动流程
1 @Test 2 public void testStartProcess() { 3 RuntimeService runtimeService = processEngine.getRuntimeService(); 4 runtimeService.startProcessInstanceByKey("myProcess"); 5 }
七、查看任务
1 @Test 2 public void testQueryTask() { 3 TaskService taskService = processEngine.getTaskService(); 4 //String assignee = "employee"; 5 String assignee = "manager"; 6 List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list(); 7 for (Task task : tasks) { 8 logger.info("ID:" + task.getId()); 9 logger.info("Name:" + task.getName()); 10 logger.info("Assignee:" + task.getAssignee()); 11 logger.info("CreateTime:" + task.getCreateTime()); 12 } 13 }
八、处理任务
1 @Test 2 public void testCompleteTask(){ 3 TaskService taskService = processEngine.getTaskService(); 4 String taskId = "7502"; 5 taskService.complete(taskId); 6 }