Java Servlet 3.0 push server: sending data multiple times using the same AsyncContext

Give some examples and answers to them (mainly http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=3 ), I want the server to send a response several times to the client without completing the request. When the request expires, I create another one, etc.

I want to avoid a lengthy poll, because I need to update the request every time I get a response. (and this is not at all what the asynchronous features of servlet 3.0 are aimed at).

I have this on the server side:

@WebServlet(urlPatterns = {"/home"}, name = "async", asyncSupported = true) public class CometServlet extends HttpServlet { public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { AsyncContext ac = request.startAsync(request, response); HashMap<String, AsyncContext> store = AppContext.getInstance().getStore(); store.put(request.getParameter("id"), ac); } } 

And a stream for writing to an asynchronous context.

 class MyThread extends Thread { String id, message; public MyThread(String id, String message) { this.id = id; this.message = message; } public void run() { HashMap<String, AsyncContext> store = AppContext.getInstance().getStore(); AsyncContext ac = store.get(id); try { ac.getResponse().getWriter().print(message); } catch (IOException e) { e.printStackTrace(); } } } 

But when I make a request, the data is sent only if I call ac.complete() . Without this request, there will always be a timeout. So basically, I want the data to β€œoverflow” before the request completes.

To make a note, I tried this with the Jetty 8 Continuation API , I also tried printing with OutputStream instead of PrintWriter . I also tried flushBuffer() when responding. Same.

What am I doing wrong?

The client side is as follows:

  var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://localhost:8080/home', true); xhr.onreadystatechange = function () { if (xhr.readyState == 3 || xhr.readyState == 4) { document.getElementById("dynamicContent").innerHTML = xhr.responseText; } } xhr.send(null); 

Can someone at least confirm that the server side is ok? :)

+3
source share
2 answers

Your server and client code is really fine. Actually, the problem is with buffering the text / plain browser from your web server. It is for this reason that you do not see this problem when using curl.

I took your client side code and I was able to see incremental responses with only one slight change:

 response.setContentType("text/html"); 

Incremental responses appeared immediately, regardless of their size.

Without this setting, when my output was a small message, it was considered text / regular and did not appear immediately on the client. As I added client responses more and more, it accumulated until the buffer size reached about 1024 bytes , and then all this appeared on the client side. However, after this moment small increments appeared immediately (there was no more accumulation).

+8
source

I know this is a bit outdated, but you can just drop the buffer on the answer.

0
source

All Articles