工作流JBPM
jbpm3
共分为12种节点
1.node节点:如果该节点没有定义ACTION,则流转到该节点时不会停留,会继续往下走,如果定义ACTION,执行完ACTION后会停在该节点上,调用 token.signal后继续流转
node节点支持事件 node-enter,node-leave
2.state 节点 调用 token.signal后继续流转,否则会停下
3.tasknode 节点 该节点中可以创建多个任务 task 一个task产生一个任务实例没定义task会自动流转 state 节点只能创建一个task
TaskMgmtInstance tmg = pi.getTaskMgmtInstance();
TaskInstance startTask = tmg.createStartTaskInstance();
任务实例生命周期 创建--开始--完成 取消--挂起
任务实例 挂起 表taskinstance issuspended = true 挂起后不能操作 查询不到
isopen = true 是否关闭 关闭后不能操作 查询不到
action 当节点往下走的时候触发当前的节点的action
如果一个节点有多个任务,调用signal 会随机进入一个tasknode 根据流程实例获取token 根据token获取node,根据node获取 levaingTranstaion, 可执行的Transtaion列表
流程节点条件判断有三种方式1.在transtation 中定义变量,2在decode中定义表达式,在hadler中返回transtation ,3 继承 JbpmVariableResolver(推荐)
jbpm 自己解析流程实例变量 继承 JbpmVariableResolver 目的 不能写那么多个handler
查询单独非配给自己的私有任务列表 context.getTaskMgmtSession.findTaskInstances(actorid);
查询所属组的任务实例 context.getTaskMgmtSession.findPooledTaskInstance(actorid);
两个集合要合并 才能查看该人的所有任务
查询尚未结束的任务实例
Collection taskInstances = executionContext.getTaskMgmtInstance().getUnfinishedTasks(tonke);
会签
<task-node name = "审批" create-tasks="no" signal = "first" end-tasks="yes">
<event type="node-enter">
<action class="">
</action>
<task name="审批">
</task>
<task name="审批">
<event type = "task-end"> 在每个任务实例被结束的时候
<action class="tActionHandler">
</action>
<event>
</task>
end-tasks="yes" 有一个人审批通过后继续流转,其他人的任务都变成已完成
TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
TaskNode tasknode = (TaskNode )executionContext.getNode();
Task task = tasknode.getTasks().iterator.next();
for(){
TaskInstance ti = tmi.createTaskInstance(task,executionContext);
ti.setActorId(username);
}
//----------------
Conllection allTaskInstances = tmi.getTaskInstances();//获取当前流程实例下的所有任务实例
//获取当前流程实例当前Token 的未完成的任务实例
Conllection unfinishedTasks = tmi.getUnfinishedTasks(currentToken);
利用变量表达式实现自由节点,自己解释变量JbpmVariableResolver
//获取配置文件对象 JbpmConfiguration configuration = JbpmConfiguration.getInstance(); //根据配置文件对象获取jbpm上下文 JbpmContext context = configuration.createJbpmContext(); //根据上下文对象获取与jbpm表打交道的GraphSession GraphSession graphSession = context.getGraphSession(); //根据GraphSession获取流程定义对象 ProcessDefinition processDefinition = graphSession.findLatestProcessDefinition("sqj"); //根据流程模板创建流程实例 ProcessInstance instance = processDefinition.createProcessInstance(); //流程实例ID System.out.println(instance.getId()); //根据流程实例获取流程实例上下文对象 ContextInstance contextInstance = instance.getContextInstance(); contextInstance.setVariable("username", "张小三"); //根据流程实例获取任务管理器 TaskMgmtInstance taskMgmtInstance = instance.getTaskMgmtInstance(); //在开始节点中创建任务setActorId设置用户,不能在handler中设置,在handler中设置不上 context.setActorId("张小三"); //SecurityHelper.getAuthenticatedActorId(); //手动进入开始任务 TaskInstance taskInstance = taskMgmtInstance.createStartTaskInstance(); //任务实例ID System.out.println(taskInstance.getId()); //往当前任务实例中添加任务实例参数 taskInstance.setVariable("nextname", "李四"); //结束任务向下流转 //taskInstance.setActorId("张小三"); taskInstance.end("李四审批"); context.close();
UserHandler //获取当前token Token token = executionContext.getToken(); //根据当前token获取节点 Node node = token.getNode(); System.out.println(node.getName()); if(node.getName().equals("开始")){ String tusername = (String) executionContext.getVariable("username"); System.out.println(tusername); assignable.setActorId(tusername); //获取当前的流程实例 //TaskInstance taskInstance = executionContext.getTaskInstance(); //taskInstance.setActorId(tusername); }else{ String tusername = (String) executionContext.getVariable("nextname"); System.out.println(tusername); //获取当前的流程实例 TaskInstance taskInstance = executionContext.getTaskInstance(); taskInstance.setActorId(tusername); assignable.setActorId(tusername); }
public static void main(String[] args) { //创建流程模板 ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("helloword/processdefinition.xml"); //创建流程实例 ProcessInstance processInstance = new ProcessInstance(processDefinition); //获取一个流程实例的上下文对象 //ContextInstance contextInstance = processInstance.getContextInstance(); //上下文对象存放临时流程变量和获取同名会覆盖 //contextInstance.setTransientVariable("", ""); //contextInstance.addVariables(variables); //根据流程实例创建开始节点的任务实例 //TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance(); //向任务实例当中写入先关变量 //taskInstance.setVariable("money_count", ""); //结束任务实例流程的Token就进入下一节点 //taskInstance.end(); //流程节点 Token token = processInstance.getRootToken(); Node node = token.getNode(); //node节点不会触发 System.out.println(node.getName()); //节点向下流转一步 token.signal(); System.out.println(node.getName()); //判断是否结束 //token.getProcessInstance().hasEnded(); //单例,线程安全,配置文件对象 JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance(); //主要作用创建jbpmContext上下文对象//发布流程到数据库 JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); //从当前线程中获取上下文对象 //JbpmContext jbpmContext1 = jbpmConfiguration.getCurrentJbpmContext(); //创建数据库表 重新调用会重新创建 //jbpmConfiguration.createSchema(); //删除数据库表 //jbpmConfiguration.dropSchema(); //获取系统里的任务调度服务对象 //jbpmConfiguration.getJobExecutor(); //开始系统的任务调度服务 //jbpmConfiguration.startJobExecutor(); //jbpmContext.getTaskList(); //根据当前人ID获取任务列表 jbpmContext.getTaskList(""); //根据任务ID获取任务节点 TaskInstance instance = jbpmContext.getTaskInstance(11); instance.end("同意"); //根据最新的流程名称获取流程模板 //ProcessDefinition lastDefinition = jbpmContext.getGraphSession().findLatestProcessDefinition("helloword"); }
//发布流程模板 String path = "E:/work/hello.zip"; JbpmContext context = JbpmConfiguration.getInstance().createJbpmContext(); InputStream inputStream = new FileInputStream(new File(path)); ZipInputStream zipInputStream = new ZipInputStream(inputStream); ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream); context.deployProcessDefinition(processDefinition); zipInputStream.close(); inputStream.close(); context.close();
//创建JBPM数据库表 public static void createDB(){ //JbpmConfiguration configuration = JbpmConfiguration.getInstance(); JbpmConfiguration configuration = JbpmConfiguration.getInstance("hibernate.cfg.xml"); //configuration.createSchema(); } /** * 部署流程文件 * @param path */ public static void depFlow(String path){ //String path = "E:/liucheng/qingjia.zip"; JbpmContext context = null; InputStream inputStream = null; ZipInputStream zipInputStream = null; try { context = JbpmConfiguration.getInstance().createJbpmContext(); inputStream = new FileInputStream(new File(path)); zipInputStream = new ZipInputStream(inputStream); ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream); context.deployProcessDefinition(processDefinition); } catch (Exception e) { e.printStackTrace(); }finally{ try { zipInputStream.close(); inputStream.close(); context.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 开启流程 * @param flowName */ public static void startFlow(String flowName){ JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance(); JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); GraphSession graphSession = jbpmContext.getGraphSession(); ProcessDefinition processDefinition = graphSession.findLatestProcessDefinition(flowName); ProcessInstance processInstance = new ProcessInstance(processDefinition); ContextInstance contextInstance = processInstance.getContextInstance(); contextInstance.createVariable("name", "张三"); processInstance.signal(); } public static void startFlow2(String flowName){ JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance(); JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); GraphSession graphSession = jbpmContext.getGraphSession(); ProcessDefinition processDefinition = graphSession.findLatestProcessDefinition(flowName); ProcessInstance pi = processDefinition.createProcessInstance(); TaskMgmtInstance tmg = pi.getTaskMgmtInstance(); TaskInstance startTask = tmg.createStartTaskInstance(); startTask.setVariableLocally("JitTaskVar", ""); startTask.end(); } /** * 获取最新的所有流程模板 * @return */ public static List<ProcessDefinition> getAllLastProcessDefinitions(){ JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance(); JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); GraphSession graphSession = jbpmContext.getGraphSession(); List<ProcessDefinition> processDefinitions = graphSession.findLatestProcessDefinitions(); for (ProcessDefinition processDefinition : processDefinitions) { System.out.println("流程ID: "+processDefinition.getId()+"流程版本: "+processDefinition.getVersion()+" 流程名称"+processDefinition.getName()); } jbpmContext.close(); return processDefinitions; } /** * 获取当前人的任务列表 * @param actorId * @return */ public static List<Task> getAllTask(String actorId){ JbpmConfiguration configuration = JbpmConfiguration.getInstance(); JbpmContext context = configuration.createJbpmContext(); TaskMgmtSession mgmtSession = context.getTaskMgmtSession(); List<Task> tasks = mgmtSession.findTaskInstances(actorId); context.close(); return tasks; } /** * 获取所有的流程版本 * @return */ public static List<ProcessDefinition> getAllDefinitions(){ JbpmConfiguration configuration = JbpmConfiguration.getInstance(); JbpmContext context = configuration.createJbpmContext(); GraphSession graphSession = context.getGraphSession(); List<ProcessDefinition> definitions = graphSession.findAllProcessDefinitions(); for (ProcessDefinition processDefinition : definitions) { System.out.println("流程ID: "+processDefinition.getId()+"流程版本: "+processDefinition.getVersion()+" 流程名称"+processDefinition.getName()); } context.close(); return definitions; } /** * 获取当前流程的所有版本 * @return */ public static List<ProcessDefinition> getAllDefinitions(String defName){ JbpmConfiguration configuration = JbpmConfiguration.getInstance(); JbpmContext context = configuration.createJbpmContext(); GraphSession graphSession = context.getGraphSession(); List<ProcessDefinition> definitions = graphSession.findAllProcessDefinitionVersions(defName); for (ProcessDefinition processDefinition : definitions) { System.out.println("流程ID: "+processDefinition.getId()+"流程版本: "+processDefinition.getVersion()+" 流程名称"+processDefinition.getName()); } context.close(); return definitions; }