目录
部署
1.1流程定义部署
部署流程定义就是要将绘制的图形即流程定义(
.bpmn
)部署在工作流程引擎
activiti
中方法如下
// 获取repositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
//部署对象
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("myholiday.bpmn")// bpmn文 件
.addClasspathResource("myholiday.png")// 图片文件
.name("请假申请流程")
.deploy();
System.out.println("流程部署id:" + deployment.getId());
System.out.println("流程部署名称:" + deployment.getName());
1.2流程定义单个文件部署
@Test
public void deployProcess() {
// 获取repositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
// bpmn输入流
InputStream inputStream_bpmn=
this.getClass().getClassLoader().getResourceAsStream("diagram/holiday.bpmn");
// 图片输入流
InputStream inputStream_png = this.getClass().getClassLoader().getResourceAsStream("diagram/holiday.png");
// 流程部署对象
Deployment deployment = repositoryService.createDeployment()
.addInputStream("holiday.bpmn", inputStream_bpmn)
.addInputStream("holiday.png", inputStream_png)
.deploy();
System.out.println("流程部署id:" + deployment.getId());
System.out.println("流程部署名称:" + deployment.getName());
}
1.3压缩包部署方式
@Test
public void deployProcessByZip() {
// 定义zip输入流
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("diagram/holiday.zip");
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
// 获取repositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
// 流程部署
Deployment deployment = repositoryService.createDeployment().addZipInputStream(zipInputStream)
.deploy();
System.out.println("流程部署id:" + deployment.getId());
System.out.println("流程部署名称:" + deployment.getName());
}
流程定义部署后act_re_deployment ,act_re_procdef ,act_ge_bytearray 之间关系
流程定义部署后操作 activiti 数据表如下:
SELECT * FROM act_re_deployment #流程定义部署表,记录流程部署信息
SELECT * FROM act_re_procdef #流程定义表,记录流程定义信息
SELECT * FROM act_ge_bytearray #资源表(bpmn,png)
说明:
act_re_deployment 和 act_re_procdef 一对多关系,一次部署在流程部署表生成一条记录,但一次部署
可以部署多个流程定义,每个流程定义在流程定义表生成一条记录。每一个流程定义在
act_ge_bytearray 会存在两个资源记录,bpmn 和 png。
建议:一次部署一个流程,这样部署表和流程定义表是一对一有关系,方便读取流程部署及流程定
义信息。
启动
流程实例启动比如部署系统请假流程后,如果某用户要申请请假这时就需要执行这个流程,如果 另外一个用户也要申请请假则也需要执行该流程,每个执行互不影响,每个执行是单独的流程实例。
@Test
public void startProcessInstance() {
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取RunTimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
// 根据流程定义key启动流程
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myholiday01");
System.out.println(" 流程定义 id : " + processInstance.getProcessDefinitionId());
System.out.println("流程实例id:" + processInstance.getId());
System.out.println(" 当前活动 Id : " + processInstance.getActivityId());
}
1.1启动时与业务流程关联
//流程实例的启动
@Test
public void testprocessstart(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//流程定义激活即有效
RepositoryService repositoryService = processEngine.getRepositoryService();
repositoryService.activateProcessDefinitionByKey("myProcess");
//流程定义终止即无效
//repositoryService.suspendProcessDefinitionByKey("myProcess");
//创建流程启动对象
RuntimeService runtimeService = processEngine.getRuntimeService();
String processDefinitionKey = "myProcess";
String businessKey = "1";
// 根据流程定义的key启动一个流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey);
System.out.println("活动id"+ processInstance.getActivityId());
System.out.println("实例id"+ processInstance.getProcessInstanceId());
}
启动流程实例时,指定的
businesskey
,就会在
act_ru_execution #
流程实例的执行表中存储
businesskey
。
Businesskey
:业务标识,通常为业务表的主键,业务标识和流程实例一一对应
比如:请假流程启动一个流程实例,就可以将请假单的
id
作为业务标识存储到
activiti
中,将来查询 activiti 的流程实例信息就可以获取请假单的
id
从而关联查询业务系统数据库得到请假单信息。
1.2启动时设置流程变量
//流程实例的启动
@Test
public void testprocessstart(){
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//创建流程启动对象
RuntimeService runtimeService = processEngine.getRuntimeService();
String processDefinitionKey = "myProcess";
String businessKey = "1";
//方式一:根据流程定义的key,启动某一个流程实例
//ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
// 方式二:根据流程定义的key启动一个流程实例
//ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey);
//设置流程变量
//在启动流程实例时设置流程变量
//定义流程变量为每个流程中的每个环节分配负责人
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("dept_assign", "张三");
variables.put("leader_assign", "李四");
//设置流程变量assignee
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,businessKey, variables);
System.out.println("活动id"+ processInstance.getActivityId());
System.out.println("实例id"+ processInstance.getProcessInstanceId());
}
variables);
2.查询
2.1流程定义查询
@Test
public void queryProceccDefinition() {
// 流程定义key
String processDefinitionKey = "holiday";
// 获取repositoryService
RepositoryService repositoryService = processEngine.getRepositoryService();
// 查询流程定义对象
ProcessDefinitionQuery processDefinitionQuery =repositoryService.createProcessDefinitionQuery();
//遍历查询结果
List<ProcessDefinition> list = processDefinitionQuery
.processDefinitionKey(processDefinitionKey)
.orderByProcessDefinitionVersion().desc().list();
for (ProcessDefinition processDefinition : list) {
System.out.println("------------------------");
System.out.println(" 流 程 部 署 id :"+processDefinition.getDeploymentId());
System.out.println("流程定义id:" + processDefinition.getId());
System.out.println("流程定义名称" +processDefinition.getName());
System.out.println("流程定义key:" + processDefinition.getKey());
System.out.println("流程定义版本:" + processDefinition.getVersion());
}
}
2.2 流程实例查询
查询
流程实例:
流程在运行过程中可以查询流程实例的状态,
当前运行结点
等信息。
@Test
public void queryProcessInstance() {
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 流程定义key
String processDefinitionKey = "myProcess";
// 获取RunTimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
List<ProcessInstance> list = runtimeService .createProcessInstanceQuery()
.processDefinitionKey(processDefinitionKey)//
.list();
for (ProcessInstance processInstance : list) {
System.out.println("----------------------------");
System.out.println("流程实例id:"+ processInstance.getProcessInstanceId());
System.out.println("所属流程定义id:"+processInstance.getProcessDefinitionId());
System.out.println("流程实例名称:"+processInstance.getName());
System.out.println("是否执行完成:" +processInstance.isEnded());
System.out.println("是否暂停:" + processInstance.isSuspended());
System.out.println(" 当 前 活 动 标 识 : " +processInstance.getActivityId());
}
}
2.3流程资源查询
// 获取流程定义图片资源
@Test
public void getProcessResources() throws IOException {
//流程部署id
String deploymentId = "9001";
// 通过流程引擎获取repositoryService
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
//读取资源名称
List<String> resources = repositoryService.getDeploymentResourceNames(deploymentId);
String resource_image = null;
//获取图片
for(String resource_name :resources){
if(resource_name.indexOf(".png")>=0){
resource_image = resource_name;
} }
//图片输入流
InputStream inputStream = repositoryService.getResourceAsStream(deploymentId, resource_image);
File exportFile = new File("d:/holiday.png");
FileOutputStream fileOutputStream = new FileOutputStream(exportFile);
byte[] buffer = new byte[1024];
int len = -1;
//输出图片
while((len = inputStream.read(buffer))!=-1){
fileOutputStream.write(buffer, 0, len);
}
inputStream.close();
fileOutputStream.close();
}
说明:
- 1) deploymentId 为流程部署 ID
- 2) resource_name 为 act_ge_bytearray 表中 NAME_列的值
- 3) 使用 repositoryService 的 getDeploymentResourceNames方法可以获取指定部署下得所有文件的名 称
- 4) 使用 repositoryService 的 getResourceAsStream 方法传入部署 ID和资源图片名称可以获取部署下 指定名称文件的输入流
- 5) 最后的将输入流中的图片资源进行输出。
2.4.流程资源文件输出
使用common-io 进行输入流和输出流的转化
//资源文件的输出
@Test
public void exprotResourcefile(){
//获取流程引擎(创建25张表)
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取存储资源服务
RepositoryService repositoryService = processEngine.getRepositoryService();
//获取查询对象
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
//设置查询条件
processDefinitionQuery.processDefinitionKey("myProcess");
//获取流程定义对象
ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
//流程部署id
String deploymentId =processDefinition.getDeploymentId();
//获取png资源的名称processDefinition.getDiagramResourceName()
//获取bpmn资源的名称processDefinition.getResourceName()
InputStream pnginputstream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());
//bpmn资源输入流
InputStream bpmninputstream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
//构建输出流
OutputStream pngoutputstream = null;
OutputStream bpmnoutputstream = null;
//通过commonsio进行输入流,输出流转化
try {
pngoutputstream = new FileOutputStream("E:\\levelpro\\"+processDefinition.getDiagramResourceName());
bpmnoutputstream= new FileOutputStream("E:\\levelpro\\"+processDefinition.getResourceName());
IOUtils.copy(bpmninputstream, bpmnoutputstream);
IOUtils.copy(pnginputstream, pngoutputstream);
bpmnoutputstream.close();
pngoutputstream.close();
pnginputstream.close();
bpmninputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2.5 历史流程的查询
历史流程活动查询
@Test
public void historyProcessActivitiQuery() {
//获取流程引擎(创建25张表)
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取历史服务historyService
HistoryService historyService = processEngine.getHistoryService();
//查询流程活动实例
HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
//设置
historicActivityInstanceQuery.processInstanceId("2501");
List<HistoricActivityInstance> list = historicActivityInstanceQuery.list();
for (HistoricActivityInstance historicActivityInstance : list) {
System.out.println("活动id:"+historicActivityInstance.getActivityId());
System.out.println("活动名称:"+historicActivityInstance.getActivityName());
System.out.println("流程定义id:"+historicActivityInstance.getProcessDefinitionId());
System.out.println("流程实例id:"+historicActivityInstance.getProcessInstanceId());
System.out.println("=======================================");
}
}
历史流程任务实例查询
@Test
public void historyProcesstaskQuery() {
//获取流程引擎(创建25张表)
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取历史服务historyService
HistoryService historyService = processEngine.getHistoryService();
//查询流程活动实例
HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery();
//设置
historicTaskInstanceQuery.processInstanceId("2501");
List<HistoricTaskInstance> list = historicTaskInstanceQuery.list();
for (HistoricTaskInstance historicTaskInstance : list) {
System.out.println("历史任务开始时间:"+historicTaskInstance.getStartTime());
System.out.println("历史任务结束时间:"+historicTaskInstance.getEndTime());
System.out.println("流程定义id:"+historicTaskInstance.getProcessDefinitionId());
System.out.println("流程实例id:"+historicTaskInstance.getProcessInstanceId());
System.out.println("=======================================");
}
}
3.流程定义删除
@Test
public void deleteDeployment() {
// 流程部署id
String deploymentId = "8801";
// 通过流程引擎获取repositoryService
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
//删除流程定义,如果该流程定义已有流程实例启动则删除时出错
repositoryService.deleteDeployment(deploymentId);
//设置true 级联删除流程定义,即使该流程有流程实例启动也可以删除,设
//置为false非级别删除方式,如果流程
//repositoryService.deleteDeployment(deploymentId, true);
}
4.挂起,激活
- 挂起流程实例:将当前运行的流程暂停而不是直接删除,流程暂停后将不会继续执 行。
- 流程定义为挂起状态该流程定义将不允许启动新的流程实例,同时该流程定义下所有的流程实例将 全部挂起暂停执行。
4.1全部流程实例挂起与激活
// 挂起激活流程定义
@Test
public void suspendOrActivateProcessDefinition() {
//获取流程引擎
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 流程定义id
String processDefinitionId = "";
RepositoryService repositoryService = processEngine.getRepositoryService();
// 获得流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(processDefinitionId).singleResult();
//是否暂停
boolean suspend = processDefinition.isSuspended();
if(suspend){
//如果暂停则激活,这里将流程定义下的所有流程实例全部激活
repositoryService.activateProcessDefinitionById(processDefinitionId,true, null);
System.out.println("流程定义:"+processDefinitionId+"激活");
}else{
//如果激活则挂起,这里将流程定义下的所有流程实例全部挂起
repositoryService.suspendProcessDefinitionById(processDefinitionId, true, null);
System.out.println("流程定义:"+processDefinitionId+"挂起");
}
}
注意:全部流程挂起和激活与该服务RepositoryService repositoryService = processEngine.getRepositoryService()相关
4.2 单个流程实例挂起与激活
操作流程实例对象,针对单个流程执行挂起操作,某个流程实例挂起则此流程不再继续执行,完成 该流程实例的当前任务将报异常。
@Test
public void suspendOrActiveProcessInstance() {
//获取流程引擎(创建25张表)
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 流程实例id
String processInstanceId = "2501";
// 获取RunTimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
//根据流程实例id查询流程实例
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
boolean suspend = processInstance.isSuspended();
if(suspend){
//如果暂停则激活
runtimeService.activateProcessInstanceById(processInstanceId);
System.out.println("流程实例:"+processInstanceId+"激活");
}else{
//如果激活则挂起
runtimeService.suspendProcessInstanceById(processInstanceId);
System.out.println("流程实例:"+processInstanceId+"挂起");
}
}
注意:单个流程挂起和激活,该服务RuntimeService runtimeService = processEngine.getRuntimeService();相关