diff --git a/examples/async-rest/async-rest-jar/src/main/java/org/eclipse/jetty/example/asyncrest/AsyncRestServlet.java b/examples/async-rest/async-rest-jar/src/main/java/org/eclipse/jetty/example/asyncrest/AsyncRestServlet.java index df1f88d24a4..bcd15a6edb9 100644 --- a/examples/async-rest/async-rest-jar/src/main/java/org/eclipse/jetty/example/asyncrest/AsyncRestServlet.java +++ b/examples/async-rest/async-rest-jar/src/main/java/org/eclipse/jetty/example/asyncrest/AsyncRestServlet.java @@ -25,7 +25,6 @@ import java.util.Map; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicInteger; - import javax.servlet.AsyncContext; import javax.servlet.ServletConfig; import javax.servlet.ServletException; @@ -62,7 +61,7 @@ public class AsyncRestServlet extends AbstractRestServlet public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); - + _client = new HttpClient(); try @@ -79,19 +78,19 @@ public class AsyncRestServlet extends AbstractRestServlet protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Long start=System.nanoTime(); - + // Do we have results yet? Queue> results = (Queue>) request.getAttribute(RESULTS_ATTR); - + // If no results, this must be the first dispatch, so send the REST request(s) if (results==null) { // define results data structures - final Queue> resultsQueue = new ConcurrentLinkedQueue>(); + final Queue> resultsQueue = new ConcurrentLinkedQueue<>(); request.setAttribute(RESULTS_ATTR, results=resultsQueue); - + // suspend the request - // This is done before scheduling async handling to avoid race of + // This is done before scheduling async handling to avoid race of // dispatch before startAsync! final AsyncContext async = request.startAsync(); async.setTimeout(30000); @@ -99,7 +98,7 @@ public class AsyncRestServlet extends AbstractRestServlet // extract keywords to search for String[] keywords=request.getParameter(ITEMS_PARAM).split(","); final AtomicInteger outstanding=new AtomicInteger(keywords.length); - + // Send request each keyword for (final String item:keywords) { @@ -119,19 +118,19 @@ public class AsyncRestServlet extends AbstractRestServlet } }); } - + // save timing info and return request.setAttribute(START_ATTR, start); - request.setAttribute(DURATION_ATTR, new Long(System.nanoTime() - start)); + request.setAttribute(DURATION_ATTR, System.nanoTime() - start); return; } // We have results! - + // Generate the response String thumbs = generateThumbs(results); - + response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); @@ -145,17 +144,17 @@ public class AsyncRestServlet extends AbstractRestServlet long total=now-start0; long generate=now-start; long thread=initial+generate; - + out.print("Asynchronous: "+request.getParameter(ITEMS_PARAM)+"
"); out.print("Total Time: "+ms(total)+"ms
"); out.print("Thread held (red): "+ms(thread)+"ms (" + ms(initial) + " initial + " + ms(generate) + " generate )
"); out.print("Async wait (green): "+ms(total-thread)+"ms
"); - + out.println(""+ ""+ ""); - + out.println("
"); out.println(thumbs); out.println(""); @@ -163,21 +162,11 @@ public class AsyncRestServlet extends AbstractRestServlet out.close(); } - private abstract class AsyncRestRequest implements Response.Listener + private abstract class AsyncRestRequest extends Response.Listener.Empty { final Utf8StringBuilder _content = new Utf8StringBuilder(); - + AsyncRestRequest() - { - } - - @Override - public void onBegin(Response response) - { - } - - @Override - public void onHeaders(Response response) { } @@ -188,19 +177,9 @@ public class AsyncRestServlet extends AbstractRestServlet _content.append(bytes,0,bytes.length); } - @Override - public void onSuccess(Response response) - { - } - - @Override - public void onFailure(Response response, Throwable failure) - { - } - @Override public void onComplete(Result result) - { + { // extract auctions from the results Map query = (Map) JSON.parse(_content.toString()); Object[] auctions = (Object[]) query.get("Item"); @@ -215,14 +194,12 @@ public class AsyncRestServlet extends AbstractRestServlet abstract void onAuctionFound(Map details); abstract void onComplete(); - - }; + } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } - }