1、设置并获取基本类型的变量
package cn.et.db;
import java.util.Date;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
public class Variable {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**
* 部署流程定义
*/
@Test
public void deploymentProcessDefinition(){
//流程定义和部署对象相关的service
RepositoryService repositoryService = processEngine.getRepositoryService();
//创建一个部署对象
DeploymentBuilder createDeployment = repositoryService.createDeployment();
createDeployment.name("hello activiti");
//从classpath下加载资源,一次只能加载一个文件
DeploymentBuilder addClasspathResource = createDeployment.addClasspathResource("diagrams/variable.bpmn").addClasspathResource("diagrams/variable.png");
//完成部署
Deployment deploy = addClasspathResource.deploy();
System.out.println("部署id"+deploy.getId());
System.out.println("部署名称"+deploy.getName());
}
/**
* 启动流程实例
*/
@Test
public void startProcessInstance(){
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance startProcessInstanceByKey = runtimeService.startProcessInstanceByKey("variable");
System.out.println("流程实例id"+startProcessInstanceByKey.getId());
System.out.println("流程定义id"+startProcessInstanceByKey.getProcessDefinitionId());
}
/**
* 设置变量
*/
@Test
public void setVariable(){
//设置任务流程的变量
TaskService taskService = processEngine.getTaskService();
taskService.setVariable("27504", "请假天数", 1);
taskService.setVariable("27504","请假理由","病假");
taskService.setVariable("27504","请假时间",new Date());
System.out.println("ok");
}
/**
* 获取变量
*/
@Test
public void getVariable(){
TaskService taskService = processEngine.getTaskService();
Object variable = taskService.getVariable("27504", "请假天数");
Object variable2 = taskService.getVariable("27504", "请假理由");
Object variable3 = taskService.getVariable("27504", "请假时间");
System.out.println(variable+" "+variable2+" "+variable3);
}
}
2、设置并获取javaBean类型
javaBean(javaBean必须序列化 )
package cn.et.db;
import java.io.Serializable;
public class Person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package cn.et.db;
import java.util.Date;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
public class Variable {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**
* 部署流程定义
*/
@Test
public void deploymentProcessDefinition(){
//流程定义和部署对象相关的service
RepositoryService repositoryService = processEngine.getRepositoryService();
//创建一个部署对象
DeploymentBuilder createDeployment = repositoryService.createDeployment();
createDeployment.name("hello activiti");
//从classpath下加载资源,一次只能加载一个文件
DeploymentBuilder addClasspathResource = createDeployment.addClasspathResource("diagrams/variable.bpmn").addClasspathResource("diagrams/variable.png");
//完成部署
Deployment deploy = addClasspathResource.deploy();
System.out.println("部署id"+deploy.getId());
System.out.println("部署名称"+deploy.getName());
}
/**
* 启动流程实例
*/
@Test
public void startProcessInstance(){
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance startProcessInstanceByKey = runtimeService.startProcessInstanceByKey("variable");
System.out.println("流程实例id"+startProcessInstanceByKey.getId());
System.out.println("流程定义id"+startProcessInstanceByKey.getProcessDefinitionId());
}
/**
* 使用javabean设置变量
*/
@Test
public void setJavaBeanVariable(){
TaskService taskService = processEngine.getTaskService();
Person p=new Person();
p.setId(1);
p.setName("王小二");
taskService.setVariable("40004", "人员信息", p);
System.out.println("ok");
}
/**
* 获取javaBean
*/
@Test
public void getJavaBeanVariable(){
TaskService taskService = processEngine.getTaskService();
Person variable = (Person)taskService.getVariable("40004", "人员信息");
System.out.println(variable.getId()+"========="+variable.getName());
}
}
SELECT * FROM `act_ru_variable` #正在执行的流程变量
SELECT * FROM `act_hi_varinst` #历史的流程变量
3、历史查询(流程实例和历史活动)
package cn.et.db;
import java.util.List;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.junit.Test;
public class Isend {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**
* 查询历史流程实例
*/
@Test
public void historyProcessInstance(){
//与历史数据相关的service
HistoryService historyService = processEngine.getHistoryService();
//创建历史流程实例查询
HistoricProcessInstanceQuery createHistoricProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery();
//使用流程实例id查询
HistoricProcessInstanceQuery processInstanceId = createHistoricProcessInstanceQuery.processInstanceId("57501");
/*HistoricProcessInstanceQuery orderByProcessInstanceStartTime = processInstanceId.orderByProcessInstanceStartTime().asc();
HistoricProcessInstance singleResult = orderByProcessInstanceStartTime.singleResult();*/
HistoricProcessInstance singleResult = processInstanceId.singleResult();
System.out.println(singleResult.getId()+" "+singleResult.getStartTime()+" "+singleResult.getEndTime());
}
/**
* 查询历史活动
*/
@Test
public void historyActiviti(){
//与历史数据相关的service
HistoryService historyService = processEngine.getHistoryService();
//创建历史活动实例查询
HistoricActivityInstanceQuery createHistoricActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
//使用流程实例id查询
HistoricActivityInstanceQuery processInstanceId = createHistoricActivityInstanceQuery.processInstanceId("57501");
HistoricActivityInstanceQuery asc = processInstanceId.orderByHistoricActivityInstanceEndTime().asc();
List list = asc.list();
for(HistoricActivityInstance h:list){
System.out.println(h.getId()+"====>"+h.getStartTime()+"=====>"+h.getEndTime());
System.out.println("===================================");
}
}
}