1. 流程变量的含义: 我们可以在业务系统和流程系统当中,通过流程变量来制定对应的分支,如:员工请假 2. 流程变量的数据类型: string,double,boolean,short,long,Integer,binary,date日期 3. 流程变量的作用域 1.默认情况下,流程变量存在整个流程实例当中,每一个环节都可以获取到该变量,称之为global,类似于Java全局变量 4.流程变量案例:请假天数<=3那么部门经理审批后交给人事,如果>3那么部门经理审批结束后交给总经理审批,总经理结束后交给任务 4.1 定义流程图,在流程线上设置流程变量的条件
4.2 定义POJO,要去实现serializable接口
/** * 业务实体 */ public class Holiday implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String holidayName; //申请人 private Date startDate; //开始时间 private Date endDate; //结束时间 private Integer num; //请假天数 private String reason; //请假事由 private String type; //请假类型 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getHolidayName() { return holidayName; } public void setHolidayName(String holidayName) { this.holidayName = holidayName; } public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } public Date getEndDate() { return endDate; } public void setEndDate(Date endDate) { this.endDate = endDate; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
/** * 流程部署 */ @Test public void deployment() { //获取ProcessEngine对象 默认配置文件名称:activiti.cfg.xml 并且configuration的Bean实例ID为processEngineConfiguration ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取RepositoryService对象进行流程部署 RepositoryService repositoryService = processEngine.getRepositoryService(); //进行部署,将对应的流程定义文件生成到数据库当中,作为记录进行保存 Deployment deployment = repositoryService.createDeployment() .addClasspathResource("holiday/holiday2.bpmn") //加载流程文件 .addClasspathResource("holiday/holiday2.png") .name("请假流程-流程变量") //设置流程名称 .deploy(); //部署 //输出部署信息 System.out.println("流程名称:" deployment.getName()); System.out.println("流程ID:" deployment.getId()); System.out.println("流程Key:" deployment.getKey()); }
/** * 启动流程实例 */ @Test public void startInstance() { //获取ProcessEngine对象 默认配置文件名称:activiti.cfg.xml 并且configuration的Bean实例ID为processEngineConfiguration ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取RuntimeService RuntimeService runtimeService = processEngine.getRuntimeService(); //设置流程变量 Holiday holiday = new Holiday(); holiday.setNum(3); //定义一个Map集合,存放流程变量的值 Map<String, Object> assMap = new HashMap<>(); assMap.put("holiday", holiday); //启动流程 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holiday2"); System.out.println("流程实例启动成功~"); }
小于三天lisi执行完后直接到zhaoliu代办
如果请假4天,则需要流转到wangwu审批通过后才能到zhaoliu
5. 任务执行时,设置流程变量/** * 任务执行时,设置流程变量 */ @Test public void runTask() { //获取ProcessEngine对象 默认配置文件名称:activiti.cfg.xml 并且configuration的Bean实例ID为processEngineConfiguration ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取一个TaskService对象 TaskService taskService = processEngine.getTaskService(); //查询代办业务 createTaskQuery查询任务 taskCandidateOrAssigned查询任务执行者 processDefinitionKey:查询流程 /** * taskCandidateOrAssigned匹配规则:1.Assigned 2.配置bpmn文件中定义的值 * taskAssignee匹配规则:1.Assigned */ /*List<Task> list = taskService.createTaskQuery().taskCandidateOrAssigned("lisi").processDefinitionKey("holiday").list();*/ List<Task> list = taskService.createTaskQuery().taskAssignee("lisi").processDefinitionKey("holiday2").list(); //分页:List<Task> list = taskService.createTaskQuery().taskAssignee("zhangsan").processDefinitionKey("holiday").listPage(i,j); for (Task task : list) { System.out.println("任务名称:" task.getName()); System.out.println("任务执行人:" task.getAssignee()); System.out.println("任务ID:" task.getId()); //设置流程变量 Holiday holiday = new Holiday(); holiday.setNum(5); //定义一个Map集合,存放流程变量的值 Map<String, Object> assMap = new HashMap<>(); assMap.put("holiday", holiday); //处理任务 taskService.complete(task.getId(),assMap); System.out.println("流程实例ID:" task.getProcessInstanceId()); } }
6. 通过流程实例ID设置流程变量的值执行lisi分支任务之前,执行此方法设置值 /** * 获取到流程实例ID,跟据流程实例设置流程变量的值 */ @Test public void getInstanceVar(){ //获取ProcessEngine对象 默认配置文件名称:activiti.cfg.xml 并且configuration的Bean实例ID为processEngineConfiguration ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取RuntimeService RuntimeService runtimeService = processEngine.getRuntimeService(); //设置流程变量 Holiday holiday = new Holiday(); holiday.setNum(3); //设置流程变量 runtimeService.setVariable("25001","holiday",holiday); }
7. 根据任务ID,设置流程变量/** * 根据任务ID,设置流程变量 */ @Test public void runTask() { //获取ProcessEngine对象 默认配置文件名称:activiti.cfg.xml 并且configuration的Bean实例ID为processEngineConfiguration ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //获取一个TaskService对象 TaskService taskService = processEngine.getTaskService(); //查询代办业务 createTaskQuery查询任务 taskCandidateOrAssigned查询任务执行者 processDefinitionKey:查询流程 /** * taskCandidateOrAssigned匹配规则:1.Assigned 2.配置bpmn文件中定义的值 * taskAssignee匹配规则:1.Assigned */ /*List<Task> list = taskService.createTaskQuery().taskCandidateOrAssigned("lisi").processDefinitionKey("holiday").list();*/ List<Task> list = taskService.createTaskQuery().taskAssignee("lisi").processDefinitionKey("holiday2").list(); //分页:List<Task> list = taskService.createTaskQuery().taskAssignee("zhangsan").processDefinitionKey("holiday").listPage(i,j); for (Task task : list) { System.out.println("任务名称:" task.getName()); System.out.println("任务执行人:" task.getAssignee()); System.out.println("任务ID:" task.getId()); //设置流程变量 Holiday holiday = new Holiday(); holiday.setNum(5); taskService.setVariable(task.getId(),"holiday",holiday); //处理任务 taskService.complete(task.getId()); System.out.println("流程实例ID:" task.getProcessInstanceId()); } }
|
|