1.新建一个动态的web项目
2.在lib目录下加入以下jar包(由于用到fileupload的上传,所以还要加入fileupload的依赖库)
引用
*JBPM4_HOME/jbpm.jar
*JBPM4_HOME/lib/*.jar
*jakarta-taglibs-standard-1.1.2_HOME/lib/jstl.jar
*jakarta-taglibs-standard-1.1.2_HOME/lib/standard.jar
*commons-fileupload-1.2.1_HOME/lib/commons-fileupload-1.2.1.jar
3.绘制流程图(在resources目录下建立一个leave.jpdl.xml文件)
Activity(活动):
1. start活动的意义在于指明一个流程的实例应该从哪里开始发起,即流程的入口。start活动必须有一个流出转移(transition);
2. 流程实例流到State状态活动时,就陷入了一个中断、等待状态,必须得收到外部的干预操作后才能继续运行;
3. decision活动可以拥有多个流出转移,当流程实例到达decision活动时,会根据最先匹配成功的一个条件自动地通过相应的流出转移。
三种方式实现decision活动流向:
* 使用decision活动的condition元素。
* 使用decision活动的expr属性。
* 使用decision活动的handler元素。
4. fork-join:分支/聚合活动
5. end:结束活动
6. task:人工任务活动
7. sub-process:子流程活动
8. custom:自定义活动
Java代码
- <?xml version= "1.0" encoding= "UTF-8" ?>
-
- <process name="leave" xmlns= "http://jbpm.org/4.0/jpdl" >
- <start g="201,14,48,48" name= "开始" >
- <transition g="-42,-10" name= " 请假" to= "填写请假单" />
- </start>
- <task assignee="Kayzhan" g= "178,87,92,52" name= "填写请假单" >
- <transition g="-97,2" name= "判 断是不是经理" to= "是不是经理" />
- </task>
- <decision expr="#{manager}" g= "204,158,48,48" name= "是不是经理" >
- <transition g="-23,-11" name= " 否" to= "经理审核" />
- <transition g="14,-11" name= " 是" to= "老板审批" />
- </decision>
- <task assignee="Lingo" g= "103,252,92,52" name= "经理审核" >
- <transition g="150,450:10,-21" name= "经理批准" to= "结束" />
- <transition g="-22,-22" name= " 请假天数>3" to= "老板审批" />
- <transition g="-61,-1" name= " 经理不批准" to= "终止" />
- <transition g="149,114:-55,82" name= "经理驳回" to= "填写请假 单" />
- </task>
- <task assignee="ForgetDavi" g= "278,251,92,52" name= "老板审批" >
- <transition g="326,450:-58,-24" name= "老板批准" to= "结束" />
- <transition g="7,0" name= "老板不 批准" to= "终止" />
- <transition g="323,114:13,61" name= "老板驳回" to= "填写请假 单" />
- </task>
- <end g="219,429,48,48" name= "结束" state= "confirm" />
- <end g="220,360,48,48" name= "终止" state= "dissent" />
- </process>
<?xml version="1.0" encoding="UTF-8"?>
<process name="leave" xmlns="http://jbpm.org/4.0/jpdl">
<start g="201,14,48,48" name="开始">
<transition g="-42,-10" name="请假" to="填写请假单"/>
</start>
<task assignee="Kayzhan" g="178,87,92,52" name="填写请假单">
<transition g="-97,2" name="判断是不是经理" to="是不是经理"/>
</task>
<decision expr="#{manager}" g="204,158,48,48" name="是不是经理">
<transition g="-23,-11" name="否" to="经理审核"/>
<transition g="14,-11" name="是" to="老板审批"/>
</decision>
<task assignee="Lingo" g="103,252,92,52" name="经理审核">
<transition g="150,450:10,-21" name="经理批准" to="结束"/>
<transition g="-22,-22" name="请假天数>3" to="老板审批"/>
<transition g="-61,-1" name="经理不批准" to="终止"/>
<transition g="149,114:-55,82" name="经理驳回" to="填写请假单"/>
</task>
<task assignee="ForgetDavi" g="278,251,92,52" name="老板审批">
<transition g="326,450:-58,-24" name="老板批准" to="结束"/>
<transition g="7,0" name="老板不批准" to="终止"/>
<transition g="323,114:13,61" name="老板驳回" to="填写请假单"/>
</task>
<end g="219,429,48,48" name="结束" state="confirm"/>
<end g="220,360,48,48" name="终止" state="dissent"/>
</process>
4.编写流程发布及控制的类
- package zzg;
-
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.zip.ZipInputStream;
-
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.commons.fileupload.DiskFileUpload;
- import org.apache.commons.fileupload.FileItem;
- import org.jbpm.api.Configuration;
- import org.jbpm.api.Execution;
- import org.jbpm.api.ExecutionService;
- import org.jbpm.api.ProcessDefinition;
- import org.jbpm.api.ProcessDefinitionQuery;
- import org.jbpm.api.ProcessEngine;
- import org.jbpm.api.ProcessInstance;
- import org.jbpm.api.RepositoryService;
- import org.jbpm.api.TaskService;
- import org.jbpm.api.task.Task;
-
-
-
-
- public class LeaveServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private ProcessEngine processEngine = null ;
- private RepositoryService repositoryService = null ;
- private ExecutionService executionService = null ;
- private ProcessInstance processInstance = null ;
- private TaskService taskService = null ;
- private Task task = null ;
- private Execution execution = null ;
- private String url = null ;
- private List list = null ;
- private List<Task> taskList = null ;
-
-
- public void init(ServletConfig config) throws ServletException {
- super .init(config);
-
- // sdfsdfsdf
- processEngine = new Configuration().buildProcessEngine();
- repositoryService = processEngine.getRepositoryService();
- executionService = processEngine.getExecutionService();
- taskService = processEngine.getTaskService();
- }
-
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- process(request, response);
- }
-
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- process(request, response);
- }
-
- protected void process(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- String zzg = request.getParameter("zzg" );
-
- if (zzg.equals( "deploy" )) {
- try {
- deploy(request, response);
- } catch (Exception e) {
- e.printStackTrace();
- }
- list = getLatestProcessDefinition(request, response);
- request.setAttribute("process" , list);
- request.getRequestDispatcher("/index.jsp" ).forward(request,
- response);
- } else if (zzg.equals( "leave" )) {
- url = leave(request, response);
- request.getRequestDispatcher(url).forward(request, response);
- } else if (zzg.equals( "query" )) {
- list = getProcessInstanceById(request, response);
- request.setAttribute("pi" , list);
- request.getRequestDispatcher("/index.jsp" ).forward(request,
- response);
- } else if (zzg.equals( "apply" )) {
- apply(request, response);
- list = getProcessInstanceById(request, response);
- request.setAttribute("pi" , list);
- request.getRequestDispatcher("/index.jsp" ).forward(request,
- response);
- } else if (zzg.equals( "getTask" )) {
- list = getTask(request, response);
- request.setAttribute("taskList" , list);
- request.getRequestDispatcher("/check.jsp" ).forward(request,
- response);
- } else if (zzg.equals( "view" )) {
- Map<String, Object> map = view(request, response);
- request.setAttribute("map" , map);
- request.getRequestDispatcher("/view.jsp" )
- .forward(request, response);
- } else if (zzg.equals( "confirm" )) {
- confirm(request, response);
- list = getTask(request, response);
- request.setAttribute("taskList" , list);
- request.getRequestDispatcher("/check.jsp" ).forward(request,
- response);
- } else if (zzg.equals( "dissent" )) {
- dissent(request, response);
- list = getTask(request, response);
- request.setAttribute("taskList" , list);
- request.getRequestDispatcher("/check.jsp" ).forward(request,
- response);
- }
-
-
-
-
-
-
-
- }
-
- protected void deploy(HttpServletRequest request,
- HttpServletResponse response) throws Exception, IOException {
- String temp = getServletContext().getRealPath("/temp" );
- String upload = getServletContext().getRealPath("/upload" );
- DiskFileUpload diskFileUpload = new DiskFileUpload();
- diskFileUpload.setFileSizeMax(1 * 1024 * 1024 );
- diskFileUpload.setSizeThreshold(4096 );
- diskFileUpload.setRepositoryPath(temp);
- List fileItems = diskFileUpload.parseRequest(request);
- Iterator iter = fileItems.iterator();
- if (iter.hasNext()) {
- FileItem item = (FileItem) iter.next();
- if (!item.isFormField()) {
- String name = item.getName();
- long size = item.getSize();
- System.out.println("name: " + name + "&size:" + size);
- if (name != null && !name.equals( "" ) && size > 0 ) {
-
- repositoryService.createDeployment()
- .addResourcesFromZipInputStream(
- new ZipInputStream(item.getInputStream()))
- .deploy();
- }
- }
- }
- }
-
- protected String leave(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
-
- processInstance = executionService.startProcessInstanceByKey("leave" );
-
- if (processInstance.isActive( "填写请假单" )) {
- url = "/apply.jsp" ;
- }
- return url;
- }
-
- protected Map<String, Object> apply(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
-
- Map<String, Object> variables = new HashMap<String, Object>();
- String name = request.getParameter("applyName" );
- String time = request.getParameter("applyTime" );
- String leaveDay = request.getParameter("leaveDay" );
- String content = request.getParameter("content" );
- String position = request.getParameter("position" );
-
- variables.put("name" , name);
- variables.put("time" , time);
- variables.put("leaveDay" , leaveDay);
- variables.put("content" , content);
- variables.put("position" , position);
- if (position.trim().equals( "经理" )) {
-
- variables.put("manager" , "是" );
- } else {
- variables.put("manager" , "否" );
- }
-
- taskList = taskService.findPersonalTasks("zzg" );
-
- task = taskList.get(0 );
-
- taskService.setVariables(task.getId(), variables);
- taskService.completeTask(task.getId());
-
-
-
-
- return variables;
- }
-
-
-
-
-
-
-
-
-
-
- protected List<Task> getTask(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- String name = request.getParameter("name" );
- taskList = taskService.findPersonalTasks(name);
- return taskList;
- }
-
-
-
-
-
-
-
-
-
-
- protected Map<String, Object> view(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- String taskId = request.getParameter("id" );
- Set<String> set = taskService.getVariableNames(taskId);
- Map<String, Object> map = taskService.getVariables(taskId, set);
-
- return map;
- }
-
- protected void confirm(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- String taskId = request.getParameter("id" );
- task = taskService.getTask(taskId);
- execution = executionService.findExecutionById(task.getExecutionId());
- if (execution.getProcessInstance().isActive( "老板审批" )) {
- taskService.completeTask(taskId, "老板批准" );
- } else if (execution.getProcessInstance().isActive( "经理审核" )) {
- String variable = (String) taskService.getVariable(taskId,
- "leaveDay" );
- if (Integer.valueOf(variable) > 3 ) {
- taskService.completeTask(taskId, "请假天数>3" );
- } else {
- taskService.completeTask(taskId, "经理批准" );
- }
- }
- }
-
- protected void dissent(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- String taskId = request.getParameter("id" );
- task = taskService.getTask(taskId);
- execution = executionService.findExecutionById(task.getExecutionId());
- if (execution.getProcessInstance().isActive( "老板审批" )) {
- taskService.completeTask(taskId, "老板不批准" );
- } else if (execution.getProcessInstance().isActive( "经理审核" )) {
- taskService.completeTask(taskId, "经理不批准" );
- }
- }
-
-
-
-
-
-
-
-
-
- protected List<ProcessDefinition> getLatestProcessDefinition(
- HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- List<ProcessDefinition> processDefinitions = repositoryService
- .createProcessDefinitionQuery().orderAsc(
- ProcessDefinitionQuery.PROPERTY_NAME).list();
- Map<String, ProcessDefinition> map = new LinkedHashMap<String, ProcessDefinition>();
- for (ProcessDefinition pd : processDefinitions) {
- String key = pd.getKey();
- ProcessDefinition definition = map.get(key);
- if ((definition == null )
- || (definition.getVersion() < pd.getVersion())) {
- map.put(key, pd);
- }
- }
- return new ArrayList(map.values());
- }
-
-
-
-
-
-
-
-
-
- protected List<ProcessInstance> getProcessInstanceById(
- HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String pdId = request.getParameter("id" );
- return executionService.createProcessInstanceQuery()
- .processDefinitionId(pdId).list();
- }
-
- }
package zzg;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipInputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.jbpm.api.Configuration;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;
/**
* Servlet implementation class LeaveServlet
*/
public class LeaveServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ProcessEngine processEngine = null;
private RepositoryService repositoryService = null;
private ExecutionService executionService = null;
private ProcessInstance processInstance = null;
private TaskService taskService = null;
private Task task = null;
private Execution execution = null;
private String url = null;
private List list = null;
private List<Task> taskList = null;
/* 初始化ProcessEngine. */
public void init(ServletConfig config) throws ServletException {
super.init(config);
// 获取流程引擎
processEngine = new Configuration().buildProcessEngine();
repositoryService = processEngine.getRepositoryService();
executionService = processEngine.getExecutionService();
taskService = processEngine.getTaskService();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String zzg = request.getParameter("zzg");
if (zzg.equals("deploy")) {
try {
deploy(request, response);
} catch (Exception e) {
e.printStackTrace();
}
list = getLatestProcessDefinition(request, response);
request.setAttribute("process", list);
request.getRequestDispatcher("/index.jsp").forward(request,
response);
} else if (zzg.equals("leave")) {
url = leave(request, response);
request.getRequestDispatcher(url).forward(request, response);
} else if (zzg.equals("query")) {
list = getProcessInstanceById(request, response);
request.setAttribute("pi", list);
request.getRequestDispatcher("/index.jsp").forward(request,
response);
} else if (zzg.equals("apply")) {
apply(request, response);
list = getProcessInstanceById(request, response);
request.setAttribute("pi", list);
request.getRequestDispatcher("/index.jsp").forward(request,
response);
} else if (zzg.equals("getTask")) {
list = getTask(request, response);
request.setAttribute("taskList", list);
request.getRequestDispatcher("/check.jsp").forward(request,
response);
} else if (zzg.equals("view")) {
Map<String, Object> map = view(request, response);
request.setAttribute("map", map);
request.getRequestDispatcher("/view.jsp")
.forward(request, response);
} else if (zzg.equals("confirm")) {
confirm(request, response);
list = getTask(request, response);
request.setAttribute("taskList", list);
request.getRequestDispatcher("/check.jsp").forward(request,
response);
} else if (zzg.equals("dissent")) {
dissent(request, response);
list = getTask(request, response);
request.setAttribute("taskList", list);
request.getRequestDispatcher("/check.jsp").forward(request,
response);
}
/*
* try { Class clz = this.getClass(); Method method =
* clz.getDeclaredMethod(action, HttpServletRequest.class,
* HttpServletResponse.class); method.invoke(this, request, response); }
* catch (Exception ex) { ex.printStackTrace(); }
*/
}
protected void deploy(HttpServletRequest request,
HttpServletRespon