Open Source Reverse Ajax Library: Understanding Atmosphere and COMETD
Source: Internet
Author: User
KeywordsOpen Source ajaxatmospherecometd
This series of articles shows you how to develop event-driven WEB programs using reverse Ajax technology. The 1th part describes Reverse Ajax, polling, streaming, Comet, and long polling. Part 2nd describes how to implement Reverse Ajax using WebSocket, and discusses the limitations of using Comet and WebSocket WEB servers. The 3rd part explores some of the difficulties you may encounter in implementing your own Comet or WebSocket communication system when you need to support multiple servers or provide a stand-alone WEB application that users can deploy on their own servers. Even if the JavaScript code on the client is simple, you still need some exception handling, reconnection, and validation features. On the server side, the lack of a global API and some WEB server APIs results in the need for a framework with abstract functionality. The 3rd part also discusses the Socket.io.
In this article, you will learn about atmosphere and cometd, which are the most famous open source reverse Ajax libraries for Java servers.
For
Ideally, if you want to make the most of this article, you should know about JavaScript and Java. To run the examples in this article, you will need to use the latest version of Maven and JDK.
Atmosphere Framework
Atmosphere is a Java technology framework that provides a common API for using many WEB servers (including Tomcat, Jetty, GlassFish, Weblogic, Grizzly, Jbossweb, JBoss, and Resin) Comet and WebSocket characteristics. It supports any WEB server that supports Servlet 3.0 specification. Of all the frameworks shown in this article series, atmosphere supports the most servers.
Atmosphere can detect native server APIs for Comet,atmosphere and switch back to the Servlet 3.0 runtime environment. Or it will fall back to the "managed" asynchronous mode (but not the scalability of the Jetty continuations), which also applies to Comet. Atmosphere has been on the market for more than two years and is still active in the development phase. It is used in large Web applications, such as JIRA, which is one of the most famous problem trackers. Figure 1 shows the atmosphere framework.
Figure 1. Atmosphere Architecture View
The atmosphere framework, composed of the atmosphere runtime, provides a common API for various WEB server solutions and standards. At runtime, clients can access APIs and reverse Ajax features by setting up a servlet and using Google Web Toolkit (GWT). Alternatively, you can use Jersey, a framework for implementing the JSR-311 (Jax specification). As a result, you can use annotations to apply atmosphere to more rest scenarios. After you configure the module you selected, you can access the atmosphere runtime by implementing some classes (discussed later in this article). You can also add support for clustering, messaging, and dependency injection by using the plugins provided. If you are using a WEB framework (Wicket, Struts, or Spring MVC), you can explicitly add support for reverse Ajax by using atmosphere's meteorservlet. The servlet shows the Meteor object, which can be retrieved in the controller and service to suspend or restart the request.
The advantage of atmosphere is still the server side: it provides a standardized API that contains all the different solutions and methods for communicating with WebSockets or Comet. Atmosphere does not use protocols for communication between client and server, such as Socket.io and cometd. Both libraries provide client JavaScript and server-side servlet communications using specific protocols (for handshake, messaging, acknowledgments, and Heartbeat protocols). The goal of atmosphere is to provide a common communication channel on the server side. If you need to use a specific protocol, such as Bayeux (a protocol used by COMETD), you must develop your own handlers in atmosphere. The COMETD plug-in meets your needs: It uses the atmosphere API to suspend and restart requests and to delegate cometd classes to manage cometd traffic by using the Bayeux protocol.
Atmosphere provides a JQuery client library that makes connection settings easier and can automatically detect the best transport protocol (WebSockets or cometd) that can be used. The use of the atmosphere JQuery plug-in is similar to the HTML5 websockets API. First you connect to the server, register a callback to receive the message, and then put some data in.
The source code provided in this article contains a atmosphere sample that uses the handler directly for the atmosphere servlet. The client code remains unchanged, as is the code used in parts 1th, 2nd, and 3rd of this series (all in the chat sample code that uses Comet long polling). You can also use the atmosphere JQuery plug-in, but this is not necessary because atmosphere does not force any communication protocol to be used. It is strongly recommended that you look at other examples in the atmosphere project, especially those using JSR-311 annotations (Jersey). They do simplify the writing of handlers.
Listing 1 shows the interface of the atmosphere handler.
The ONrequest method receives all requests from the client and decides whether to suspend or restart the request (or not perform any action). Each time a request is suspended or restarted, a broadcast is sent, or a time-out occurs, an event is sent and received by the Onstatechange method.
Listing 2 shows the implementation of the ONrequest method for the Comet chat application.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.