目录
- 1.部署流程定义
- 2.查询部署列表
- 3.查询定义列表
- 4.删除部署信息
- 5.删除流程定义
- 6.查询部署对应的文件流
- 7.获取PNG文件输入流
- 8.启动流程定义2种方式
- 9.查询流程实例列表
- 10.结束流程实例
- 11.查询任务列表
- 12.查询任务列表
- 13.查询最新版本的流程定义列表
1.部署流程定义
/**
* 部署流程定义 方式一:读取单个的流程定义文件 方式二:读取zip压缩文件
*/
public void test1() {
DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
// 方式一:读取单个的流程定义文件
/*
deploymentBuilder.addClasspathResource("test1.bpmn");
deploymentBuilder.addClasspathResource("test1.png");
Deployment deployment = deploymentBuilder.deploy();
*/
// 方式二:读取zip压缩文件
ZipInputStream zipInputStream = new ZipInputStream(this.getClass().getClassLoader().getResourceAsStream("process.zip"));
deploymentBuilder.addZipInputStream(zipInputStream);
deploymentBuilder.name("请假流程部署");
Deployment deployment = deploymentBuilder.deploy();
}
2.查询部署列表
/**
* 查询部署列表
*/
public void test2() {
// 部署查询对象,查询表act_re_deployment
DeploymentQuery query = processEngine.getRepositoryService().createDeploymentQuery();
List<Deployment> list = query.list();
for (Deployment deployment : list) {
String id = deployment.getId();
String name = deployment.getName();
System.out.println(id+"---"+name);
}
}
3.查询定义列表
/**
* 查询流程定义列表
*/
public void test3() {
// 流程定义查询对象,查询表act_re_procdef
ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
List<ProcessDefinition> list = query.list();
for (ProcessDefinition pd : list) {
String deploymentId = pd.getDeploymentId();
System.out.println(pd.getName() + "" + pd.getId()+"部署ID:"+deploymentId);
}
}
4.删除部署信息
/**
* 删除部署信息
*/
public void test4() {
String deploymentId = "1";
// processEngine.getRepositoryService().deleteDeployment(deploymentId);
processEngine.getRepositoryService().deleteDeployment(deploymentId, true); //级联删除
}
5.删除流程定义
/**
* 删除流程定义(通过删除部署信息达到删除流程定义的目的)
*/
public void test5() {
String deploymentId = "22501";
// processEngine.getRepositoryService().deleteDeployment(deploymentId);
processEngine.getRepositoryService().deleteDeployment(deploymentId,true);
}
6.查询部署对应的文件流
/**
* 查询一次部署对应的流程定义文件名称和对应的输入流(bpmn png)
*/
public void test6() throws Exception {
String deploymentId = "20001";
List<String> names = processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
for (String name : names) {
System.out.println(name);
InputStream in = processEngine.getRepositoryService().getResourceAsStream(deploymentId, name);
// 将文件保存到本地磁盘
File file = new File("d:\\" + name);
File f1=new File( file.getParent());
if(f1.exists()==false){
f1.mkdirs();
}
OutputStream out = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.close();
in.close();
}
}
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
/**
* 查询一次部署对应的流程定义文件名称和对应的输入流(bpmn png)
*/
public void test6_1() throws Exception {
String deploymentId = "20001";
List<String> names = processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
for (String name : names) {
System.out.println(name);
InputStream in = processEngine.getRepositoryService().getResourceAsStream(deploymentId, name);
// 将文件保存到本地磁盘
File file = new File("d:\\" + name);
File f1=new File( file.getParent());
if(f1.exists()==false){
f1.mkdirs();
}
FileUtils.copyInputStreamToFile(in, new File("d:\\" + name));
in.close();
}
}
7.获取PNG文件输入流
/**
* 获得png文件的输入流
*/
@Test
public void test7() throws Exception {
String processDefinitionId = "HelloWorldKsy:2:2504";
InputStream pngInputStream = processEngine.getRepositoryService().getProcessDiagram(processDefinitionId);
FileUtils.copyInputStreamToFile(pngInputStream, new File("d:\\my.png"));
}
8.启动流程定义2种方式
/**
* 启动流程实例 方式一:根据流程定义的id启动 方式二:根据流程定义的key启动(自动选择最新版本的流程定义启动流程实例)
*/
public void test8() {
/*
* String processDefinitionId = "HelloWorldKsy:2:2504"; ProcessInstance
* processInstance =
* processEngine.getRuntimeService().startProcessInstanceById
* (processDefinitionId ); System.out.println(processInstance.getId());
*/
String processDefinitionKey = "HelloWorldKsy";
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey);
System.out.println(processInstance.getId());
}
根据Key启动流程,会使用最新的的流程定义的Key。选择最新的流程定义进行启动!
9.查询流程实例列表
/**
* 查询流程实例列表,查询act_ru_execution表
*/
public void test9(){
//流程实例查询对象,查询act_ru_execution表
ProcessInstanceQuery query = processEngine.getRuntimeService().createProcessInstanceQuery();
query.processDefinitionKey("HelloWorldKsy");
query.orderByProcessInstanceId().desc();
query.listPage(0, 2);
List<ProcessInstance> list = query.list();
for (ProcessInstance pi : list) {
System.out.println(pi.getId() + " " + pi.getActivityId());
}
}
10.结束流程实例
/**
* 结束流程实例,操作的表act_ru_execution act_ru_task
*/
public void test10(){
String processInstanceId = "10001";
processEngine.getRuntimeService().deleteProcessInstance(processInstanceId , "我愿意");
}
11.查询任务列表
/**
* 查询任务列表
*/
public void test11() {
// 任务查询对象,查询act_ru_task表
TaskQuery query = processEngine.getTaskService().createTaskQuery();
String assignee = "张三";
query.taskAssignee(assignee);
query.orderByTaskCreateTime().desc();
List<Task> list = query.list();
for (Task task : list) {
System.out.println(task.getId());
}
}
12.查询任务列表
/**
* 办理任务
*/
public void test12(){
String taskId = "32504";
processEngine.getTaskService().complete(taskId);
}
13.查询最新版本的流程定义列表
/**
* 查询最新版本的流程定义列表
*/
@Test
public void test14() {
ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
query.orderByProcessDefinitionVersion().asc();
List<ProcessDefinition> list = query.list();
Map<String, ProcessDefinition> map = new HashMap<String, ProcessDefinition>();
for (ProcessDefinition pd : list) {
map.put(pd.getKey(), pd);
}
ArrayList<ProcessDefinition> lastList = new ArrayList<>(map.values());
for (ProcessDefinition processDefinition : lastList) {
System.out.println(processDefinition.getName() + " " + processDefinition.getVersion());
}
}