Kev*_*vin 1 java asynchronous comet java-ee servlet-3.0
我无法为用户建立AsyncContexts并使用它们向他们推送通知.在页面加载时,我有一些jQuery代码来发送请求:
$.post("TestServlet",{
action: "registerAsynchronousContext"
},function(data, textStatus, jqXHR){
alert("Server received async request"); //Placed here for debugging
}, "json");
Run Code Online (Sandbox Code Playgroud)
在"TestServlet"中,我在doPost方法中有这个代码:
HttpSession userSession = request.getSession();
String userIDString = userSession.getAttribute("id").toString();
String paramAction = request.getParameter("action");
if(paramAction.equals("registerAsynchronousContext"))
{
AsyncContext userAsyncContext = request.startAsync();
HashMap<String, AsyncContext> userAsynchronousContextHashMap = (HashMap<String, AsyncContext>)getServletContext().getAttribute("userAsynchronousContextHashMap");
userAsynchronousContextHashMap.put(userIDString, userAsyncContext);
getServletContext().setAttribute("userAsynchronousContextHashMap", userAsynchronousContextHashMap);
System.out.println("Put asynchronous request in global map");
}
//userAsynchronousContextHashMap is created by a ContextListener on the start of the web-app
Run Code Online (Sandbox Code Playgroud)
但是,根据Opera Dragonfly(像Firebug这样的调试工具),服务器在发送请求后大约30000ms会发送一个HTTP 500响应.
使用userAsyncContext.getResponse().getWriter().print(SOME_JSON)创建的任何响应,并在浏览器未收到HTTP 500响应之前发送,我不知道为什么.只有当处理AsyncContext的"if"语句中的所有代码都不存在时,浏览器才会使用常规响应对象发送响应(response.print(SOME_JSON)).
有人可以帮我吗?我有一种感觉,这是因为我误解了异步API的工作原理.我认为我可以将这些AsyncContexts存储在全局映射中,然后检索它们并使用它们的响应对象将内容推送到客户端.但是,似乎AsyncContexts不能写回客户端.
任何帮助都会被贬低.
我解决了这个问题.好像我的方法有几个问题:
在Glassfish中,AsyncContext对象的默认超时时间均为30,000毫秒(.5分钟).一旦此期限到期,整个响应将提交给客户端,这意味着您将无法再次使用它.
如果您正在实施长轮询,这可能不是什么大问题(因为您最终会在响应之后发送另一个请求),但是如果您希望实现流式传输(将数据发送回客户端而不提交响应)你要么增加超时,要么一起摆脱它.这可以使用AsyncContext的.setTimeout()方法完成.请注意,虽然规范声明:"超时值为零或更少表示没有超时.",Glassfish(此时)似乎将0解释为"需要立即响应",并将任何负数解释为"无超时".
如果您正在实现流式传输,则必须使用printwriter的.flush()方法在使用其.print() .println()或.write()方法写入数据后将数据推送到客户端.
在客户端,如果您已对数据进行流式处理,则会触发readyState为3("交互式",这意味着浏览器正在接收响应).如果您使用的是jQuery,则没有简单的方法来处理3的readyStates,因此如果您正在实现流式传输,那么您将不得不恢复为常规Javascript以发送请求并处理响应.