前一篇讲了前端利用ajax轮询查询请求进度,下面我们来说下,后台怎么处理。废话不多说,上代码:
首先,定义好接口规则:
public interface CheckFilter {
/**
* 检查订单
*
* @param eorder
* @return
*/
boolean check(CheckInvocation invocation);
/**
* 获取错误信息
*
* @return
*/
String getErrorMessage();
}
此接口定义了检测类的实现规范,以及提示信息。然后定义默认的实现类:
package com.zxtx.apps.traffic.eorder.check.impl;
import com.zxtx.apps.traffic.DangerEnterprise;
import com.zxtx.apps.traffic.DangerVehicle;
import com.zxtx.apps.traffic.DangerWorker;
import com.zxtx.apps.traffic.Enterprise;
import com.zxtx.apps.traffic.VehicleLicense;
import com.zxtx.apps.traffic.eorder.Eorder;
import com.zxtx.apps.traffic.eorder.EorderGoods;
import com.zxtx.apps.traffic.eorder.check.CheckFilter;
import com.zxtx.apps.traffic.eorder.check.CheckInvocation;
import org.llama.library.utils.ClassUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Created by Tonny on 2016/10/25.
*/
public class DefaultCheckInvocation implements CheckInvocation {
private Eorder eorder;
private DangerVehicle tractor;
private VehicleLicense vehicleLicense;
private DangerVehicle trailer;
private VehicleLicense trailerLicense;
private DangerWorker driver;
private DangerWorker supercargo;
private List<EorderGoods> eorderGoods;
private DangerEnterprise dangerEnterprise;
private Iterator<CheckFilter> checkFilterIterator;
private boolean executed = false;
private String filterClasses;
private String errorMessage;
private List<String> progess = new ArrayList<String>();
@Override
public Eorder getEorder() {
return eorder;
}
@Override
public DangerVehicle getTractor() {
return tractor;
}
@Override
public DangerVehicle getTrailer() {
return trailer;
}
@Override
public DangerWorker getDriver() {
return driver;
}
@Override
public DangerWorker getSupercargo() {
return supercargo;
}
@Override
public VehicleLicense getVehicleLicense() {
return vehicleLicense;
}
@Override
public VehicleLicense getTrailerLicense() {
return trailerLicense;
}
@Override
public List<EorderGoods> getEorderGoods() {
// TODO Auto-generated method stub
return eorderGoods;
}
public void setEorder(Eorder eorder) {
this.eorder = eorder;
}
public void setTractor(DangerVehicle tractor) {
this.tractor = tractor;
}
public void setTrailer(DangerVehicle trailer) {
this.trailer = trailer;
}
public void setDriver(DangerWorker driver) {
this.driver = driver;
}
public void setSupercargo(DangerWorker supercargo) {
this.supercargo = supercargo;
}
public void setFilterClasses(String filterClasses) {
this.filterClasses = filterClasses;
}
public void setVehicleLicense(VehicleLicense vehicleLicense) {
this.vehicleLicense = vehicleLicense;
}
public void setTrailerLicense(VehicleLicense trailerLicense) {
this.trailerLicense = trailerLicense;
}
public void setEorderGoods(List<EorderGoods> eorderGoods) {
this.eorderGoods = eorderGoods;
}
@Override
public List<String> getProgess() {
return progess;
}
@Override
public String getErrorMessage() {
return errorMessage;
}
public void setProgess(List<String> progess) {
this.progess = progess;
}
public boolean isExecuted() {
return executed;
}
public void setExecuted(boolean executed) {
this.executed = executed;
}
@Override
public DangerEnterprise getDangerEnterprise() {
return dangerEnterprise;
}
public void setDangerEnterprise(DangerEnterprise dangerEnterprise) {
this.dangerEnterprise = dangerEnterprise;
}
@Override
public boolean invoke() {
if (executed) {
return false;
}
if (checkFilterIterator.hasNext()) {
CheckFilter filter = checkFilterIterator.next();
boolean result = filter.check(this);
if (!result) {
if(errorMessage==null){
errorMessage = filter.getErrorMessage();
}
return false;
}
}
executed = true;
return true;
}
@Override
public void init() {
if (checkFilterIterator == null) {
String[] c = filterClasses.split(",");
List<CheckFilter> fs = new ArrayList<CheckFilter>();
for (String s : c) {
CheckFilter o = ClassUtils.newInstance(s);
fs.add(o);
}
this.checkFilterIterator = fs.iterator();
}
}
}
默认实现类,实例化需要检测的类对象,并实现检测规则。发送检测请求
public String checkEorder(){
session.remove("defaultCheckInvocation");
initCheck();//注意这行代码初始化方法
session.put("defaultCheckInvocation", defaultCheckInvocation);
defaultCheckInvocation.invoke();//注意这行代码
ret = true;
return SUCCESS;
}
获取轮询请求:
public String getCheckProgress(){
while(true){
defaultCheckInvocation = (DefaultCheckInvocation) session.get("defaultCheckInvocation");
if(defaultCheckInvocation!=null){
break;
}
}
progessList = defaultCheckInvocation.getProgess();
//出现错误信息
if(defaultCheckInvocation.getErrorMessage()!=null){
progessList.add("-1");
progessList.add(defaultCheckInvocation.getErrorMessage());
session.remove("defaultCheckInvocation");
}
if(defaultCheckInvocation.isExecuted()){
progessList.add("0");
session.remove("defaultCheckInvocation");
}
defaultCheckInvocation.setProgess(new ArrayList<String>());
return SUCCESS;
}
下面就是各个检测的实现类:
public class WorkerExistCheck implements CheckFilter{
@Override
public boolean check(CheckInvocation invocation) {
// TODO Auto-generated method stub
//获取人员
DangerWorker worker = invocation.getDriver();
//获取押运员
DangerWorker supercargo = invocation.getSupercargo();
if(worker==null||supercargo==null){
return false;
}
invocation.getProgess().add("是否配备驾驶员和押运员检测完毕");
return invocation.invoke();
}
@Override
public String getErrorMessage() {
// TODO Auto-generated method stub
return "未配备驾驶员或者押运员";
}
}