GeezerVison is showing github.com — updated · MarcosDavidP/Java@6c45a9c
File tree
Expand file treeCollapse file tree
Open diff view settings
java-async-servlet/src/main
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: java-async-servlet/src/main/java/com/hmkcode/AsyncServlet.java +19-4Lines changed: 19 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
15 | 15 | @WebServlet(name="asyncServlet",value = {"/async"},asyncSupported = true) |
16 | 16 | public class AsyncServlet extends HttpServlet |
17 | 17 | { |
| 18 | + public static int index = 0; |
| 19 | + |
| 20 | + public AsyncServlet(){ |
| 21 | + System.out.println("AsyncServlet - "+index++); |
| 22 | + } |
18 | 23 |
|
19 | 24 | private static final long serialVersionUID = 1L; |
20 | 25 |
|
| 26 | +String param =""; |
21 | 27 | @Override |
22 | 28 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
23 | 29 | throws ServletException, IOException { |
24 | 30 |
|
| 31 | + |
25 | 32 | final AsyncContext ctx = req.startAsync(); |
| 33 | + param = ctx.getRequest().getParameter("seq"); |
| 34 | + |
26 | 35 |
|
27 | | - ctx.setTimeout(3000); |
| 36 | + ctx.setTimeout(0); |
28 | 37 |
|
29 | 38 | ctx.addListener(new AsyncListener() { |
30 | 39 |
|
@@ -49,19 +58,25 @@ public void onComplete(AsyncEvent arg0) throws IOException { |
49 | 58 | } |
50 | 59 | }); |
51 | 60 |
|
52 | | - |
53 | 61 | ctx.start(new Runnable() { |
54 | 62 | @Override |
55 | 63 | public void run() { |
56 | | - // TODO Auto-generated method stub |
57 | 64 | try { |
58 | | - ctx.getResponse().getWriter().write("Async Started..."); |
| 65 | + |
| 66 | + |
| 67 | + while(!param.equals("exit")){ |
| 68 | + |
| 69 | + } |
| 70 | + ctx.getResponse().getWriter().write(ctx.getRequest().getParameter("seq")); |
| 71 | + |
59 | 72 | } catch (IOException e) { |
60 | 73 | e.printStackTrace(); |
61 | 74 | } |
62 | 75 |
|
63 | 76 | ctx.complete(); |
64 | 77 | } |
| 78 | + |
| 79 | + |
65 | 80 | }); |
66 | 81 | } |
67 | 82 | } |
Collapse file
Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package com.hmkcode; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import javax.servlet.ServletException; |
| 5 | +import javax.servlet.annotation.WebServlet; |
| 6 | +import javax.servlet.http.HttpServlet; |
| 7 | +import javax.servlet.http.HttpServletRequest; |
| 8 | +import javax.servlet.http.HttpServletResponse; |
| 9 | + |
| 10 | + |
| 11 | +@WebServlet(name="syncServlet",value = {"/sync"}) |
| 12 | +public class SyncServlet extends HttpServlet |
| 13 | +{ |
| 14 | + public static int index = 0; |
| 15 | + |
| 16 | + public SyncServlet(){ |
| 17 | + System.out.println("SyncServlet - "+index++); |
| 18 | + } |
| 19 | + |
| 20 | +private static final long serialVersionUID = 1L; |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 24 | + throws ServletException, IOException { |
| 25 | + |
| 26 | + |
| 27 | + String seq = req.getParameter("seq"); |
| 28 | + |
| 29 | + try { |
| 30 | + Thread.sleep(50 * Integer.parseInt(seq)); |
| 31 | + } catch (NumberFormatException e) { |
| 32 | + e.printStackTrace(); |
| 33 | + } catch (InterruptedException e) { |
| 34 | + e.printStackTrace(); |
| 35 | + } |
| 36 | + resp.getWriter().write(seq); |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + } |
| 41 | +} |
Collapse file
Copy file name to clipboard +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +<?xml version="1.0" encoding="UTF-8"?> |
| 2 | +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 3 | +xmlns="http://java.sun.com/xml/ns/javaee" |
| 4 | +xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" |
| 5 | +xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" |
| 6 | +id="WebApp_ID" version="3.0"> |
| 7 | + <display-name>java async servlet</display-name> |
| 8 | + <welcome-file-list> |
| 9 | + <welcome-file>index.html</welcome-file> |
| 10 | + </welcome-file-list> |
| 11 | + |
| 12 | +</web-app> |