views:

78

answers:

1

I am trying to understand more about async notifications. I have a URL in the form of:

http://www.sample.com/AsyncNotify?sessionId=xxxxxx

Now if I call this URL with the sessionId, it is equivalent to registering for Asynchronous notifications. I am using Apache HTTP Commons library to do Http Post and Get. If that's the case, then how can I receive events from the server side? Do I have to forget this approach and use sockets instead? Currently, this is my approach:

HttpClient httpClient = new HttpClient;
String url = "http://www.sample.com/AsyncNotify?sessionId=xxxxxx"
GetMethod get = new GetMethod(url);
try {
   httpClient.executeMethod(get);
   //read the response
} catch(Exception e) {

}

What I was thinking was to establish a socket level connection inside a while loop and call a handler whenever it receives some data, but is there a better way to achieve this?

EDIT:

I've used xSocket to get to the following stage but the connection closes after 30 seconds:

         try {
            String _GETRequest = "/sample/notify";
            HttpClientConnection con = new HttpClientConnection("10.0.0.23", 5050);

            con.setConnectionTimeoutMillis(100000);
            GetRequest request = new GetRequest(_GETRequest);
            request.setParameter("id", id);

            IHttpResponseHandler responseHandler = new AsyncHandler();

            con.send(request, responseHandler);
            org.xlightweb.client.HttpClient httpClient = new org.xlightweb.client.HttpClient();
            request.setParameter("id", id);
            con.send(request, responseHandler);

                    // Don't let the program terminate. In other words,
                    // wait for a message from the server
            while(con.isOpen()) {};

            if(!con.isOpen()) {
            }

        } catch (ConnectException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
+1  A: 

Tomcat came out with a technology called Tomcat Comet ( http://tomcat.apache.org/tomcat-6.0-doc/aio.html ). It has also been used for the new Servlet 3.0 spec. This technology will allow you to do persistent HTTP connections through which you can push notifications to any clients.

There is also a technolgy called WebSockets that is part of HTML 5
( http://dev.w3.org/html5/websockets/ ) Of course it only works in a limied set of browsers for now. Probably should wait on this.

Of course the current way to do it to be technolgy backwards compatible (even if it sucks) is to poll the server periodically and get results that way.

Of course if everybody (clients and servers) are on a local network then probably something like RMI or even EJBs or JMS Pub/Sub would be best.

Here is a Comet tutorial http://www.ibm.com/developerworks/web/library/wa-cometjava/index.html and another one http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp-test.html

Romain Hippeau
@Romain: Thanks for your input. I am in a fix on how to write a Java client that works with a Comet server. As for WebSocket, my server does not support it so that's not an option. I would really love if you have some input on a very simple pure-Java based client that works with a Comet server.
Legend
@Legend - which app server are you using ? What is your client written in ? I added a couple of links at the end of my answer that might help
Romain Hippeau
@Romain: Thanks for your reply. I think the server uses grizzly comet `https://grizzly.dev.java.net/` I am able to get it working with javascript using the XMLHttpRequest but using Java, for some reason, it doesn't respond. I already looked at the tutorials you posted. I added the jars that were mentioned in the tutorial but some classes like IHttpRequestHandler etc. are still not visible.
Legend
@Legend - look here for the client library referenced http://xlightweb.sourceforge.net/
Romain Hippeau
@Romain: +1 for your help... Thanks. I managed to setup a client using xsocket. I am not receiving any events from the server side even when I'm triggering them. In addition, the connection is getting terminated after 30-32 seconds even when I'm manually setting the timeout to 100 seconds. Do you know what could be going wrong?
Legend
@Legend - Not really, did you look at their tutorial ? http://xsocket.sourceforge.net/tutorial/tutorial.htm
Romain Hippeau
@Romain: Yeap. This is from their tutorial. I added the while loop in my code so that the program won't terminate. Other than that, I am not doing anything on my own.
Legend
could be your http client? Maybe try with curl -N http://www.sample.com/AsyncNotify?sessionId=xxxxxx if connection stays open?
Alfred