|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2018, Manorrock.com. All Rights Reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are met: |
| 6 | + * |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | + * POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | +package jakartaee.examples.servlet.asyncservlet; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.PrintWriter; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.concurrent.ArrayBlockingQueue; |
| 32 | +import java.util.concurrent.BlockingQueue; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | +import java.util.concurrent.ScheduledExecutorService; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | +import javax.servlet.AsyncContext; |
| 37 | +import javax.servlet.ServletConfig; |
| 38 | +import javax.servlet.ServletException; |
| 39 | +import javax.servlet.annotation.WebServlet; |
| 40 | +import javax.servlet.http.HttpServlet; |
| 41 | +import javax.servlet.http.HttpServletRequest; |
| 42 | +import javax.servlet.http.HttpServletResponse; |
| 43 | + |
| 44 | +/** |
| 45 | + * The Servlet for the asynchronous servlet example. |
| 46 | + * |
| 47 | + * @author Manfred Riem ([email protected]) |
| 48 | + */ |
| 49 | +@WebServlet(asyncSupported = true, value = "/*") |
| 50 | +public class AsyncServlet extends HttpServlet { |
| 51 | + |
| 52 | + /** |
| 53 | + * Stores the executor service (for async processing). |
| 54 | + */ |
| 55 | + private ScheduledExecutorService executorService; |
| 56 | + |
| 57 | + /** |
| 58 | + * Stores the queue. |
| 59 | + */ |
| 60 | + private BlockingQueue queue; |
| 61 | + |
| 62 | + /** |
| 63 | + * Start the request. |
| 64 | + * |
| 65 | + * @param request the request. |
| 66 | + * @param response the response. |
| 67 | + * @throws IOException when an I/O error occurs. |
| 68 | + * @throws ServletException when a Servlet error occurs. |
| 69 | + */ |
| 70 | + @Override |
| 71 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 72 | + queue.add(request.startAsync()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Initialize the servlet. |
| 77 | + * |
| 78 | + * @param config the servlet configuration. |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public void init(ServletConfig config) { |
| 82 | + executorService = Executors.newScheduledThreadPool(1); |
| 83 | + executorService.scheduleAtFixedRate(() -> { |
| 84 | + ArrayList<AsyncContext> clients = new ArrayList<>(); |
| 85 | + queue.drainTo(clients); |
| 86 | + clients.parallelStream().forEach((AsyncContext context) -> { |
| 87 | + try { |
| 88 | + PrintWriter writer = context.getResponse().getWriter(); |
| 89 | + writer.println("OK"); |
| 90 | + writer.flush(); |
| 91 | + context.complete(); |
| 92 | + } catch (IOException ioe) { |
| 93 | + } |
| 94 | + }); |
| 95 | + }, 0, 2, TimeUnit.SECONDS); |
| 96 | + queue = new ArrayBlockingQueue(1000); |
| 97 | + } |
| 98 | +} |
0 commit comments