想验证一下 JBPM 中的并发和事件监听,于是想了一个实现员工申请购买物品的会签功能。
需要员工所在部门manager,HR_manager,Planning_manager,Purchasing_manager 四个人都同意了就可以获得申请。
在四个manager可以按照任意的顺序来批准该购买申请。
当四个manager 都批准后,任务流转到提出申请的员工,让该员工确认这次申请成功。
为了能够追踪到工作流流转的情况,实现了事件监听机制,每完成一个任务,系统都会给申请人发送一条内部信件。
以下是jbpm.xml 文件内容,在这里,只实现了任务申请人发送了一封提醒邮件和向四个manager 各自发送一封提醒邮件,提示有待办任务需要他们去完成。
<?xml version="1.0" encoding="UTF-8"?>
<process name="Async" xmlns="http://jbpm.org/4.4/jpdl">
<start g="415,17,48,48" name="start1">
<transition to="applyPurchase"/>
</start>
<task assignee="#{purchase_Owner}" form="/SSH/LeaveJSP/Async/purchase_add.jsp" g="397,95,92,52" name="applyPurchase">
<on event="end">
<event-listener class="com.util.PurchaseEventListenner">
<field name="taskMsg"><string value="start on process definition"/></field>
</event-listener>
</on>
<transition to="fork1"/>
</task>
<fork g="424,180,48,48" name="fork1">
<transition to="manager_aprove">
</transition>
<transition to="planning_aprove"/>
<transition to="purchasing_aprove"/>
<transition to="hr_aprove"/>
</fork>
<join g="434,385,48,48" name="join1">
<transition name="to task1" to="task1" g="-44,-18"/>
</join>
<task assignee="#{purchase_manager}" form="leaveAction!to_purchase_manager.action" g="233,279,92,52" name="manager_aprove">
<transition name="to_join1" to="join1" g="-41,-18"/>
</task>
<task assignee="#{purchasing_manager}" form="leaveAction!to_purchasing_manager.action" g="347,283,92,52" name="planning_aprove">
<transition name="to_join1" to="join1" g="-41,-18"/>
</task>
<task assignee="#{planning_manager}" form="leaveAction!to_planning_manager.action" g="453,283,92,52" name="purchasing_aprove">
<transition name="to_join1" to="join1" g="-41,-18"/>
</task>
<task assignee="#{hr_manager}" form="leaveAction!to_hr_manager.action" g="562,284,92,52" name="hr_aprove">
<transition name="to_join1" to="join1" g="-41,-18"/>
</task>
<task name="task1" g="420,474,92,52" assignee="#{purchase_Owner}" form="leaveAction!to_confirm_purchase.action">
<transition name="toend1" to="end1" g="-42,-18"/>
</task>
<end name="end1" g="443,536,48,48"/>
</process>
时间监听类的代码:
PurchaseEventListenner.java
package com.util;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.TaskService;
import org.jbpm.api.history.HistoryTask;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.pvm.internal.history.model.HistoryTaskImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dao.MessageDao;
import com.model.Message;
import com.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.service.MessageService;
import com.service.UserService;
import com.service.imp.MessageServiceImp;
import com.service.imp.UserServiceImp;
public class PurchaseEventListenner implements EventListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private String taskMsg;
private Message message;
private MessageDao messageDao;
private MessageService messageService;
private UserService userService;
private ProcessEngine processEngine;
private HttpSession httpSession;
private TaskService taskService;;
@Override
public void notify(EventListenerExecution execution) throws Exception {
Map session=ActionContext.getContext().getSession();
User user=(User)session.get("user");
processEngine=(ProcessEngine)session.get("processEngine");
if(processEngine==null){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
processEngine=(ProcessEngine) context.getBean("processEngine");
messageService=(MessageService)context.getBean("messageService");
}
messageService=(MessageServiceImp)session.get("messageService");
userService=(UserServiceImp)session.get("userService");
String applyUserName="";
message=new Message();
String purchase_manager=(String) execution.getVariable("purchase_manager");
String purchasing_manager=(String) execution.getVariable("purchasing_manager");
String planning_manager=(String) execution.getVariable("planning_manager");
String hr_manager=(String) execution.getVariable("hr_manager");
message.setFromAccess(true);
message.setFromUser(user);
message.setGroupOrPerson(Message.Message_person);
message.setImportLevel(3);
message.setMsg(user.getUsername()+" : you have apply a purchasing application"+"--------------"+taskMsg);
message.setTitle(taskMsg);
message.setToAccess(true);
message.setMessageType("unread");
Calendar cc=Calendar.getInstance();
message.setSendDate(cc.getTime());
/// message.setToDepa rtment(toDepartment);
message.setToUser(user);
messageService.saveMessage(message);
// send a message to user who apply this application
message.setToUser(userService.findUserByName(purchase_manager));
message.setMsg("you have a purchasing application to approve ....from "+user.getUsername());
messageService.saveMessage(message);
// send a message to manager
//send a message to purchasing_manager
Message message1=new Message();
message1.setToUser(userService.findUserByName(purchasing_manager));
message1.setFromAccess(true);
message1.setFromUser(user);
message1.setGroupOrPerson(Message.Message_person);
message1.setImportLevel(3);
message1.setMsg("you have a purchasing application to approve ....from "+user.getUsername());
message1.setTitle(taskMsg);
message1.setToAccess(true);
message1.setMessageType("unread");
message1.setSendDate(cc.getTime());
messageService.saveMessage(message1);
//send a message to purchasing_manager
Message message2=new Message();
message2.setToUser(userService.findUserByName(planning_manager));
message2.setFromAccess(true);
message2.setFromUser(user);
message2.setGroupOrPerson(Message.Message_person);
message2.setImportLevel(3);
message2.setMsg("you have a purchasing application to approve ....from "+user.getUsername());
message2.setTitle(taskMsg);
message2.setToAccess(true);
message2.setMessageType("unread");
message2.setSendDate(cc.getTime());
messageService.saveMessage(message2);
//send a message to purchasing_manager
Message message3=new Message();
message3.setToUser(userService.findUserByName(hr_manager));
message3.setFromAccess(true);
message3.setFromUser(user);
message3.setGroupOrPerson(Message.Message_person);
message3.setImportLevel(3);
message3.setMsg("you have a purchasing application to approve ....from "+user.getUsername());
message3.setTitle(taskMsg);
message3.setToAccess(true);
message3.setMessageType("unread");
message3.setSendDate(cc.getTime());
messageService.saveMessage(message3);
Message message4=new Message();
message4.setToUser(userService.findUserByName(purchase_manager));
message4.setFromAccess(true);
message4.setFromUser(user);
message4.setGroupOrPerson(Message.Message_person);
message4.setImportLevel(3);
message4.setMsg("you have a purchasing application to approve ....from "+user.getUsername());
message4.setTitle(taskMsg);
message4.setToAccess(true);
message4.setMessageType("unread");
message4.setSendDate(cc.getTime());
messageService.saveMessage(message4);
//send a message to planning_manager
System.out.println("taskMsg----------------"+taskMsg);
}
}