1. Construire des
applications temps
réel en utilisant Grizzly
Comet
Jean-François Arcand
Ingénieur Sénior
Sun Microsystèmes du Canada
http://twitter.com/project_grizzly
1
2. Votre matinée sur Grizzly Comet
• Cours de français
• C’est quoi Comet/Ajax Pousse
• C’est quoi Grizzly
• Description détaillée du Grizzly Comet Framework
• C’est quoi Atmosphere
• La conclusion
2
3. Cours de Français
Québec France US
Fureteur Browzer Browser
Courriel Mail Mail
Magasinage Shopping Shopping
Québecois Canadien Who?
Pourriel Spam Junk Mail
Résautage Réseau social Facebook
Chandail T-Shirt T-Shirt
3
4. What is Ajax Push, exactly?
Responsive, low-latency interaction for the web.
• highly responsive, event driven browser applications
> Keep clients up-to-date with data arriving or changing on the server,
without frequent polling
• Pros
> Lower latency, not dependent on polling frequency
> Server and network do not have to deal with frequent polling requests
to check for updates
• Example Applications •
4homemedia.com
> GMail and GTalk (using GlassFish project's Comet)
> Meebo •
JotLive
> Many more … •
KnowNow
4
5. Ajax Poll vs Ajax Push
Bending the rules of HTTP.
5
6. Ajax Poll vs Ajax Push
Bending the rules of HTTP.
• Poll:
> Send a request to the server every X seconds.
> The response is “empty” if there is no update.
• Long Poll:
> Send a request to the server, wait for an event to happen, then send the
response.
> The response is never empty.
> HTTP specification satisfied: indistinguishable from “slow” server
• Http Streaming:
> Send a request, wait for events, stream multi-part/chunked response, and
then wait for the events.
> The response is continually appended to.
6
7. How Push works
Keep an open connection.
• Deliver data over a previously opened connection
• Always “keep a connection open”
> do not respond to the initiating request until event occurs
• Streaming is an option
> send response in multiple parts without closing the
connection in between
7
8. HTTP Polling
Regularly checking for updates.
GET /chatLog HTTP/1.1
Accept: */*
Connection: keep-alive
<message>One</message>
• Uses the HTTP protocol in a standard way, but
requests are regularly invoked
setTimeout(‘poll()’, 10000);
8
10. HTTP Streaming
The long response.
GET /chatLog HTTP/1.1
Accept: */*
Connection: keep-alive
messageOne/message
messageTwo/message
messageThree/message
messageFour/message
• Parse most recent message in JavaScript
programming language (not shown here)
• The original 1999 “Push” technique (Netscape 1.1)
10
11. What is Project Grizzly
Open Source Project on java.net,
https://grizzly.dev.java.net
Open Sourced under CDDL/LGPL license.
Very open community policy.
All project communications are done on Grizzly mailing list. No internal,
off mailing list conversations.
Project meetings open to anyone (public conference call).
Project decisions are made by project member votes.
Sun and non Sun commiters
11
12. What is Project Grizzly
Uses Java NIO primitives and hides the complexity programming with Java
NIO.
Easy-to-use high performance APIs for TCP, UDP and SSL communications.
Brings non-blocking sockets to the protocol processing layer.
Utilizes high performance buffers and buffer management.
Choice of several different high performance thread pools.
Ship with an HTTP module which is really easy to extend.
Use the Framework, but STILL have a way to control the IO layer.
12
14. 1. Who?
Grizzly Electronic
Grizzly RCM Nortel
Port Unification Arts (EA)
Many many
Yahoo Brazil Tomcat* MINA*
more!!!
Sun Shared
Restlet Sailfin Jetty
Shell
Grizzly NIO Framework
14
15. HTTP modules
• The Grizzly Framework also have an HTTP framework that can be used to
build Web Server
This is what GlassFish™ v1|2|3 build on top of.
More specialized modules are also available like Comet (Async HTTP).
• Simple interface to allow customization of the HTTP Protocol
GrizzlyRequest: A utility class to manipulate the HTTP protocol request.
GrizzlyResponse: A utility class to manipulate the HTTP protocol
response.
GrizzlyAdapter: A utility class to manipulate the HTTP request/response
object.
15
16. Example: Jersey
(jersey.dev.java.net).
• Jersey is the open source JAX-RS (Java Specification Request (JSR) 311)
Reference Implementation for building RESTful Web services.
• By default, Jersey ship with support for Grizzly HTTP WebServer :
Implement the GrizzlyAdapter interface, use the GrizzlyRequest and
Response object to handle the http protocol.
• In less than 20 minutes, Jersey was running with Grizzly!
16
19. 2.4 Who?
Many many
Bindows AltMobile ItsNat
more!!!
Sun Instant
4homedia ICEFaces DWR
Messenger
Messages
Grizzlet Bayeux Continuation
Bus
Grizzly Comet
Framework
Grizzly HTTP (Sync/Async)
Grizzly NIO Framework
19
20. Introduction to Grizzly Comet
• Grizzly Comet is a framework that ship with
GlassFish v1|2|3, and can also be embedded into
any application using the Grizzly Embed interface
(no fish involved).
• The Grizzly Comet Framework includes a set of
components that can be used for building Comet
based application:
Grizzly Comet, Continuation, Grizzlet, Messages Bus,
Bayeux support
20
21. Grizzly Comet Framework
• The Framework contains the classes required to add
support for Comet in a Web Application
• Main classes to interact with (details next):
CometEngine
CometContext
CometHandler
NotificationHandler
CometReader
CometWriter
21
22. How it works
send CometHandler
CometHandler
Browser 1 CometHandler
CometContext push
Browser 2
filters
send
Push data
NotificationHandler
Servlet
22
23. CometContext
• A CometContext is a distribution mechanism for
pushing messages that are delivered to multiple
subscribers called CometHandler.
• All connections registered to a CometContext
automatically becomes suspended, waiting for an event
(a push) to happens.
• A browser receives only those messages published
after the client “register “ to a CometContext.
• Its contains references to all suspended connections
(encapsulated inside a CometHandler)
23
24. CometContext - Example
//Create a CometContext for my Chat application
CometContext chatContext
= CometEngine.getEngine().register(“chatroom”);
// Suspend the request
ChatRoomHandler chr = new ChatRoomHandler();
chatContext.addCometHandler(chr);
// Push welcome message
chatContext.push(“Ted is entering the room”);
// Push bye bye message
chatContext.push(“Alexandre is leaving the room”);
// Later, resume the request
chatContext.resumeCometHandler(chr);
24
25. CometHandler
• The CometHandler is the master piece of a Grizzly
Comet based application.
• A CometHandler contains the business logic of what
will be pushed back to the browser.
• A CometHandler might be invoked by the Container:
When a push operation happens
When a I/O operations are ready to be process
(asynchronous read or write)
When the browser close the connection.
25
26. CometHandler
• The CometHandler is the master piece of a Grizzly
Comet based application.
• A CometHandler contains the business logic of what
will be pushed back to the browser.
• A CometHandler might be invoked by the Container:
When a push operation happens
When a I/O operations are ready to be process
(asynchronous read or write)
When the browser close the connection.
26
27. CometHandler API
//Invoked when CometContext.notify() is called
public void onEvent(CometEvent ce);
// Invoked when the browser close a suspended
// connection or when the suspend timeout expire.
public void onInterrupt(CometEvent ce);
// Invoked when the request is suspended
public void onInitialize(CometEvent ce);
// Attach an object
// most probably the HttpServletResponse}
public void attach(E e);
27
29. NotificationHandler
• The NotificationHandler object is the masterpiece when writing Comet
application
This is inside that object that you will decide to what to do with the push
operation:
– Throttle: If too many push occurs simultaneously, should we delay
them?
– Aggregate: Should we cache push operations and aggregate them to
avoid overloading the network?
– Filter: Should all messages by pushed back to the client.
– Should a thread pool be used to improve the push speed operation?
Should a JMS backed be used to deliver the message?
• The DefaultNotificationHandler push all messages.
29
31. Asyncronous I/0 – Read and Write
• A CometHandler can be notified for asynchronous read and write
operations
• Useful when reading or writing large chunk (like file upload.
• Bring NIO to Servlet indirectly
31
33. Grizzly Messages Bus
• The Grizzly Messages Bus implements the Grizzly Comet Protocol (GCP).
• The GCP protocol is a very basic protocol that can be used by browser to
share data, using the comet technique, between several clients without
having to poll for it.
• The protocol is very simple. First, a client must subscribe to a topic:
http://host:port/contextPath?subscribe=[topic
name]cometTechnique=[polling|log-polling|http-
streaming]message=[text]
• When issuing the URL above, the connection will be automatically suspended
based on the cometTechnique specified
33
34. Grizzly Messages Bus
• To share data between applications, a browser just
need to send the following request:
http://host:port/contextPath?publish=[topic
name]message=[text]
• The Servlet can be used as it is or extended to add
extra features like filtering messages, security, login,
etc.
• Quite easy to write games using the Grizzly
Messages Bus. No server side implementation
required, just client side!
34
36. The Bayeux Protocol
• http://svn.xantus.org/shortbus/trunk/bayeux/bayeux.html
• Bayeux is a JSON-based protocol for clients to register interest in events and
for servers to deliver them in a more timely fashion than Ajax-based polling
allows.
• With Bayeux, you only needs to write the client.
• The goals of the Bayeux spec so far have been to:
make event delivery fast
keep it simple
provide extension points in the protoco
36
38. What is a Grizzlet
• Simple interface build on top of the Comet
Framework:
public void onRequest(AsyncConnection
asyncConnection)
public void onPush(AsyncConnection
asyncConnection)
• Implement that interface, deploy your POJO in
Grizzly Web Server or a GlassFish v3 Adapter.
Boom it works!
38
40. Grizzly Continuation
• Simple API to resume/suspend requests:
GrizzlyContinuation continuation =
Continuation.getContinuation();
continuation.suspend(timeout);
…
// From another Thread
continuation.resume();
• Used in Servlet/JSP.
40
41. What is Project Atmosphere
The state of Comet (Ajax Push) right now
• Since July 2006the Grizzly Comet Framework and its extensions’
popularity keep increasing.
Among the most popular framework right now.
• Comet techniques aren't standardized among Web Container
Grizzly Comet applications aren't portable (cannot run on Tomcat nor
Jetty)
• Servlet 3.0 will supports only a subset of Comet
No async I/O, No pushing functionalities
Support will takes time (Spec to be finalized)
41
42. What is Project Atmosphere (con't)
So...
• Atmosphere is a POJO based framework using
Inversion of Control (IoC) to bring Comet to the
masses.
• A framework which can run on any Java based Web
Server.....without having to wait for Servlet 3.0 or
without the needs to learn how Comet support has
been differently implemented by current Web Server
supporting Comet
• Run on top of Servlet 2.5 compliant Web Server
42
43. What is Project Atmosphere (con't)
Extends and improve the Grizzlet concept....
• Grizzlet is a POJO based approach for writing
Comet application.
Grizzlet only runs on top of the Grizzly Comet
Framework right now
Grizzly community asked many times to have the
concept ported to other Web Server
• Evolve the Grizzlet...and make it the core of Project
Atmosphere.
43
44. Architecture
Reuse experience and code...from Grizzly
Comet to Jersey!
Grizzlet Atmosphere re-usable
(Atmosphere POJO) Components library
Jersey's Injection Provider, IoC Support, and more....
Portable Comet Layer API (PCL)
Tomcat Comet Grizzly Comet Jetty Comet Blocking WS
User defined Atmosphere Component
44
45. Example
Easy of use of Jersey, power of Grizzly Comet
@Grizzlet(Grizzlet.Scope.APPLICATION)
@Path(quot;myGrizzletquot;)
public class MyGrizzlet{
@Suspend(6000)
@GET
@Push
public String onGet(){
return quot;Suspending the connectionquot;;
}
@POST
@Push
public String onPost(@Context HttpServletRequest req,
@Context HttpServletResponse res) throws IOException{
res.setStatus(200);
res.getWriter().println(quot;OK, info pushedquot;);
return req.getParameter(quot;chatMessagequot;);
}
45
46. Summary
The Asynchronous Web Revolution is Now
• The Asynchronous Web will revolutionize human
interaction
• Push can scale with Asynchronous Request
Processing.
• With GlassFish, the revolution begins with your
applications today
• Get ready for Atmosphere and Servlet 3.0
46
47. For More Information
http://twitter.com/project_grizzly
• Getting Started with GlassFish and Comet
http://weblogs.java.net/blog/jfarcand/archive/2008/04/the_hitchhikers.html
http://weblogs.java.net/blog/jfarcand/archive/2006/10/writting_a_come.html
• Grizzly Active’s Bloggers:
Alexey: http://blogs.sun.com/oleksiys/
Shing Wai: http://blogs.sun.com/swchan/
John: http://weblogs.java.net/blog/johnmann/
Sebastien: http://weblogs.java.net/blog/survivant
Jeanfrancois: http://weblogs.java.net/jfarcand
• Project Grizzly mailing lists,
dev@grizzly.dev.java.net users@dev.grizzly.java.net
47