将JBPM4整合到web工程---流程实例管理

1.将上一篇“将JBPM4整合到web工程”项目拷贝一份

2.修改index.jsp文件至如下,在现实流程定义里面增加两个操作“启动流程,查看流程”。同时增加一个用来显示流程实例的table,并添加一个“执行”的操作。
Java代码   收藏代码
  1. <%@page contentType="text/html;charset=UTF-8" %>  
  2. <%@page import="org.jbpm.api.*" %>  
  3. <%@page import="java.util.*" %>  
  4. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  5.   
  6.   
  7.       
  8.         jBPM4学习  
  9.       
  10.       
  11.         "jbpm?zzg=deploy" method="post">  
  12.             "xml" cols=50 rows=15>  
  13.             "submit" value="发布"/>  
  14.             "reset" value="取消"/>  
  15.           
  16.           
  17.         480 border=1>  
  18.             "5">流程定义  
  19.               
  20.                 ID  
  21.                 Key  
  22.                 名称  
  23.                 版本  
  24.                 操作  
  25.               
  26.             "process" items="${process}">  
  27.               
  28.                 ${process.id}  
  29.                 ${process.key}  
  30.                 ${process.name}  
  31.                 ${process.version}  
  32.                 "jbpm?zzg=start&key=${process.key }">启动  
  33.                 "jbpm?zzg=query&id=${process.id }">查看  
  34.                 "jbpm?zzg=delete&id=${process.id }">删除  
  35.               
  36.               
  37.           
  38.         
      
  39.         
      
  40.         
      
  41.         480 border=1>  
  42.             "5">流程实例  
  43.               
  44.                 ID  
  45.                 Key  
  46.                 状态  
  47.                 操作  
  48.               
  49.             "pi" items="${pi}">  
  50.               
  51.                 ${pi.id}  
  52.                 ${pi.key}  
  53.                 ${pi.state}  
  54.                 "jbpm?zzg=run&pid=${pi.id }">执行  
  55.               
  56.               
  57.           
  58.       
  59.   


3.然后修改至JbpmServlet.java文件如下
Java代码   收藏代码
  1. package zzg;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.LinkedHashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import javax.servlet.ServletConfig;  
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.http.HttpServlet;  
  12. import javax.servlet.http.HttpServletRequest;  
  13. import javax.servlet.http.HttpServletResponse;  
  14.   
  15. import org.jbpm.api.Configuration;  
  16. import org.jbpm.api.ExecutionService;  
  17. import org.jbpm.api.ProcessDefinition;  
  18. import org.jbpm.api.ProcessDefinitionQuery;  
  19. import org.jbpm.api.ProcessEngine;  
  20. import org.jbpm.api.ProcessInstance;  
  21. import org.jbpm.api.RepositoryService;  
  22.   
  23. public class JbpmServlet extends HttpServlet {  
  24.     private static final long serialVersionUID = 1L;  
  25.     private ProcessEngine processEngine = null;  
  26.     private RepositoryService repositoryService = null;  
  27.     private ExecutionService executionService = null;  
  28.     private ProcessInstance processInstance = null;  
  29.   
  30.     /* 初始化ProcessEngine. */  
  31.     public void init(ServletConfig config) throws ServletException {  
  32.         processEngine = new Configuration().buildProcessEngine();  
  33.         repositoryService = processEngine.getRepositoryService();  
  34.         executionService = processEngine.getExecutionService();  
  35.     }  
  36.   
  37.     protected void doGet(HttpServletRequest request,  
  38.             HttpServletResponse response) throws ServletException, IOException {  
  39.         process(request, response);  
  40.     }  
  41.   
  42.     protected void doPost(HttpServletRequest request,  
  43.             HttpServletResponse response) throws ServletException, IOException {  
  44.         process(request, response);  
  45.     }  
  46.   
  47.     protected void process(HttpServletRequest request,  
  48.             HttpServletResponse response) throws ServletException, IOException {  
  49.         String zzg = request.getParameter("zzg");  
  50.         List pi = null;  
  51.         if (zzg.equals("deploy")) {  
  52.             deployProcessDefinition(request, response);  
  53.         } else if (zzg.equals("delete")) {  
  54.             removeProcessDefinitionById(request, response);  
  55.         } else if (zzg.equals("start")) {  
  56.             startProcessInstance(request, response);  
  57.         } else if (zzg.equals("query")) {  
  58.             pi = getProcessInstance(request, response);  
  59.             request.setAttribute("pi", pi);  
  60.         } else if (zzg.equals("run")) {  
  61.             signalProcessInstance(request, response);  
  62.             pi = getProcessInstance(request, response);  
  63.             request.setAttribute("pi", pi);  
  64.         }  
  65.         List list = getLatestProcessDefinition(request,  
  66.                 response);  
  67.         request.setAttribute("process", list);  
  68.         request.getRequestDispatcher("/index.jsp").forward(request, response);  
  69.     }  
  70.   
  71.     /** 
  72.      * 发布流程方法 
  73.      *  
  74.      * @param request 
  75.      * @param response 
  76.      * @throws ServletException 
  77.      * @throws IOException 
  78.      */  
  79.     protected void deployProcessDefinition(HttpServletRequest request,  
  80.             HttpServletResponse response) throws ServletException, IOException {  
  81.         String xml = request.getParameter("xml");  
  82.         repositoryService.createDeployment().addResourceFromString(  
  83.                 "yy.jpdl.xml", xml).deploy();  
  84.     }  
  85.   
  86.     /* 获取最新流程. */  
  87.     protected List getLatestProcessDefinition(  
  88.             HttpServletRequest request, HttpServletResponse response)  
  89.             throws ServletException, IOException {  
  90.         // 获取全部的流程定义  
  91.         List processDefinitions = repositoryService  
  92.                 .createProcessDefinitionQuery().orderAsc(  
  93.                         ProcessDefinitionQuery.PROPERTY_NAME).list();  
  94.   
  95.         Map map = new LinkedHashMap();  
  96.         for (ProcessDefinition pd : processDefinitions) {  
  97.             String key = pd.getKey();  
  98.             ProcessDefinition definition = map.get(key);  
  99.             if ((definition == null)  
  100.                     || (definition.getVersion() < pd.getVersion())) {  
  101.                 map.put(key, pd);  
  102.             }  
  103.         }  
  104.         return new ArrayList(map.values());  
  105.     }  
  106.   
  107.     /* 删除流程定义. */  
  108.     protected void removeProcessDefinitionById(HttpServletRequest request,  
  109.             HttpServletResponse response) throws ServletException, IOException {  
  110.         String id = request.getParameter("id");  
  111.         // 根据流程ID获取流程定义实例  
  112.         ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()  
  113.                 .processDefinitionId(id).uniqueResult();  
  114.         // 根据流程定义发布的ID来删除流程定义  
  115.         // 注意:jbpm4是不允许直接根据流程定义的ID来直接删除流程定义的  
  116.         repositoryService.deleteDeploymentCascade(pd.getDeploymentId());  
  117.     }  
  118.   
  119.     /* 根据流程定义的key值启动流程. */  
  120.     protected void startProcessInstance(HttpServletRequest request,  
  121.             HttpServletResponse response) throws ServletException, IOException {  
  122.         String key = request.getParameter("key");  
  123.         processInstance = executionService.startProcessInstanceByKey(key);  
  124.     }  
  125.   
  126.     /* 根据流程定义的ID查询流程实例. */  
  127.     protected List getProcessInstance(  
  128.             HttpServletRequest request, HttpServletResponse response)  
  129.             throws ServletException, IOException {  
  130.         String pdId = request.getParameter("id");  
  131.         return executionService.createProcessInstanceQuery()  
  132.                 .processDefinitionId(pdId).list();  
  133.     }  
  134.   
  135.     /* 根据流程实例的ID执行流程实例. */  
  136.     protected void signalProcessInstance(HttpServletRequest request,  
  137.             HttpServletResponse response) throws ServletException, IOException {  
  138.         String pid = request.getParameter("pid");  
  139.         executionService.signalExecutionById(pid);  
  140.     }  
  141. }  


4.控制流程的活动主要包括以下几个:
  • Start 启动流程
  • End 结束流程
  • State 流程等待
  • Decision 决定流程流向
  • Task 等待
  • Fork 流程分支
  • Join 流程汇聚
  • Custom 操作流程
  • Sub-process 启动一个子流程

你可能感兴趣的:(jBPM)