Please click on any Advertisement, if you like this blog.

Friday, August 16, 2013

Network Reliability by CometD: Hello world in CometD

Friends! I am going to discuss just top of the CometD here.

While creating any web application for palm devices like mobile phone, tabs, ipad etc, the main villain is network failure. It always defeats our hero (web application) by kidnaping and destroying application data (data lost) on request – response chain. For smooth functioning of any web application there should be a guaranty that if data is sent from client, it will definitely reach to server and if server dispatches a response, it will certainly reach to corresponding client. To fulfill this requirement, we need a side hero i.e. a tool to establish Network reliability.

Here comes the role of Bayeux Protocol. It is used to establish the bidirectional interactions between web clients, for example using AJAX, and the web server. It provides several specifications to establish network reliability in Web Application. One of the implementation of these specifications is CometD by DOJO technology.

“CometD is a scalable web event routing bus that allows you to write low-latency, server-side, event-driven web applications. Typical examples of such applications are stock trading applications, web chat applications, online games, and monitoring consoles. CometD makes use of an Ajax push technology pattern known as Comet, but also uses emerging web standards such as WebSocket for low latency communication.is a scalable web event routing bus that allows you to write low-latency, server-side, event-driven web applications. Typical examples of such applications are stock trading applications, web chat applications, online games, and monitoring consoles.”

OHH!!!!! What a great….. English. Today, I came to know that even I can also write such English. Don’t worry, these are not my words. These are from CometD document.

I think, I have bored you a lot. So, before you start beating me, I should move on to the steps for configuring CometD and creating a Hellow world program.


Step 1) Download library and jar files: Download cometd-2.5.0-distribution.tar.gz from http://download.cometd.org/ and go to directory “cometd-2.5.0-distribution\cometd-2.5.0\cometd-demo\target”. Here, you will get cometd-demo-2.5.0.war. Use following command to unwar it: “jar –xvf cometd-demo-2.5.0.war”. Extract the following jar files from the newly created folder.



Along with above jar files, extract following javascript library files from the above unwar folder:



Step 2): Create a Dynamic Web Project in Eclipse.

Step 3): Copy the above jar files in lib folder and javascript library file in “resources” folder under WebContent.

Step 4): Create a web.xml file as given below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>CometDPoc</display-name>
 
      <servlet>
        <servlet-name>cometd</servlet-name>
        <servlet-class>org.cometd.java.annotation.AnnotationCometdServlet</servlet-class>
        <async-supported>true</async-supported>        
        <init-param>
              <param-name>services</param-name>
            <param-value>com.test.ai.amap.gsontest.MyService</param-value>
          </init-param>   
       
        <load-on-startup>1</load-on-startup>
    </servlet>
   
    <servlet-mapping>
        <servlet-name>cometd</servlet-name>
        <url-pattern>/cometd/*</url-pattern>
    </servlet-mapping>   
 
</web-app>

In this web.xml file AnnotationCometdServlet is the built-in servlet of CometD and it will receive all the request coming from client through cometD.

MyService is a java class that acts as the service class and it will be initialized by CometD container automatically and it is singleton. All the requests, coming to AnnotationCometdServlet, are forwarded to this service class.

Step 5): Create MyService.java service class:

package com.test.ai.amap.gsontest;

import org.cometd.bayeux.server.ConfigurableServerChannel;
import org.cometd.bayeux.server.ServerMessage;
import org.cometd.bayeux.server.ServerSession;
import org.cometd.java.annotation.Configure;
import org.cometd.java.annotation.Listener;
import org.cometd.java.annotation.Service;
import org.cometd.server.authorizer.GrantAuthorizer;

@Service
public class MyService
{
      @org.cometd.java.annotation.Session
    private ServerSession _session;
     
      @Configure("/service/*")
    public void configureExecuteCommand(ConfigurableServerChannel channel)
    {
      channel.addAuthorizer(GrantAuthorizer.GRANT_SUBSCRIBE_PUBLISH);
      channel.setPersistent(true);
    }
   
    @Listener("/service/foo")
    public void handleFooMessages(ServerSession remote, ServerMessage message)
    {
      System.out.println("---------------------Hello World---------------------------");
      remote.deliver(_session, message.getChannel(),"I am Saying Hello World From Server", null);
    }
}

Function configureExecuteCommand() with annotation like “@Configure("/service/*")” makes all the service channel available for subscribing.

Function handleFooMessages() with annotation like “@Listener("/service/foo")” declares itself as the listener or subscriber for channel “/service/foo” and all the request published on this channel from client will come to this function.

Step 6): Create handshake function on client: Before starting communication with server through cometD, it is necessary to make a handshake of client with server where both detect each other’s compatibility. This is done by dojox.cometd.init() function of cometD as given in file CometDMessagingManager.js

Step 7) Create client side publisher of request on client: We have to create the client side publisher of request which publishes requests on service channel that are subscribed on java side by functions of service classes as done in echoRpc() function of user.js

Step 8) Create client side subscriber of response from server: We have to create the client side subscriber of the channel on which server sends the response. Actually server sends response on the save channel from which it has received the request. That means the client side subscriber should be created on same channel on which the client side publisher is created. It is done on echoRpc() function of user.js. In the attached application, the function echoRPCReturn() of user.js is the corresponding client side subscriber.

Step 9) Provide comet url: Provide the cometD url as http://localhost:8080/CometDIdea/cometd in the dojox.cometd.init() function in setUpMessaging() function of CometDMessagingManager.js file. This is the url where every request will be dispatched and they will be handled by AnnotationCometDServlet.java file as configured in web.xml file.

I have attached the complete application code. Please go through it for getting clear idea. Following url may be helpful.


I am not able to attach the code here. So, if anybody needs it, please email me on jksnu1@gmail.com or put a comment on this blog.

No comments:

Post a Comment

Please provide your precious comments and suggestion