BigPipe要点
来源:互联网 发布:淘宝客推广找不到宝贝 编辑:程序博客网 时间:2024/06/05 15:26
BigPipe是facebook的研究科学家Changhao Jiang发明的一种快速加载网页的方法。
1. 页面分块(pagelet)
2. 服务端先输出给客户端一个不完整的框架性的HTML页面(没有结尾的</body>),里面有表示分块的占位符(比如空的div)
3. 服务端可以并发地生成每个pagelet的内容,每生成完一个立即flush回浏览器(flush的内容为JS),页面可以立即执行
3.1 生成JS而不是HTML,可以不必受页面顺序的限制,任何一个pagelet都可以先返回。
4. BigPipe对搜索引擎不友好,出于SEO的考虑,如果服务器判断user-agent为爬虫,或浏览器不支持JS,则用普通方式输出。
服务端代码例子如下:(来自:http://my.oschina.net/hanshubo/blog/130713)
在使用队列方面没有仔细斟酌,随便找一个过来,就用了。
注意一点,就是不要把 PrintWriter 的实例对象拿到多线程里去用,否则会出莫名其妙的异常。
import java.io.IOException;import java.io.PrintWriter;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class BigPipeServlet extends HttpServlet { private static ExecutorService executor = Executors.newFixedThreadPool(50); public void service(HttpServletRequest request, HttpServletResponse response) throws IOException { final ArrayBlockingQueue<String> q = new ArrayBlockingQueue<String>(6); for (int i = 0; i < 6; i++) { final int id = i + 1; executor.execute(new Runnable() { public void run() { try { Thread.sleep((int) (Math.random() * 10000)); q.put(pagelet("content" + id, "Wohooo" + id)); } catch (InterruptedException e) { } } }); } response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); out.println("<html><head>" + "<script type=\"text/javascript\">function arrived(id,text) { var b=document.getElementById(id); b.innerHTML = text; }</script>" + "</head><body>" + "<div>Progressive Loading"); content(out, "content1", "content2", "content3", "content4", "content5", "content6"); out.println("</div>"); for (int i = 0; i < 6; i++) { try { out.println(q.take()); out.flush(); } catch (InterruptedException e) { e.printStackTrace(); } } out.println("</body></html>"); }
0 0
- BigPipe要点
- BigPipe
- bigpipe
- 关于Bigpipe
- 白话BigPipe
- 白话BigPipe
- 白话BigPipe
- BigPipe介绍
- BigPipe笔记
- BigPipe facebook
- facebook bigpipe
- BigPipe学习研究
- BigPipe学习研究
- BigPipe 好文收录
- 白话Facebook BigPipe
- BigPipe学习研究
- 数据加载的bigpipe
- BigPipe学习研究
- 【HP 打印机】安装 Windows 7 中的网络打印机时收到错误消息:"0x0000052e"
- HDU - 2476 String painter
- GPIB
- 王家林 第四讲Hadoop图文训练课程:实战构建真正的Hadoop分布式集群环境
- 由后序和中序遍历得到先序遍历
- BigPipe要点
- GPIB_Demo
- linux man
- VC_GPIB_Demo
- 用html实现页面跳转
- Perl的技巧总结
- Apache+Tomcat中Cas Server 3.4和CasClient3.2的配置
- 王家林的“云计算分布式大数据Hadoop实战高手之路---从零开始”的第五讲Hadoop图文训练课程:解决典型Hadoop分布式集群环境搭建问题
- 接着c语言