Skip to content

Instantly share code, notes, and snippets.

@cassc
Created June 5, 2014 03:14
Show Gist options
  • Save cassc/deea6850c7ff5046b0ee to your computer and use it in GitHub Desktop.
Save cassc/deea6850c7ff5046b0ee to your computer and use it in GitHub Desktop.
@RequestMapping(value="/pushMsg")
public void pushMsg(HttpServletRequest request, HttpServletResponse response){
long t0 = System.currentTimeMillis();
log.info("Starting ");
Continuation continuation = ContinuationSupport.getContinuation(request);
continuation.setTimeout(5000);
continuation.suspend();
long t1 = System.currentTimeMillis();
log.info("Suspended ");
Integer count=(Integer) continuation.getAttribute("count");
if(count==null){
continuation.setAttribute("count", 0);
count=0;
}else{
// continuation.resume();
}
long t2 = System.currentTimeMillis();
PrintWriter pw =null;
try {
pw = response.getWriter();
response.setContentType("text/plain");
pw.write("msg: "+count++);
// TimeUnit.SECONDS.sleep(5);
continuation.setAttribute("count", count);
log.info("Sending msg "+(count-1)+", expired? "+continuation.isExpired());
long t3 = System.currentTimeMillis();
if(count>10){
continuation.complete();
log.info("stopping continuation");
}
} catch (Exception e) {
log.error("",e);
if(pw!=null){
pw.write("error");
}
continuation.complete();
long t4 = System.currentTimeMillis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment