//得到流程定义
List<ProcessDefinition> pdL = repositoryService.createProcessDefinitionQuery().list();
//流程实例
List<ProcessInstance> piL = executionService.createProcessInstanceQuery().list();
//当前用户的任务列表
List<Task> tL = taskService.findGroupTasks(username);
List<Task> tLByPerson = taskService.findPersonalTasks(username);
1、执行流程
根据类ProcessInstance的属性ID,执行流程
String pid = processInstance.getId();
executionService.signalExecutionById(pid);
//获取指定节点的Execution
Execution exec = processInstance.findActiveExecutionIn("state1");
executionService.signalExecutionById(exec.getId(), "to end1");
1、获取activityNames
Set<String> activityNames = processInstance.findActiveActivityNames();
if (activityNames == null) {
System.out.println("current activityNames is null" );
}
else {
for (String name: activityNames) {
System.out.println("activity name is "+name);
}
}
1、设置添加变量
executionService.setVariable(pid, "content","task2");
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("result", "ok");
taskService.setVariables(task.getId(), variables);
1、获取变量
Set<String> variableNames = taskService.getVariableNames(taskDbid);
Map<String, Object> variables = taskService.getVariables(taskDbid, variableNames);
2、查找所有任务
List<Task> ts = jbpmTemplate.getTaskService().createTaskQuery().list();
for(Task t : ts){
if(t.getName().equals("task1")){
jbpmTemplate.completeTask(t.getId());//通过任务id让任务向下流转
Map map = HashMap();
map.put("day",day);
jbpmTemplate.getTaskService().completeTask(t.getId(),map);//可以传参数
}
3、通过任务ID取指定变量的值
String leaveId = (String) jbpmTemplate.getVariableByTaskId(taskId, "leaveId");
String leaveId = (String) taskService.getVariable(taskId, "leaveId");
//通过流程实例id取指定变量的值
String pid = processInstance.getId();
String answer = (String) executionService.getVariable(pid, "answer");
4、判断流程是否执行在某个节点
processInstance.isActive("submit document");
5、任务转交
1.设置candidate-groups="lingo"
2.lingo可以通过findGroupsTasks()看到共享任务
3.使用assignTask(taskid, "admin")将这个任务转交给admin
4.admin通过findPersonalTasks()看到待办任务
5.admin通过completeTask()完成任务。
如果按照上面的步骤,jbpm-4.3下测试是通过的,下面是测试代码:
ProcessInstance processInstance = executionService
.startProcessInstanceByKey("X0603CandidateGroupTest");
List<Task> taskList = taskService.findGroupTasks("lingo");
assertEquals(1, taskList.size());
Task task = taskList.get(0);
taskService.assignTask(task.getId(), "admin");
taskList = taskService.findPersonalTasks("admin");
assertEquals(1, taskList.size());
taskService.completeTask(task.getId());
6、多条件判断
expr="#{a == 1 && b == 1}"
7、获取一个流程中的个人任务
List<Task> taskList = taskService.findPersonalTasks("johndoe");
Task task = taskList.get(0);
大家要慎用以上方法
强烈推荐以下2个方法,以免造成许多不必要的错误。
- //根据节点名取一流程实例下的任务
- Task task1 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).activityName("经理签收").uniqueResult();
- //根据任务分配者userId取一流程实例下的任务
- Task task2 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).assignee(userId).uniqueResult();
- 查看源码可得知processInstance.getId()为ExecutionID
8、获得当前节点与获得一个节点的后继节点
获得一个节点的后继节点很简单,只要getActivity()获得当前节点,然后getOutgoingTransitions()获得所有转出的连线,然后遍历连线获得destination就可以了。
节点类型可以通过activity的getType()方法获得。
9、怎么得到上一节点
最简单的,先获得executionImpl,executionImpl.getActivity()获得当前活动,然后根据activityImpl.getIncomingTransitions()获得所有前面的连线,得到transitionImpl。然后根据transitionImpl.getSource()获得连线的开始节点,这样就获得了前面的activity。
请注意,请注意,流程中的回退是千变万化的,一定要小心注意。很难写出通用的代码来。
-
- public void execute(ActivityExecution activityExecution) throws Exception {
- ExecutionImpl executionimpl = (ExecutionImpl) activityExecution;
- DbSession dbsession = EnvironmentImpl.getFromCurrent(DbSession.class);
- TaskImpl taskimpl = dbsession.createTask();
10、获取流程定义的xml文件
在页面上显示流程定义的xml内容
ProcessEngine processEngine = Configuration.getProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
String pdId = request.getParameter("pdId");
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionId(pdId).uniqueResult();
Set<String> names = repositoryService.getResourceNames(pd.getDeploymentId());
for (String n :names) {
if (n.indexOf(".jpdl.xml") != -1) {
InputStream in = repositoryService.getResourceAsStream(pd.getDeploymentId(), n);
byte[] buffer = new byte[1024*4];
int length;
while ((length = in.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, length);
}
response.getOutputStream().close();
}
}
11、获取流程定义的名字
如果需要流程定义的名字,需要通过repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).list()的方式先查询得到ProcessDefinition,,再调用对应的getName()方法获得流程定义名称。
12、请问在实现了AssignmentHandler的处理类中,如何得知是哪个节点调用的它?
((ExecutionImpl) openExecution).getActivity()
13、JBPM4中如何获取fork名字?
如果只为了获得定义中所有fork名称,可以直接遍历ProcessDeifnitionImpl中的activities,查找getType()为"fork"的活动。
14、
public void assign(Assignable assignable, OpenExecution execution) {
TaskImpl taskImpl = (TaskImpl)assignable;
ExecutionImpl executionImpl = (ExecutionImpl)execution;
}