Skip to content

Instantly share code, notes, and snippets.

@StephanSchmidt
Created July 5, 2010 05:50

Revisions

  1. StephanSchmidt revised this gist Jul 12, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion BigPipe.java
    Original 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();
    }
    }
    }
  2. StephanSchmidt revised this gist Jul 12, 2010. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions BigPipe.java
    Original 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\" id=\"facebook\" class=\" no_js\"> \n" +
    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();
    }

    }
  3. StephanSchmidt renamed this gist Jul 11, 2010. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. StephanSchmidt renamed this gist Jul 11, 2010. 1 changed file with 23 additions and 26 deletions.
    49 changes: 23 additions & 26 deletions gistfile1.java → BigPipe in Java with invokeAll
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,14 @@
    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 {

    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>");
    writer.write("</div>\n");

    ExecutorService executor = Executors.newFixedThreadPool(6);
    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;
    executor.execute(new Runnable() {
    public void run() {
    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(1700));
    }
    synchronized (writer) {
    pagelet(writer, "content" + id, "Wohooo" + id);
    Thread.sleep(random.nextInt(2000));
    }
    pagelet(writer, "content" + id, "Wohooo" + id);
    } catch (InterruptedException e) {
    // Ignored
    return false;
    }
    return true;
    }
    });
    }

    try {
    executor.shutdown();
    executor.awaitTermination
    (1500, TimeUnit.MILLISECONDS);
    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>");
    writer.write("<div id=\"" + id + "\">-</div>\n");
    }
    }

    private void pagelet(PrintWriter writer, String id, String content) {
    if (writer.checkError()) return;
    writer.write("<script >\n" +
    writer.write("<script>" +
    "arrived(\"" + id + "\", \"" + content + "\");" +
    "</script>");
    "</script>\n");
    writer.flush();
    }

  5. StephanSchmidt renamed this gist Jul 5, 2010. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion gistfile1.txt → gistfile1.java
    Original 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;
  6. StephanSchmidt created this gist Jul 5, 2010.
    81 changes: 81 additions & 0 deletions gistfile1.txt
    Original 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();
    }

    }