首先在eclipse内添加activiti的插件,https://blog.csdn.net/qq_22701869/article/details/79537971
1.创建一个maven的java工程
2.在pom文件内导入下面的包
<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"> <modelVersion>4.0.0</modelVersion> <groupId>com.xfma</groupId> <artifactId>acticvity</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!-- <activiti.version>5.15.1</activiti.version> --> <activiti.version>5.22.0</activiti.version> <mysql.version>5.1.46</mysql.version> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-model</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-bpmn-converter</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-process-validation</artifactId> <version>${activiti.version}</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.0.2</version> </dependency> </dependencies> </project>
3.在resources下面创建activiti.cfg.xml文件
<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:mysql://localhost:3306/activity" /> <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>
是通过spring初始化创建流程引擎
4.加入日志文件 在resources下
log4j.rootLogger = debug,stdout,D,E log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = C://logs/log.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File =C://logs/error.log log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
接下来就可以通过junit创建测试类进行测试了。
在resources下面创建helloworld.bpmn文件,然后到eclipse的设置内设置保存bpmn文件自动生成图片
package acticvity; import java.util.List; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.TaskService; import org.activiti.engine.repository.Deployment; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; public class ActivitiTest { /** * 发布流程 */ @Test public void deployProcess() { RepositoryService repositoryService = ProcessEngines.getDefaultProcessEngine() .getRepositoryService(); Deployment deployment = repositoryService.createDeployment().name("Hello流程名字") .addClasspathResource("helloworld.bpmn") .addClasspathResource("helloworld.png") .deploy(); System.out.println(deployment.getId() + " " + deployment.getName()); } /** * 启动任务 */ @Test public void startProcess() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("helloWorld"); System.out.println(processInstance.getId() + " " + processInstance.getActivityId()); } /** * 查询我的任务 */ @Test public void queryMyTask() { String assignee = "张三"; ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); List<Task> list = processEngine.getTaskService().createTaskQuery()//创建任务查询对象 .taskAssignee(assignee)//指定个人任务办理人 .list(); if (list != null && list.size() > 0) { for (Task task : list) { System.out.println(task.getAssignee() +" "+ task.getId() +" "+ task.getName()); } } } /** * 完成任务 */ @Test public void completeTask() { String taskId = "104"; ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); taskService.complete(taskId); System.out.println("任务已完成"); System.out.println("######################"); } }