针对于手机端访问服务器端,中间件设置相应等待时间问题。
如果某个方法执行时间过长,超过等待时间会自动跳转到错误页面。针对此种情况可以将后台方法放到一个线程中,当访问后台时启动线程 执行方法。前台通过ajax轮询方式获取线程中方法的执行结果。
1.
public class XyThread {
//线程ID
private String id;
//线程状态(
//1执行中
//2 执行结束
private String status="1";
//执行结果
private String result;
}
2.
public class RunnableUtil implements Runnable {
private Thread t;
//线程名称
private String threadName;
private Map<String,XyThread> runningPublishTheads;
//当前session
private HttpSession session;
//当前的request
private HttpServletRequest request;
public RunnableUtil(Map<String, XyThread> runningPublishTheads, String threadName, HttpSession session) {
super();
this.runningPublishTheads = runningPublishTheads;
this.threadName = threadName;
this.session = session;
}
@Override
public void run() {
System.out.println("成功进入了线程的run方法");
System.out.println(runningPublishTheads.get("1"));
System.out.println(t.getId());
XyThread xyThread = new XyThread();
xyThread.setId(t.getId()+"");
xyThread.setStatus("1");
runningPublishTheads.put("1", xyThread);
}
public void start () {
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}