获取默认流程引擎,会自动读取activiti.cfg.xml文件
private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
部署流程定义
方式一:
@Test
public void deploy() {
Deployment deployent = processEngine.getRepositoryService() //获取部署相关service
.createDeployment() //创建部署
.addClasspathResource("diagrams/HelloWorld.bpmn") //加载资源文件
.addClasspathResource("diagrams/HelloWorld.png") //加载资源文件
.name("HelloWorld流程") //流程名称
.deploy(); //部署
System.out.println("流程部署ID:"+deployent.getId());
System.out.println("流程部署Name:"+deployent.getName());
}
方式二:
@Test
public void deployWithZip() {
InputStream inputStream = this.getClass() //获取当前class对象
.getClassLoader() //获取类加载器
.getResourceAsStream("diagrams/HelloWorld.zip"); //获取指定文件流
ZipInputStream zipInputStream = new ZipInputStream(inputStream); //实例化输入流
Deployment deployent = processEngine.getRepositoryService() //获取部署相关service
.createDeployment() //创建部署
.addZipInputStream(zipInputStream) //添加zip输入流
.name("HelloWorld教程") //流程名称
.deploy(); //部署
System.out.println("流程部署ID:"+deployent.getId());
System.out.println("流程部署Name:"+deployent.getName());
}
自动流程实例
@Test
public void start() {
ProcessInstance pi = processEngine.getRuntimeService() //运行时service
.startProcessInstanceByKey("myFirstProcess"); //流程定义表的key字段值
System.out.println("流程实例ID:"+pi.getId());
System.out.println("流程定义ID:"+pi.getProcessDefinitionId());
}
查看任务
@Test
public void findTask() {
List<Task> taskList = processEngine.getTaskService() //任务相关service
.createTaskQuery() //创建任务查询
.taskAssignee("java1234_小峰") //指定某个人
.list();
for(Task task:taskList) {
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());
}
}
完成任务
@Test
public void completeTask() {
processEngine.getTaskService() //任务相关service
.complete("2504");
}
查询流程定义 返回流程定义集合 对应表act_re_procdef
@Test
public void list() {
String processDefinitionKey = "myFirstProcess";
List<ProcessDefinition> pdList = processEngine.getRepositoryService() //获取service
.createProcessDefinitionQuery() //创建流程定义查询
.processDefinitionKey(processDefinitionKey ) //通过key查询
.list(); //返回一个集合
for(ProcessDefinition pd:pdList) {
System.out.println("ID:"+pd.getId());
System.out.println("NAME_:"+pd.getName());
System.out.println("KEY_:"+pd.getKey());
System.out.println("VERSION_:"+pd.getVersion());
System.out.println("=============================");
}
}
通过ID查询某个流程定义
@Test
public void getById() {
String processDefinitionId = "myFirstProcess:2:7504";
ProcessDefinition pd = processEngine.getRepositoryService() //获取service
.createProcessDefinitionQuery() //创建流程定义查询
.processDefinitionId(processDefinitionId ) //通过ID查询
.singleResult(); //返回一个结果
System.out.println("ID:"+pd.getId());
System.out.println("NAME_:"+pd.getName());
System.out.println("KEY_:"+pd.getKey());
System.out.println("VERSION_:"+pd.getVersion());
}
根据流程部署ID和资源文件名称来查询图片
@Test
public void getImageById()throws Exception{
InputStream inputStream = processEngine.getRepositoryService() //获取service
.getResourceAsStream("10001", "HelloWorld/HelloWorld.png");
// FileUtils.copyInputStreamToFile(inputStream, new File("C:/Users/YHH/Desktop/helloWorld.png"));
FileUtils.copyInputStreamToFile(inputStream, new File("C:\\Users\\YHH\\Desktop\\helloWorld.png"));
}
查询最新版本流程定义
@Test
public void listLastVersion()throws Exception{
List<ProcessDefinition> listAll = processEngine.getRepositoryService() //获取service
.createProcessDefinitionQuery() //创建流程定义查询
.orderByProcessDefinitionVersion().asc() //根据流程定义版本升序
.list(); //返回一个集合
/**
* 定义有序map,相同的key,加入添加相同的map值 后者的值会覆盖前面相同的key的值
*/
Map<String,ProcessDefinition> map = new LinkedHashMap<String,ProcessDefinition>();
//遍历集合 根据key来覆盖前面的值 来保证最新的key覆盖前面所有老的key的值
for(ProcessDefinition pd:listAll) {
map.put(pd.getKey(), pd);
}
List<ProcessDefinition> pdList = new LinkedList<ProcessDefinition>(map.values());
for(ProcessDefinition pd:pdList) {
System.out.println("ID:"+pd.getId());
System.out.println("NAME_:"+pd.getName());
System.out.println("KEY_:"+pd.getKey());
System.out.println("VERSION_:"+pd.getVersion());
System.out.println("=============================");
}
删除所有key相同的流程实例
@Test
public void deleteByKey()throws Exception{
String processDefinitionKey = "helloWorld2";
List<ProcessDefinition> pdList = processEngine.getRepositoryService() //获取service
.createProcessDefinitionQuery() //创建流程定义查询
.processDefinitionKey(processDefinitionKey ) //根据key值查询
.list(); //返回一个集合
for(ProcessDefinition pd:pdList) {
processEngine.getRepositoryService()
.deleteDeployment(pd.getDeploymentId(),true);
}
}
与上图相比 KEY_ 为helloWorld2的已经被删除