forked from chanjarster/web-async-learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncReadServlet.java
74 lines (61 loc) · 2.24 KB
/
AsyncReadServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package me.chanjar.learning;
import org.apache.commons.lang3.exception.ExceptionUtils;
import javax.servlet.AsyncContext;
import javax.servlet.ReadListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = "/async-read", asyncSupported = true)
public class AsyncReadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Servlet thread: " + Thread.currentThread().getName());
AsyncContext asyncCtx = req.startAsync();
ServletInputStream is = req.getInputStream();
is.setReadListener(new ReadListener() {
private int totalReadBytes = 0;
@Override
public void onDataAvailable() {
System.out.println("ReadListener thread: " + Thread.currentThread().getName());
try {
byte buffer[] = new byte[1 * 1024];
int readBytes = 0;
while (is.isReady() && !is.isFinished()) {
int length = is.read(buffer);
if (length == -1 && is.isFinished()) {
asyncCtx.complete();
System.out.println("Read: " + readBytes + " bytes");
System.out.println("Total Read: " + totalReadBytes + " bytes");
return;
}
readBytes += length;
totalReadBytes += length;
}
System.out.println("Read: " + readBytes + " bytes");
} catch (IOException ex) {
ex.printStackTrace();
asyncCtx.complete();
}
}
@Override
public void onAllDataRead() {
try {
System.out.println("Total Read: " + totalReadBytes + " bytes");
asyncCtx.getResponse().getWriter().println("Finished");
} catch (IOException ex) {
ex.printStackTrace();
}
asyncCtx.complete();
}
@Override
public void onError(Throwable t) {
System.out.println(ExceptionUtils.getStackTrace(t));
asyncCtx.complete();
}
});
}
}