Workstation Setup

Workstation Setup


Connection Pool Setup

 

Downloads

FrameWork Jars

 

Sample Default Properties Files

 

HelloWorld javascript and supporting files

 

 

 

 

AsyncFW Comet Quick Start

 

Assumptions:

 

Ø      You have your workstation setup.

Ø      You have completed Part I and Part II of the Quick Starts (ooops – Take me back!)

 

For a more general Comet discussion, please see the more General Comet tutorial.

 

The FrameWork on Comet

 

AsyncComet, looks almost exactly like standard AsyncFW framework.  You will find that using Comet within the FrameWork is very easy.  This tutorial builds upon the “Hello World” project.

 

Pre-work:

1)     Copy the /src/com/test/World.java file, and name the copy /src/com/test/CometWorld.java

 

2)     Copy the /Webcontent/html/scripts/pages/World.js to CometWorld.js

 

3)     Copy the /Webcontent/html/xsl/World.xsl to CometWorld.xsl

 

4)     Copy the servlet reference for “World” in the web.xml files, and paste the copy back, and replace the “World” references with “CometWorld

      <servlet>

    <description></description>

    <display-name>CometWorld</display-name>

    <servlet-name>CometWorld</servlet-name>

    <servlet-class>com.test.CometWorld</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>CometWorld</servlet-name>

    <url-pattern>/CometWorld</url-pattern>

  </servlet-mapping> 

 

5)     Open Each of the following three files and do a text replace of “World” with “CometWorld

a)     CometWorld.java

b)     CometWorld.js

c)      CometWorld.xsl

 

6)     Open /Webcontent/html/Start.html and change the AjaxRequestPage command to the following;

 

      AjaxRequestPage("CometWorld","Init", "Page", FormContextArea);

 

            And, add the CometWorld.js script to the import script section of the

            Start.html file;

 

      <script type="text/javascript" src="./scripts/pages/World.js"></script>

      <script type="text/javascript" src="./scripts/pages/CometWorld.js"></script>

 

7)     Refresh, compile, publish

 

8)     Test it. http://localhost:8080/Hello/html/Start.html

 

 

If all is well, you “CometWorld”, should work exactly like “World”.  If not, time to debug!

 

 

Modify the ServiceLet

 

Open the CometWorld.java source file. The class definition had been changed to the following;

 

public class CometWorld  extends FWCometServiceLet implements FWServiceLetInt

 

Note: The class now extends FWCometServiceLet instead of FWHTTPServiceLet.

 

Modify the init() method to use the overloaded init method of the FWCometServiceLet;

 

public void init(ServletConfig config) throws ServletException {

       super.init(config, "CometWorld", 10);

       // WARNING: the below line turns off Session Validation.

       this.setRequiresValidation(false); //you almost never want this!!

       // END WARNING.

       this.setServletName("CometWorld");

       this.setArrayOfParms(new String[]{"CometWorld"});

       this.setOutputXSL(XSLT_CometWorld);

}

 

The overloaded init() takes two additional parameters. The first is the CometContext Name, in this case “CometWorld”, and the second is the timeout (in minutes).

 

 

At this point let’s discuss how Comet is implemented in the Framework. The FWCometServiceLet supports all the underlying FWHTTPServiceLetsysAccess” methods, such as ‘Page’, ‘Data’ etc. It also supports two additional sysAccess methods

 

1)     StartListener: This sysAccess method add the client to the pool of listeners for this ServiceLet.

2)     NotifyListeners: Notify post the response to all listening clients. The “Notify” can be triggered by the client (as this example will show), or it can be triggered on the server.

 

Modify the ServiceLets Execute() method

 

This part is very simple, but can be tricky to get your head around. The execute method must handle the three possible states ;

 

1)     Load the Page for initial access

2)     StartListening state

3)     Notification state

 

To handle these three states, we will use the ‘sysAction’ parameter of the request object.  From our JavaScript, when we just one to navigate to the screen, and load it for the first time, we will set sysAction = “init”. When we want to register as a listener, we will set sysAction = “listen”. When a particular client wants to notify all the other of a change, we will set the sysAction = “notify”.

 

Below is the updated Execute method for the CometWorld.java

 

public void Execute(FWSession fwSess) throws FWException {

      

    try {

       String stAction = 

              fwSess.getRequest().getParameter("sysAction").toLowerCase().trim();

       if ("init".equalsIgnoreCase(stAction)){

              // on init code

       }else if ("listen".equalsIgnoreCase(stAction)){

              // any code to indicating that 'listening'

       }else if ("notify".equalsIgnoreCase(stAction)){

              // do any pre-notification manipulation of data

       }else{

              fwSess.getSessionResp().AddNode("State", "FAILED");

       }

       }catch(Exception err){

              fwSess.getSessionResp().AddNode("sysError", err.getMessage());

       }

 

}

 

Modify the JavaScript

 

Open the CometWorld.js file and edit it so that it matches the following;

 

var CometWorldDirty = false;

 

function CometWorld_preRequestAction(Action) {

       return true;

}

function CometWorld_Validate(fieldName, fieldValue){

       return true;

}

function CometWorld_getDataToSend(Action) {

       return getAllFormValues("CometWorld");

}

function CometWorld_SubmitButton(ButtonObj){

       AjaxSubmitPage("CometWorld", "PostData", FormContextArea);

}

function CometWorld_Submit_PreSubmit(Action){

       return true;

}

function CometWorld_Submit_Return(Action){

 

}

function CometWorld_Request_Return(currForm){

       return true;

}

function CometWorld_Open(){

       sysAction = "listen";

       AjaxStartListener("CometWorld", sysAction);

}

function CometWorld_Notify(){

       sysAction = "notify";

       AjaxNotifyListeners("CometWorld", sysAction);

}

function CometWorld_postRequestAction(Action, req) {

       if (req.readyState == 4 && (req.status == 200)) {

              ProcessResp(req.responseXML);

       } else if (req.readyState == 4 && (req.status != 200)) {

              alert("[CometWorld_postRequestAction] Your request may have timed out. Please try again."+req.status);

       }

}

function ProcessResp(inXML){

       if(null != inXML){

              var Nodex      = inXML.getElementsByTagName("lastname")[0].attributes;

              var NodexValue = Nodex.getNamedItem("value").nodeValue;

              setFormValue("lastname", NodexValue);

              CometWorld_Open();

       }

 }

 

Most of the functions are the same as the “World.js”, with the addition of four new functions.

 

1)     the CometWorld_Open() function, which is used to establish the long running thread

2)     Second is the CometWorld_Notify() – which is used to send a client side notification to all listeners.

3)     CometWorld_postRequestAction, which is used to accept all notifications

4)     ProcessResp(), which is call by the postRequestAction(), and is used to load the update to the UI.

 

That’s it! Time to test.

 

http://localhost:8080/Hello/html/Start.html

 

To test the Comet functionality, Simply start up multiple browsers, with the URL above,  put something in the ‘LastName’ field, and press the “Notify Button”. If all is well, the value entered will be populated across all the instances.

 

 

Copyright 2009. All rights reserved by

S. Chappell