工作流第一个demo
原创
©著作权归作者所有:来自51CTO博客作者it999977的原创作品,请联系作者获取转载授权,否则将追究法律责任
maven
<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.lzh</groupId>
<artifactId>activitiDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<activiti-version>5.18.0</activiti-version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.62</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti-version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti-version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.168</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
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:mysql://localhost:3306/activiti" />
<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>
package cn.itcast.a_helloworld;
import java.util.List;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
public class HelloWorld {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**部署流程定义*/
@Test
public void deploymentProcessDefinition(){
Deployment deployment = processEngine.getRepositoryService()
.createDeployment()
.name("helloworld入门程序")
.addClasspathResource("diagrams/helloworld.bpmn")
.addClasspathResource("diagrams/helloworld.png")
.deploy();
System.out.println("部署ID:"+deployment.getId());
System.out.println("部署名称:"+deployment.getName());
}
/**启动流程实例*/
@Test
public void startProcessInstance(){
String processDefinitionKey = "helloworld";
ProcessInstance pi = processEngine.getRuntimeService()
.startProcessInstanceByKey(processDefinitionKey);
System.out.println("流程实例ID:"+pi.getId());
System.out.println("流程定义ID:"+pi.getProcessDefinitionId());
}
/**查询当前人的个人任务*/
@Test
public void findMyPersonalTask(){
String assignee = "王五";
List<Task> list = processEngine.getTaskService()
.createTaskQuery()
.taskAssignee(assignee)
.list();
if(list!=null && list.size()>0){
for(Task task:list){
System.out.println("任务ID:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务的创建时间:"+task.getCreateTime());
System.out.println("任务的办理人:"+task.getAssignee());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("########################################################");
}
}
}
/**完成我的任务*/
@Test
public void completeMyPersonalTask(){
String taskId = "302";
processEngine.getTaskService()
.complete(taskId);
System.out.println("完成任务:任务ID:"+taskId);
}
@Test
public void showView() throws Exception {
String deploymentId = "122501";
List<String> names = processEngine.getRepositoryService()
.getDeploymentResourceNames(deploymentId);
String imageName = "";
for (String name : names) {
System.out.println("name:"+name);
if(name.indexOf(".png")>=0){
imageName = name;
}
}
System.out.println("imageName:"+imageName);
if(imageName!=null){
File f = new File("e:/"+ imageName);
InputStream in = processEngine.getRepositoryService()
.getResourceAsStream(deploymentId, imageName);
FileUtils.copyInputStreamToFile(in, f);
}
}
/**查看流程图2*/
@Test
public void getImgById(){
/**
* 思路:得到资源service 然后得到资源流,根据部署定义的deploymentId 和 资源名字来获取流,然后通过common-io第三方jar包来复制到指定目录
*/
RepositoryService rep = processEngine.getRepositoryService();
InputStream img = rep.getResourceAsStream("1", "diagrams/lzh.png");
try {
FileUtils.copyInputStreamToFile(img, new File("E://img/lzh.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**得到最新的发布版本*/
@Test
public void getListLastVersion(){
/**
* 思路:得到资源service 然后创建流程定义查询根据定义的版本升序排列得到list,然后new map 让key相同的覆盖之前的对象。得到最新的流程定义对象
*/
List<ProcessDefinition> list = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.orderByProcessDefinitionVersion().asc().list();
Map<String, ProcessDefinition> map=new LinkedHashMap<String, ProcessDefinition>();
for(ProcessDefinition pd:list){
map.put(pd.getKey(), pd);
}
Set<Entry<String, ProcessDefinition>> entrySet = map.entrySet();
for(Entry<String, ProcessDefinition> e:entrySet){
System.out.println(e.getKey()+"-->"+e.getValue().getVersion());
}
}
/**删除流程根据定义流程key,就是说这个定义的全部版本都删除*/
@Test
public void deleteProcessByKey(){
/**
* 思路:得到资源service 然后创建流程定义查询根据流程定义key得到list,遍历list,通过service删除定义
*/
List<ProcessDefinition> list = processEngine.getRepositoryService()
.createProcessDefinitionQuery()
.processDefinitionKey("myProcess_lzh2").list();
for(ProcessDefinition pd:list){
processEngine.getRepositoryService().deleteDeployment(pd.getDeploymentId(), true);
}
}
/**得到流程实例状态*/
@Test
public void getState(){
/**
* 思路:得到运行时service 然后创建流程实例查询根据流程实例id得到唯一值。判断是否正在执行
*/
ProcessInstance singleResult = processEngine.getRuntimeService()
.createProcessInstanceQuery().processInstanceId("27501")
.singleResult();
if(singleResult!=null){
System.out.println("该流程正在执行");
}else{
System.out.println("该流程已经执行完");
}
}
/**得到历史任务完成信息*/
@Test
public void getHistoryTaskList(){
/**
* 思路:得到运行时service 然后创建历史任务实例查询,通过实例id,查找已经完成的任务
*/
List<HistoricTaskInstance> list = processEngine.getHistoryService()
.createHistoricTaskInstanceQuery().processInstanceId("2501")
.finished().list();
for(HistoricTaskInstance h:list){
System.out.println(h.getId());
System.out.println(h.getAssignee());
System.out.println(h.getName());
System.out.println("-----------------");
}
}
/**得到历史活动信息*/
@Test
public void getHistoryActList(){
/**
* 思路:得到运行时service 然后创建历史活动实例查询,通过实例id,查找已经完成的历史活动
*/
List<HistoricActivityInstance> list = processEngine.getHistoryService()
.createHistoricActivityInstanceQuery().processInstanceId("27501").finished().list();
for(HistoricActivityInstance h:list){
System.out.println(h.getId());
System.out.println(h.getAssignee());
System.out.println(h.getActivityName());
System.out.println(h.getActivityId());
System.out.println(h.getActivityType());
System.out.println("-----------------");
}
}