Created
July 5, 2010 05:50
Revisions
-
StephanSchmidt revised this gist
Jul 12, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -73,4 +73,4 @@ private void pagelet(PrintWriter writer, String id, String content) { "</script>\n"); writer.flush(); } } -
StephanSchmidt revised this gist
Jul 12, 2010 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ protected void service(HttpServletRequest req, final HttpServletResponse resp) t String doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"; String head = "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"de\" lang=\"de\"> \n" + "<head> \n" + "<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" /> \n" + "<meta http-equiv=\"Content-language\" content=\"de\" />\n"; @@ -73,5 +73,4 @@ private void pagelet(PrintWriter writer, String id, String content) { "</script>\n"); writer.flush(); } } -
StephanSchmidt renamed this gist
Jul 11, 2010 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
StephanSchmidt renamed this gist
Jul 11, 2010 . 1 changed file with 23 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,14 @@ public class BigPipeServlet extends HttpServlet { private static ExecutorService executor = Executors.newFixedThreadPool(500, new ThreadFactory() { public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName("Service Thread "+ t.getId()); t.setDaemon(true); return t; } }); protected void service(HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final PrintWriter writer = resp.getWriter(); @@ -28,35 +26,34 @@ protected void service(HttpServletRequest req, final HttpServletResponse resp) t writer.write("</HEAD><BODY><div>Progressive Loading"); content(writer, "content1", "content2", "content3", "content4", "content5", "content6"); writer.write("</div>\n"); final Random random = new Random(); List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>(); for (int i = 0; i < 6; i++) { final int id = i + 1; tasks.add(new Callable<Boolean>() { public Boolean call() { try { // One service call is nasty and takes 50sec if (id == 3) { Thread.sleep(50000); } else { Thread.sleep(random.nextInt(2000)); } pagelet(writer, "content" + id, "Wohooo" + id); } catch (InterruptedException e) { return false; } return true; } }); } try { executor.invokeAll(tasks, 1500, TimeUnit.MILLISECONDS); } catch (InterruptedException ignored) { // ignored } @@ -65,15 +62,15 @@ public void run() { private void content(PrintWriter writer, String... contentIds) { for (String id : contentIds) { writer.write("<div id=\"" + id + "\">-</div>\n"); } } private void pagelet(PrintWriter writer, String id, String content) { if (writer.checkError()) return; writer.write("<script>" + "arrived(\"" + id + "\", \"" + content + "\");" + "</script>\n"); writer.flush(); } -
StephanSchmidt renamed this gist
Jul 5, 2010 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; -
StephanSchmidt created this gist
Jul 5, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class BigPipeServlet extends HttpServlet { protected void service(HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final PrintWriter writer = resp.getWriter(); String doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"; String head = "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"de\" lang=\"de\" id=\"facebook\" class=\" no_js\"> \n" + "<head> \n" + "<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" /> \n" + "<meta http-equiv=\"Content-language\" content=\"de\" />\n"; writer.write(doctype); writer.write(head); writer.write("<script type=\"text/javascript\">function arrived(id,text) { var b=document.getElementById(id); b.innerHTML = text; }</script>"); writer.write("</HEAD><BODY><div>Progressive Loading"); content(writer, "content1", "content2", "content3", "content4", "content5", "content6"); writer.write("</div>"); ExecutorService executor = Executors.newFixedThreadPool(6); final Random random = new Random(); for (int i = 0; i < 6; i++) { final int id = i + 1; executor.execute(new Runnable() { public void run() { try { // One service call is nasty and takes 50sec if (id == 3) { Thread.sleep(50000); } else { Thread.sleep(random.nextInt(1700)); } synchronized (writer) { pagelet(writer, "content" + id, "Wohooo" + id); } } catch (InterruptedException e) { // Ignored } } }); } try { executor.shutdown(); executor.awaitTermination (1500, TimeUnit.MILLISECONDS); } catch (InterruptedException ignored) { // ignored } writer.write("</BODY></HTML>"); } private void content(PrintWriter writer, String... contentIds) { for (String id : contentIds) { writer.write("<div id=\"" + id + "\">-</div>"); } } private void pagelet(PrintWriter writer, String id, String content) { if (writer.checkError()) return; writer.write("<script >\n" + "arrived(\"" + id + "\", \"" + content + "\");" + "</script>"); writer.flush(); } }