关于如何配置请到网上找相关资料,
jbpm4.4项目测试源码下载, 注释都在代码里!
下载地址:http://download.csdn.net/detail/liangrui1988/6303585
里面只加了struts2,没有用到spring,用的太多反而不好理解!本来也不想用strtus2的,但那样做起来太麻烦........
junit测试代码:
package com.accp.test2; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jbpm.api.Configuration; import org.jbpm.api.ExecutionService; import org.jbpm.api.ProcessEngine; import org.jbpm.api.task.Task; import org.junit.Test; public class Qingjia { //创建表 @Test public void createTable(){ new org.hibernate.cfg.Configuration().configure("jbpm.hibernate.cfg.xml"); } //加载文件 ProcessEngine pe=new Configuration().setResource("jbpm.cfg.xml").buildProcessEngine(); //ProcessEngine pe=Configuration.getProcessEngine(); //部署资源流程 @Test public void deploye(){ pe.getRepositoryService().createDeployment().addResourceFromClasspath("leave.jpdl.xml").deploy(); } //启动流程 @Test public void startProcess(){ //参数 Map map=new HashMap(); map.put("owner","张三"); map.put("day", 1); ExecutionService executionService=pe.getExecutionService(); executionService.startProcessInstanceByKey("leave",map); } //查询任务 @Test public void findTask(){ String userID="张三"; //要查询任务的用户名字 System.out.println(userID); List<Task> listTask=pe.getTaskService().findPersonalTasks(userID); for(Task t:listTask){ System.out.println("TaskID: "+t.getId()); //int day=(Integer) pe.getTaskService().getVariable(taskID,"day"); } } //完成任务 @Test public void completeTask(){ //String taskID="10004";//任务id String taskID="70004";//任务id Map map=new HashMap(); map.put("owner","张三"); map.put("manager","李经理"); map.put("boss", "王老板"); //pe.getTaskService().completeTask(taskID,map); //得到上一步传过来的值 int day=(Integer) pe.getTaskService().getVariable(taskID,"day"); map.put("day", day); //map.put("day", -1); //传入值 pe.getTaskService().setVariables(taskID, map); pe.getTaskService().completeTask(taskID);//完成任务 } }
主要action代码:
package com.accp.action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.zip.ZipInputStream; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import org.jbpm.api.Configuration; import org.jbpm.api.ProcessDefinition; import org.jbpm.api.ProcessEngine; import org.jbpm.api.ProcessInstance; import org.jbpm.api.TaskService; import org.jbpm.api.history.HistoryProcessInstance; import org.jbpm.api.task.Task; import com.accp.bean.User; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.Preparable; public class JbpmManger extends ActionSupport implements Preparable{ // 流程定义集合 private List<ProcessDefinition> processDefinitionList; // 流程实例集合 private List<ProcessInstance> procesInstanceList; // 流程参数集合 private Map<String, Object> map; // 历史流程 private List<HistoryProcessInstance> historyProcessInstanceList; // 当前用户任务集合 private List<Task> tasks; //加载jbpm.xml文件 得到工作流引擎 private ProcessEngine processEngine;// //private User user; public ProcessEngine getProcessEngine() { //processEngine=new Configuration().setResource("jbpm.cfg.xml").buildProcessEngine(); processEngine=Configuration.getProcessEngine(); return processEngine; } public void setProcessEngine(ProcessEngine processEngine) { this.processEngine = processEngine; } public List<ProcessDefinition> getProcessDefinitionList() { //流程定义集合 processDefinitionList=processEngine.getRepositoryService().createProcessDefinitionQuery().list(); return processDefinitionList; } public void setProcessDefinitionList( List<ProcessDefinition> processDefinitionList) { this.processDefinitionList = processDefinitionList; } public List<ProcessInstance> getProcesInstanceList() { //流程实列集合 procesInstanceList=processEngine.getExecutionService().createProcessInstanceQuery().list(); return procesInstanceList; } public void setProcesInstanceList(List<ProcessInstance> procesInstanceList) { this.procesInstanceList = procesInstanceList; } public List<HistoryProcessInstance> getHistoryProcessInstanceList() { //历史记录 historyProcessInstanceList=processEngine.getHistoryService().createHistoryProcessInstanceQuery().list(); return historyProcessInstanceList; } public void setHistoryProcessInstanceList( List<HistoryProcessInstance> historyProcessInstanceList) { this.historyProcessInstanceList = historyProcessInstanceList; } public List<Task> getTasks() { //通过用户 获取工作列表 System.out.println("actionUserName"+actionUserName); List<Task> listTask=processEngine.getTaskService().findPersonalTasks(actionUserName); for(Task t:listTask){ System.out.println("TaskID: "+t.getId() +" "+t.getActivityName()+"t "+t.getAssignee()); } return listTask; } public void setTasks(List<Task> tasks) { this.tasks = tasks; } //初如化action 这里暂不用spring 本来连struts也不想用的,但写在servlet实在太烦锁了 为了更好理解jbpm的动行原理======================================================== ================ private String actionUserName; private String actionRole; public void prepare() throws Exception { getProcessEngine(); actionUserName=(String) ServletActionContext.getRequest().getSession().getAttribute("username"); if("".equals(actionUserName)||null==actionUserName){ returnIndex();//如果没有用户则返回 } actionRole=(String) ServletActionContext.getRequest().getSession().getAttribute("role"); } //如果没有登陆则返回 public String returnIndex(){ return "showIndex"; } //初如化数据库表======================================================================== ================ public String initDB() throws Exception { new org.hibernate.cfg.Configuration().configure("jbpm.hibernate.cfg.xml"); return "showIndex"; } //直接 部署流程======================================================================== ================ public String deployeJbpm() throws Exception { processEngine.getRepositoryService().createDeployment().addResourceFromClasspath("leave.jpdl.xml").deploy(); return "showIndex"; } //ZIP方式 部署流程======================================================================== ================ private File fileJbpmZip;//接收上传的文件 private String fileJbpmZipFileName;//自动获取文件名称 private String fileJbpmZipContentType;//文件内型 private String saveDir;//需要保存的文件目录文件夹 public void setSaveDir(String saveDir) { this.saveDir = saveDir; } public void setFileJbpmZipFileName(String fileJbpmZipFileName) { this.fileJbpmZipFileName = fileJbpmZipFileName; } public void setFileJbpmZipContentType(String fileJbpmZipContentType) { this.fileJbpmZipContentType = fileJbpmZipContentType; } public void setFileJbpmZip(File fileJbpmZip) { this.fileJbpmZip = fileJbpmZip; } //ZIP方式 部署流程======================================================================== ================ public String deployeJbpmZIP() /*throws Exception*/ { public String deployeJbpmZIP() /*throws Exception*/ { String filePath=""; //服务器目录+文件夹的目录+/+文件名 filePath=ServletActionContext.getServletContext().getRealPath(saveDir)+File.separator+fileJbpmZipFileName; //通过文件路径创建File对象 System.out.println("filePath: "+filePath); File fileUpload=new File(filePath); //拷贝文件 上传的File拷贝到新创建的File try { FileUtils.copyFile(fileJbpmZip, fileUpload); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //创建zipInputStream文件 通过fileInputStream文件输出流 ZipInputStream zis=null; try { zis=new ZipInputStream(new FileInputStream(filePath)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //部署文件 Zip processEngine=Configuration.getProcessEngine(); processEngine.getRepositoryService().createDeployment(). addResourcesFromZipInputStream(zis).deploy(); return "showIndex"; } //ZIP方式 部署固定文件 流程======================================================================= ================ public String deployeJbpmZIP() /*throws Exception*/ { public String deployeJbpmZIPs() throws Exception { //String filePath=ServletActionContext.getServletContext().getRealPath(saveDir)+File.separator+"leave.jpdl.zip"; //创建zipInputStream文件 通过fileInputStream文件输出流 new FileInputStream(filePath) ZipInputStream zis=new ZipInputStream( this.getClass().getResourceAsStream("/leave.zip")); //部署文件 Zip processEngine=Configuration.getProcessEngine(); processEngine.getRepositoryService().createDeployment(). addResourcesFromZipInputStream(zis).deploy(); return "showIndex"; } //remove流程定义========================================================================= private String definitionID;//流程实列id public void setDefinitionID(String definitionID) { this.definitionID = definitionID; } public String removeDeployeJbpm()throws Exception { processEngine.getRepositoryService().deleteDeploymentCascade(definitionID); return "showIndex"; } //用户登陆========================================================================== /* public String userLogin(){ String manager="manager,admin,zhang"; String boss="boss,song"; if("".equals(user.getUsername())||user.getUsername()==null){ return "plassLogin"; }else if(manager.contains(user.getUsername())){ //经理登陆 user.setRole("manager"); }else if(boss.contains(user.getUsername())){ user.setRole("boss"); }else{ user.setRole("user"); } return "showIndex"; }*/ //填写申请单========================================================================== private int day; private String message; public String getMessage() { return message; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public String empAddLeave() throws Exception { //角色员工的参数操作 String role=(String) ServletActionContext.getRequest().getSession().getAttribute("role"); String username=(String) ServletActionContext.getRequest().getSession().getAttribute("username"); Map map=new HashMap(); if(0>=day){ message="请正确填写天数!"; //System.out.println("请填写天数!"); return "showIndex"; } map.put("owner",username); //获取经理,老板 需要用到sql 这里省略 map.put("manager","李经理");//部门经理 map.put("boss","王老板");//老板 map.put("day",day); processEngine.getExecutionService().startProcessInstanceByKey("leave",map); return "showIndex"; } //提交请假单complete========================================================================== private String taskId; private String flog;//用于判断是审批还是驳回 public void setFlog(String flog) { this.flog = flog; } public void setTaskId(String taskId) { this.taskId = taskId; } public String getTaskId() { return taskId; } //经理操作 public String empCompleteLeave() throws Exception { TaskService taskService =processEngine.getTaskService(); Map map=new HashMap(); map.put(actionRole,actionUserName);//传入参数 taskService.setVariables(taskId, map); flog=new String(flog.getBytes("ISO-8859-1"),"utf-8"); if("to 填写申请单".equals(flog)){ //提交请假任务 taskService.completeTask(taskId,"to 经理审批"); }else if("驳回".equals(flog)){ //经理驳回 taskService.completeTask(taskId,"驳回"); }else if("to exclusive1".equals(flog)){ //经理批准 taskService.completeTask(taskId,"to exclusive1"); }if("to end1".equals(flog)){ //老板批准 taskService.completeTask(taskId,"to end1"); } return "showIndex"; } //显示修改请假单========================================================================== public String empUpdateLeave() throws Exception { //根据任务id和变量名获取值 Object getDay= processEngine.getTaskService().getVariable(taskId,"day"); day=Integer.parseInt(getDay.toString()); return "updateJsp"; } //修改请假单提交========================================================================== public String updateLeaveOk() throws Exception { //角色员工的参数操作 String role=(String) ServletActionContext.getRequest().getSession().getAttribute("role"); String username=(String) ServletActionContext.getRequest().getSession().getAttribute("username"); Map map=new HashMap(); if(0>=day){ message="请正确填写天数!"; //System.out.println("请填写天数!"); return "updateJsp"; } map.put("owner",username); //获取经理,老板 需要用到sql 这里省略 map.put("manager","李经理");//部门经理 map.put("boss","王老板");//老板 map.put("day",day); //processEngine.getExecutionService().startProcessInstanceByKey("leave",map); processEngine.getTaskService().setVariables(taskId, map); return "showIndex"; } }