users@grizzly.java.net

Problem with comet-based web app **NEW CHALLENGE**

From: Hiram Coffy <hiram.coffy_at_pb.com>
Date: Mon, 3 Nov 2008 10:17:15 -0500

Hi All,

I have made considerable progress with my comet-based web app. Thank you Jeanfrancois and Richard for your help.
I am using a simple jMaki client to communicate with my comet-enabled servlet. The server needs to push updates to the client. I am now using a timer to simulate async events. I am seeing a set of peculiar behaviors with my app. What I hope to accomplish is to get the comet servlet to make async updates to a combobox widget on the client page. 1) I noticed that unless I call close on the connection the data does not get flushed out to the client. Calling flush appears not to have the desired effect. The client browser, in my case ff 3.0.3 might be playing buffering tricks, I am not sure. I haven't gone down that path yet. 2) It appears that the connection cannot be held open regardless of what the server does. I must be missing something obvious. After sending an inordinate amount of time trying to figure it out on my own, I have decided to throw in the towel. I am calling in the cavalry. Help....

I am including bits of relevant code here. Though my example uses streaming server-side technique, I am not necessarily wedded to streaming. It just so happened that the version of the code I am including here uses streaming. I just need a flavor of a async updates to work.

Thank in advance,

Relevant bits of code:

Client:
jmaki.subscribe("/bn/refresh/*", function(args) {

    jmaki.doAjax({method: "GET",
        url: "RefreshServlet",
        callback: function(_req) {
            var tmp = _req.responseText;
            var obj = eval("(" + tmp + ")");

            jmaki.publish('/bn/setValues', obj);
            document.getElementById('counter').innerHTML ="<div id='counter'><b>" + new Date().toString() + "</b></div>";
        }
    });
});

Server:
    @Override
    public void init(ServletConfig config) throws ServletException {
...

        /* Configure this web app server to work in async mode using comet engine */
        ServletContext ctx = config.getServletContext();
        contextPath = ctx.getContextPath();

        CometEngine engine = CometEngine.getEngine();
        CometContext cctx = engine.register(contextPath);

        //cctx.setExpirationDelay(30 * 1000);
        cctx.setExpirationDelay(-1); //Doesn't seem to make any difference
..
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        registerHandler(response, request);
        processRequest(request, response);
    }

    private void registerHandler(HttpServletResponse response, HttpServletRequest request)
        throws IllegalStateException {
        HttpSession session = request.getSession();
....

        if ( session.getAttribute(requestURL) == null) {
            RefreshHandler handler = new RefreshHandler();
            handler.attach(response);

            CometEngine engine = CometEngine.getEngine();
            CometContext context = engine.getCometContext(contextPath);

            try {
                context.addCometHandler(handler);
                session.setAttribute(requestURL, true );
            } catch (IllegalStateException ex) {
                   ...
            }
        }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        /* Notify RefreshHandler */

        CometEngine engine = CometEngine.getEngine();
        CometContext<?> cctx = engine.getCometContext(contextPath);
        cctx.notify(null);

        refreshComboBox(response);
    }

    private class RefreshHandler implements CometHandler<HttpServletResponse> {

        private HttpServletResponse response;

        public void onEvent(CometEvent event) throws IOException {
            if (CometEvent.NOTIFY == event.getType()) {

                refreshComboBox(response);

                // commented out the resume if it is Http Streaming
                //event.getCometContext().resumeCometHandler(this); //Was just experimenting. Long polling or streaming would do just fine.
            }
        }
          .....
    }

    void refreshComboBox(HttpServletResponse response) throws IOException{

        ProvisioningDocLocatorBeanWS provBean = new ProvisioningDocLocatorBeanWS();

        PrintWriter writer = response.getWriter();
        try {

            writer.write(provBean.getProvisioningDocs());
            writer.flush(); //Does not flush data to the client

        } catch (JSONException jex) {
            logger.error(jex.getMessage(), jex);
        } finally {
            writer.close(); //Unless I call close data does not get flushed out to client
            logger.info<http://logger.info>("Refreshed Client :" + System.currentTimeMillis());
        }
    }



From: Hiram Coffy
Sent: Friday, October 31, 2008 7:12 AM
To: 'users_at_grizzly.dev.java.net'
Subject: Problem with comet-based web app **SOLVED**

Hello Richard,

Thanx much for all your pointers. I hadn't realized that you had replied to my post until this morning. I am still getting use to getting around in the forum.
Apologies for the belated response. Your contributions were very helpful to me. I believe I have the mechanics of this technique down, I just needed clarifications on implementation.

Best Regards,

Hiram

From: Hiram Coffy
Sent: Thursday, October 30, 2008 4:23 PM
To: 'users_at_grizzly.dev.java.net'
Subject: RE: Problem with comet-based web app

Salut Jeanfrancois,


Thank you very much for being so responsive. Your answers to step 2 and 6 got me unstuck. I appreciate your help.



2) API: What compatible comet api should I compile the source code against?

If you plan to deploy against v2ur2, you need to build against the API
that ship with v2. Hence, in your classpath, you need to add:

${glassfish.home}/lib/appserv-rt.jar.


6)Last question: Does the app server come bundled with the comet api



yes, in v2: com.sun.enterprise.web.connector.grizzly.comet.*

in v3 and grizzly standalone: com.sun.grizzly.comet



Keep up the great work!

Regards,



Hiram



A+


From: Hiram Coffy
Sent: Wednesday, October 29, 2008 12:57 PM
To: 'users_at_grizzly.dev.java.net'
Subject: Re: Problem with comet-based web app

Hi all,

I am still struggling with getting my comet-based servlet to work. Any help would be much appreciated

I guess what would be helpful for me is a simple step-by-step procedure. So far, what I have found is scattered and fragmented bits and pieces of information, critical details appear to be glossed over.

I suppose I can boil down my request to the following set of questions:
1) APP SERVER: What do I need to get comet-based servlet working? e.g. glassfish V2UR2 with comet support enabled.

2) API: What compatible comet api should I compile the source code with?

3) API DOWNLOAD: Where do I download the API from?

4) CLASSPATH: Do I copy the API to ${as.home}/lib for example

5) TEST: How do I positively verify that comet support is indeed enabled?

6)Last question: Does the app server come bundled with the comet api

From: Hiram Coffy
Sent: Sunday, October 26, 2008 5:39 PM
To: 'users_at_grizzly.dev.java.net'
Subject: Problem with comet-based web app


Hello Jean-Francois,
Let me start by thanking you for all your good work on the Grizzly/comet front. Kudos!

I wrote a simple comet app based on the tutorial found at: http://docs.sun.com/app/docs/doc/820-4496/ggrgt?a=view.

I get the following exception when a request is received by my servlet:
java.lang.IllegalStateException: Make sure you have enabled Comet or make sure the Thread invoking that method is the same a the request Thread.
at com.sun.grizzly.comet.CometContext.addCometHandler(CometContext.java:263)
at com.sun.grizzly.comet.CometContext.addCometHandler(CometContext.java:311)
....

I have already combed through your blogs. I have tried all the suggestions that I have come across. So far, i haven't had any success. I noticed a peculiar behavior with my glassfish instance; whether <property name="cometSupport" value="true"/> is added to domain.xml or not seems to make not a bit of a difference.

I have made sure to bounce the server every time i make a change to domain.xml to ensure that the changes will be recognized. I have tried several experiments with the "cometSupport" property added and removed from domain.xml. I ave tried similar experiments with <property name="proxiedProtocols" value="ws/tcp"/> I have even enabled the flag through the command line using "asadmin set server.http-service.http-listener.http-listener-1.property.cometSupport=true"

Relevant environment information:
Sun Java System Application Server 9.1 (build b58g-fcs) (GlassfishV2)

excerpt from domain.xml:
....
<http-listener acceptor-threads="1" address="0.0.0.0" blocking-enabled="false" default-virtual-server="server" enabled="true" family="inet" id="http-listener-1" port="8080" security-enabled="false" server-name="" xpowered-by="true">
<property name="proxiedProtocols" value="ws/tcp"/>
<property name="cometSupport" value="true"/>
</http-listener>
....

excerpt from app server output console:
....
WEB0302: Starting Sun-Java-System/Application-Server.
Enabling Grizzly ARP Comet support.
WEB0712: Starting Sun-Java-System/Application-Server HTTP/1.1 on 8080
....

excerpt from web.xml
...
<servlet>
<servlet-name>RefreshServlet</servlet-name>
<servlet-class>package.RefreshServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
...

The following bit of code registers the handler and is invoked only once, upon receipt of first request.
private void registerHandler(HttpServletResponse response)
throws IllegalStateException{
if (oneShot) {
RefreshHandler handler = new RefreshHandler();
handler.attach(response);

CometEngine engine = CometEngine.getEngine();
CometContext context = engine.getCometContext(contextPath);

try{
context.addCometHandler(handler);
oneShot = false;
}catch(IllegalStateException ex){
logger.error(ex.getMessage(), ex);
throw ex;
}
}
}
Two questions:
1) Any suggestions?
2) How can I positively tell that comet support is indeed enabled?