说明:流程实例创建后,直接就到开始活动后的第一个活动,不会在开始活动停留。
ProcessInstance pi = processEngine.getExecutionService()
.startProcessInstanceByKey(processDefinitionKey);
// 准备流程变量
Map<String, Object>variables = new HashMap<String, Object>();
variables.put("申请人", "张三");
variables.put("报销金额", 1000.00);
// 启动流程实例,并设置一些流程变量
ProcessInstance pi = processEngine.getExecutionService()
.startProcessInstanceByKey(processDefinitionKey, variables);
processEngine.getExecutionService().signalExecutionById(executionId);
Map<String, Object>variables = new HashMap<String, Object>();
variables.put("审批结果", "同意");
processEngine.getExecutionService()
.signalExecutionById(executionId, variables);
String outcome= "to end1";
processEngine.getExecutionService()
.signalExecutionById(executionId, outcome);
String outcome= "to end1";
Map<String, Object>variables = new HashMap<String, Object>();
variables.put("审批结果", "同意");
processEngine.getExecutionService()
.signalExecutionById(executionId, outcome, variables);
方式1:TaskService.findPersonalTasks(userId);
方式2:List<Task> list = taskService.createTaskQuery()
.assignee(userId)
.list();
// 显示任务信息
for (Task task : taskList) {
System.out.println("id=" + task.getId()// 任务的id
+ ",name=" + task.getName()// 任务的名称
+ ",assignee=" + task.getAssignee()// 任务的办理人
+ ",createTime=" + task.getCreateTime() // 任务的创建(生成)的时间
+ ",executionId=" + task.getExecutionId());// 任务所属流程实例的id
}
方式1: taskService.findGroupTasks(userId);
方式2: List<Task> list = processEngine.getTaskService()//
.createTaskQuery()//
.candidate(userId)//
.list();
String taskId = "420001";
processEngine.getTaskService().completeTask(taskId);
processEngine.getTaskService().completeTask(taskId, outcome);
processEngine.getTaskService().completeTask(taskId, outcome, variables);
String taskId = "420001";
// 1,设置为false代表:办理完任务后不向后移动(默认为true)
TaskImpl taskImpl = (TaskImpl) processEngine
.getTaskService().getTask(taskId);
taskImpl.setSignalling(false);
// 2,办理完任务
processEngine.getTaskService().completeTask(taskId);
1, TaskService.takeTask(taskId,userId),拾取组任务到个人任务列表中,如果任务有assignee,则会抛异常。
2, processEngine.getTaskService().assignTask(taskId,userId),转交任务给其他人,(如果任务有assignee,则执行这个方法代表重新分配。也可以把assignee设为null表示组任务没有人办理了)
Execution 执行对象
表示一个执行的路线
ProcessInstance 流程实例
说明:
表示从开始到结束这个最大的执行路线
对应的表:
jbpm4_execution 正在执行的信息表
jbpm4_hist_procinst 已执行完的流程实例表
Task 任务
对应的表:
jbpm4_task 正在执行的任务表
jbpm4_hist_task 已执行完的任务表
publicclass ProcessInstanceTest {
private ProcessEngine processEngine = Configuration.getProcessEngine();
@Test
publicvoid deployProcessDefinition() {
processEngine.getRepositoryService()//
.createDeployment()//
.addResourceFromClasspath("com/xsy/jbpm/c_processinstance/test.jpdl.xml")//
.addResourceFromClasspath("com/xsy/jbpm/c_processinstance/test.png")//
.deploy();
}
// 启动流程实例
// jbpm4_execution
@Test
publicvoid testStartProcessInstance() throws Exception {
ProcessInstance pi = processEngine.getExecutionService().startProcessInstanceByKey("test");
System.out.println("流程实例启动成功:id=" + pi.getId() //
+ ", state=" + pi.getState() //状态
+ ",processDefinitionId=" +pi.getProcessDefinitionId()); // 使用的流程定义的ID
}
// 查询个人任务列表
@Test
publicvoid testFindMyPersonalTaskList() throws Exception {
String userId = "张三";
// 查询
// List<Task> list =processEngine.getTaskService().findPersonalTasks(userId);
// 分页
List<Task> list = processEngine.getTaskService()//
.createTaskQuery()//
.assignee(userId)// 指定办理人条件
.page(0, 100)// 分页
.list();
// 显示
System.out.println("======" + userId + "】的个人任务列表 ======");
for (Task task : list) {
System.out.println("id=" + task.getId()//
+ ",name=" + task.getName()//
+ ",assignee=" + task.getAssignee()//
+ ",createTime=" + task.getCreateTime()//
+ ",executionId=" + task.getExecutionId());//任务属于一个执行
}
}
// 办理任务
@Test
publicvoid testCompleteTask() throws Exception {
String taskId = "110002";
processEngine.getTaskService().completeTask(taskId);
}
// 向后执行一步
@Test
publicvoid testSignalExecution() throws Exception {
String executionId = "test.130001";
processEngine.getExecutionService().signalExecutionById(executionId);
}
}
ExecutionService.setVariable(executionId,name, value);
Object obj = executionService.getVariable(executionId,"请假人");
TaskService.setVariables(taskId,variables); // 一次设置多个变量
Object obj = executionService.getVariable(executionId,"请假人");
7.2. Variable types
jBPMsupports following Java types as process variables:
java.lang.String
java.lang.Long
java.lang.Double
java.util.Date
java.lang.Boolean
java.lang.Character
java.lang.Byte
java.lang.Short
java.lang.Integer
java.lang.Float
byte[] (byte array)
char[] (char array)
hibernate entity with a long id
hibernate entity with a string id
通过hibernate建一个表保存
Serializable序列化成为二进制
Forpersistence of these variable, the type of the variable is checked in the orderof this list. The first match will determine how the variable is stored.
String processInstanceId = "test.10001";
processEngine.getExecutionService()
.endProcessInstance(processInstanceId,ProcessInstance.STATE_ENDED);
publicclass ProcessVariableTest {
private ProcessEngine processEngine = Configuration.getProcessEngine();
// 启动流程实例
@Test
publicvoid testStartProcessInstance() throws Exception {
ProcessInstance pi = processEngine.getExecutionService().startProcessInstanceByKey("test");
System.out.println("流程实例启动成功:id=" + pi.getId());
}
// 设置流程变量
@Test
publicvoid testSetVariable() throws Exception {
String executionId = "test.170001";
processEngine.getExecutionService().setVariable(executionId, "请假天数", 15);
}
// 获取流程变量
@Test
publicvoid testGetVariable() throws Exception {
String executionId = "test.170001";
Integer days = (Integer) processEngine.getExecutionService().getVariable(executionId,"请假天数");
System.out.println("请假天数 = " + days);
}
/**
{
ExecutionService executionService =processEngine.getExecutionService();
TaskService taskService = processEngine.getTaskService();
// ============ 设置变量 ========================
executionService.setVariable(executionId, name, value); // 设置一个变量
executionService.setVariables(executionId, variablesMap); // 设置多个变量
taskService.setVariables(taskId, variables); // 设置多个变量
executionService.startProcessInstanceByKey(processDefinitionKey,variablesMap); // 启动流程实例时,先设置一些变量
taskService.completeTask(taskId, variablesMap); // 真正办理完任务前先设置一些变量
// ============ 获取变量 ========================
executionService.getVariable(executionId, variableName); // 获取一个变量
executionService.getVariableNames(executionId); // 返回Set<String>,是所有变量的名称集合
executionService.getVariables(executionId, variableNames); //获取多个变量,返回Map<String,Object>,表示指定名称的变量信息
taskService.getVariable(taskId, variableName);
taskService.getVariableNames(taskId);
taskService.getVariables(taskId, variableNames);
}
*/
}
publicclass ProcessVariableTest2 {
private ProcessEngine processEngine = Configuration.getProcessEngine();
// 启动流程实例
@Test
publicvoid testStartProcessInstance() throws Exception {
ProcessInstance pi = processEngine.getExecutionService().startProcessInstanceByKey("test");
System.out.println("流程执行Id=" + pi.getId());
}
// 设置实现序列化接口或者具备hibernate映射规则的对象
//优先使用hibernate的方式
@Test
publicvoid testSetVariable() throws Exception {
String executionId = "test.270001";
Form form = new Form(2L, "新东风科技大半年"); // 通过指定ID模拟一个持久化状态
processEngine.getExecutionService().setVariable(executionId, "form", form);
}
// 获取变量
@Test
publicvoid testGetVariable() throws Exception {
String executionId = "test.270001";
Form form = (Form) processEngine.getExecutionService().getVariable(executionId, "form");
System.out.println(form);
}
}
因为使用serializable存在这个问题:在序列化之后改变了对象的状态之后,反序列化回来的还是原来的对象,造成不同步。因此一般使用hibernate类型(Long主键或者String主键)
在放入hibernate类型的变量时,注意
String executionId ="test.150001";
Formform = new Form(1L, "我要请假,我是张三");
// 通过指定id来模拟一个游离状态的对象,就是要在数据库中有这个记录
processEngine.getExecutionService().setVariable(executionId,"form", form);
String executionId ="test.150001";
Formform = (Form) processEngine.getExecutionService().getVariable(executionId,"form");
System.out.println(form);
没有id就是临时状态,抛出这个异常
也即是说这个对象在数据库和session中没有记录,指定id之后变为游离状态,能骗过jbpm,但是在获取的时候因为在数据库中没有记录,会抛出这个异常
所以一定要在数据库中有这个对象才行。另外,在jbpm中使用的是load来获取对象,又懒加载现象,而jbpm的每个方法都是自己独立的事务,事务完成后session就关闭了,懒加载问题就会造成上面的异常,因此要去掉懒加载特性(对象设置成final【不使用代理】,映射文件lazy=fasle)