GithubHelp home page GithubHelp logo

tempbottle / bigpipe Goto Github PK

View Code? Open in Web Editor NEW

This project forked from crazysoftwarecoder/bigpipe

0.0 1.0 0.0 228 KB

A implementation of Facebook's bigpipe for the Java web platform

Java 100.00%

bigpipe's Introduction

Build Status

A BigPipe implementation for Java

A java implementation of bigpipe technology developed originally at Facebook.

Architecture

Imgur

The flow of a request goes like this:

  1. The client browser sends a HTTP request.
  2. A BigPipeDispatcherServlet(Front Controller) receives the request, and starts multiple tasks (also called as Pagelet Tasks).
  3. The Pagelet tasks start fetching data for different pagelets from the EIS/Internet data sources.
  4. Immediately after 2, the JSP to churn out presentation markup (HTML) is also cranked up.
  5. The JSP receives all the pagelet data asynchronously and flushes out HTML markup over the wire, along with javascript code to paint placeholders on the page. During this time, the server keeps the HTTP connection open.
  6. The client receives the javascript code + content (HTML, CSS + more javascript) and paints placeholders on the page appropriately.
  7. The server finally closes the HTTP connection.

Note - The execution of the JSP is still blocking (i.e. the first pagelet on the page will still block - waiting for its data to come back, and then it will go to the second pagelet, and so on). This will be changed to asynchronous execution in the very near future (Submit a patch if you'd like to contribute). However, it is good to note that such a restriction will give a pleasant user experience to visitors (as they see the header loading first, then the middle components, and then the footer in a top down fashion).

Prerequisites

  • You are running a JSP/Servlet container (Apache Tomcat)
  • You are running atleast JDK 7

Installation

git clone https://github.com/crazysoftwarecoder/bigpipe.git
cd bigpipe
mvn clean install

Import maven dependency in your pom:

<dependency>
  <groupId>org.myseriousorganization.bigpipe</groupId>
  <artifactId>bigpipe-java-web</artifactId>
  <version>0.0.1</version>
</dependency>

Usage

Step 1

Define your pagelet task methods (APIs that provide data to independent pagelets)

If ClassA.doStuff will serve a pagelet's data, then do the following

@PageletTask(name="leftNavigationBar") // Lets assume this serves the left navigation bar of a website.
public ClassA {

  /**
   * Annotate the method that will fetch the data for the pagelet.
   * This method will be called in a separate thread so that it
   * can be parallelized.
   *
   * The ONLY argument to this method must be a <b>HttpServletRequest</b>
   * Remember for the developer who is using this, this is the 
   * Servlet and the response is the <b>ViewObject</b>
   */
  @PageletTaskMethod // Annotate the method that will fetch the data for you.
  public ViewObject doStuff(HttpServletRequest servletRequest) {
    // Type code to transform the request into the 
    // response here, optionally accessing all resources to
    // help achieve that.
  }
}

###Step 2

Define the return ViewObject for each @PageletTaskMethod

class LeftNavBarDataVO implements ViewObject { // ViewObject is just a marker interface. It does not have anything.

  // Add your properties and getter and setter methods here that return
  // data for the pagelet to the JSP

  private String categoryName;
  
  public String getCategoryName() {
    return categoryName;
  }
  
  public void setCategoryName(String categoryName) {
  
  }
}

###Step 3

In your JSP

<%@ taglib prefix="bigpipe" uri="http://www.github.com/crazysoftwarecoder/bigpipe"%>
<%@ page isELIgnored="false" %>

<html>
<head></head>
<body>
	<bigpipe:enablePagelets /> <!-- Enable bigpipe tech for this JSP here -->

	<bigpipe:pagelet name="leftModule" viewObject="item"> <!-- The name is the one in @PageletTask and the viewObject is the variable name that you want to use in the pagelet to get at the data object -->
		${item.categoryName} <!-- Get at the property of the ViewObject -->
	</bigpipe:pagelet>

  <!-- A second pagelet -->
	<bigpipe:pagelet name="rightModule" viewObject="item">
		${item.moduleName}
	</bigpipe:pagelet>

  <!-- Add more pagelets here -->

</body>
</html>

###Step 4

Then finally in your web.xml

	<servlet>
		<servlet-name>HomePageServingServlet</servlet-name>
		<servlet-class>com.myseriousorganization.bigpipe.core.servlet.BigPipeDispatcherServlet</servlet-class>
		<init-param>
			<param-name>jsp-file</param-name> <!-- The JSP file to forward to after the @PageletTasks are run -->
			<param-value>/home.jsp</param-value>
		</init-param>
		<init-param>
			<param-name>pageletTaskClasses</param-name> <!-- The FQCN of the @PageletTaskClasses -->
			<param-value>com.myseriousorganization.bigpipe.example.LeftModuleDisplayTask,com.myseriousorganization.bigpipe.example.RightModuleDisplayTask</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>HomePageServingServlet</servlet-name>
		<url-pattern><url-that-you-want-serve></url-pattern> <!-- URL you want to serve -->
	</servlet-mapping>

###Step 5 You are all set! Go to http://localhost:8080/app-context/url-that-you-want-serve/ and see the results.

Example

For an example, look here

###License MIT

bigpipe's People

Contributors

crazysoftwarecoder avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.