今天在维护项目的时候遇到一个问题,先不说问题,先说是什么功能。
功能大概就是员工在微信上提一个请假申请,填写完具体项之后点提交,会提示提交成功,同时,会给该员工的审批领导发个微信消息说谁谁谁提交了什么什么申请,需要您审批,大致功能就是这样。
然后问题是什么呢,问题就在员工点击提交之后,到看到申请提交成功字样这中间的时间很长很长,大概有5、6秒的样子,更慢的时候能达到8秒,所以这会给用户带来很不好的体验,用户以为是自己的手机死机了,会点很多次提交,实际上在点第一次提交之后,校验过后就已经保存到数据库了。
后来查找原因之后发现,并不是执行存储逻辑的时候速度慢,而是走到发消息给审批人的时候慢了,这是这一行代码:
//向微信端审批人发送消息
//组织文本内容
flowEntity.applySendWX(apply, superiors);
想了一些办法,最后想着是把这行代码单独放到一个线程里,也就是另起一个线程,当程序运行到此处的时候另起一个线程负责跑这段代码,为什么要这样做呢?因为发微信消息给审批人跟保存申请的内容是不冲突的,发消息不关心是够提交申请成功,也就是说,你该提申请提申请,你提完我另起个线程去发消息,我先让你看到你提的申请是成功了还是失败了再说,因为用户又看不到你发消息给审批人,用户只关心我提的申请成功没成功,至于你发不发消息,这我就不管了。
确定解决办法之后就开始搞吧,之前是学过线程,但一直到现在都没用过,所以连搜索带摸索,总算搞出来了,其实也挺简单的,就是搞一个Thread类而已:
/**
* @author wangweiyou
* @date 2016-4-28 上午9:53:47
*/
package com.jiu.thread;
import java.util.List;
import com.jiu.attendance.entity.FlowEntity;
import com.jiu.attendance.po.Apply;
import com.jiu.sys.po.User;
import com.jiu.util.SpringHelper;
/**
*
* @author wangweiyou
* @date 2016-4-28 上午9:53:47
*/
public class ApplyThread extends Thread{
private FlowEntity flowEntity = (FlowEntity) SpringHelper.getBean("flowEntity");
private Apply apply;
private List<User> superiors;
/**
* @author wangweiyou
* @date 2016-4-28 上午10:19:02
*/
public ApplyThread(Apply apply, List<User> superiors) {
super();
this.apply = apply;
this.superiors = superiors;
}
/**
* @author wangweiyou
* @date 2016-4-28 上午9:56:36
*/
@Override
public void run() {
flowEntity.applySendWX(apply, superiors);
}
public Apply getApply() {
return apply;
}
public void setApply(Apply apply) {
this.apply = apply;
}
public List<User> getSuperiors() {
return superiors;
}
public void setSuperiors(List<User> superiors) {
this.superiors = superiors;
}
}
把要执行的那行代码放到该类的run方法里,然后在你自己的业务类里面调用一下该线程就行了:
@POST
@Path("/leaveApplication")
@Produces( { MediaType.APPLICATION_JSON})
public String leaveApplication(String param) {
JSONObject returnObj = new JSONObject();
try {
JSONObject paramJson = JSONObject.fromObject(param);
//获取code 根据code获取微信用户员工编号
String code = JsonUtil.getString(paramJson, "code");
//String ygbh = WeixinUtil.getUser(code);
if(StringUtils.isNotBlank(code)){
User user = userEntity.getUserByJobNo(code);
if(user != null){
//获取当前用户的审批人
List<User> superiors = flowEntity.getSuperiors(user);
if(superiors==null || superiors.size()<=0){//没有分配上级,不能提交申请
returnObj.put("flag", FlagEnum.FAIL.getCode());
returnObj.put("msg", "没有分配上级,不能提交申请!");
}else{
Apply apply = new Apply();
apply.setCode(applyEntity.getOrderNumber("QJSQ"));
apply.setDocumentsTime(new Date());
apply.setApplicant(user);
int applyTypeCode = JsonUtil.getInt(paramJson, "applyTypeCode");
String applyDetailTypeId = JsonUtil.getString(paramJson, "applyDetailTypeId");
String feedRuleId = JsonUtil.getString(paramJson, "feedRuleId");
String beginTime = JsonUtil.getString(paramJson, "beginTime");
String endTime = JsonUtil.getString(paramJson, "endTime");
String reasons = JsonUtil.getString(paramJson, "reasons");
apply.setApplyType(applyTypeEntity.getApplyTypeByCode(applyTypeCode));
apply.setApplyDetailType(applyDetailTypeEntity.getApplyDetailTypeById(applyDetailTypeId));
if(StringUtils.isBlank(feedRuleId)){//如果不是哺乳假规则
apply.setFeedRule(null);
apply.setBeginTime(DateUtil.getDate(beginTime, "yyyy-MM-dd HH:mm"));
apply.setEndTime(DateUtil.getDate(endTime, "yyyy-MM-dd HH:mm"));
}else{//如果是哺乳假规则
apply.setFeedRule(feedRuleEntity.getFeedRuleById(feedRuleId));
apply.setBeginTime(DateUtil.getDate(beginTime, "yyyy-MM-dd"));
apply.setEndTime(DateUtil.getDate(endTime, "yyyy-MM-dd"));
}
apply.setReasons(reasons);
apply.setStatus(0);
apply.setOperateMode(1);//微信方式
String id = applyEntity.addApply(apply);
apply = applyEntity.getApplyById(id);
// 向Message表中添加相应记录
for(int i=0;i<superiors.size();i++){
Message message = new Message();
message.setApply(apply);
message.setReceive(superiors.get(i));
message.setType(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
message.setMsg(user.getUsername() + "于"
+ sdf.format(new Date()) + "时提交了申请,需由"
+ superiors.get(i).getUsername() + "审批");
messageEntity.addMessage(message);
}
//向Attendance表中添加或更新记录
if(feedRuleId==null||feedRuleId.equals("")){//如果不是哺乳假规则
attendanceEntity.updateAttendanceByApply(beginTime,endTime,user,AttendanceEnum.LEAVE2.getCode());
}
returnObj.put("flag", FlagEnum.SUCCESS.getCode());
returnObj.put("msg", "申请成功!");
Log log = new Log();
log.setUserId(user.getId());
log.setUserName(user.getUsername());
log.setModuleName("请假申请管理");
log.setOperationType(OperationType.ADD.getCode());
log.setOperationTime(new Date());
log
.setRemark("用户" + user.getUsername() + "提交申请:"
+ apply.getCode());
logEntity.addLog(log);
//向微信端审批人发送消息
//组织文本内容
// flowEntity.applySendWX(apply, superiors);
ApplyThread lah = new ApplyThread(apply, superiors);
lah.start();
}
}else{
returnObj.put("flag", FlagEnum.FAIL.getCode());
returnObj.put("msg", "提交申请失败!");
}
}else{
returnObj.put("flag", FlagEnum.FAIL.getCode());
returnObj.put("msg", "提交申请失败!");
}
} catch (Exception e) {
returnObj.put("flag", FlagEnum.FAIL.getCode());
returnObj.put("msg", "提交申请失败!");
}
return returnObj.toString();
}