users@grizzly.java.net

Re: Simple non-servlet example of Bayeux server?

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Mon, 07 Apr 2008 10:32:30 +0200

Hi,

Shahbaz wrote:
> Hi,
> I have a Java server application which receives and sends messages to
> a number of other application (I am using quickfixj.org, for those who
> know what it is). I would like to add 'comet' to the server, so I can
> send server updates to web browsers (and eventually receive info as
> well). Following is a minimalistic example I have tried to implement.
> I get an error, which I don't really understand. I haven't done any
> ajax/comet type development before so please let me know if I am
> missing something obvious.

You have to enable Comet support in Grizzly. The way you usualy do that is:

> SelectorThread st = super.createSelectorThread(args);
> st.setEnableAsyncExecution(true);
> st.setBufferResponse(false);
> st.setFileCacheIsEnabled(false);
> st.setLargeFileCacheEnabled(false);
> AsyncHandler asyncHandler = new DefaultAsyncHandler();
> asyncHandler.addAsyncFilter(new CometAsyncFilter());
> st.setAsyncHandler(asyncHandler);

Take a look at this class and its parent to see how to start the
SelectorThread properly:

https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/standalone/Comet.html

Let us know what you get :-)

- Jeanfrancois


>
> Error:
> Exception in thread "main" java.lang.IllegalStateException: Grizzly
> Comet hasn't been registered
> at com.sun.grizzly.comet.CometContext.addCometHandler(CometContext.java:220)
> at com.sun.grizzly.comet.CometContext.addCometHandler(CometContext.java:266)
> at com.mpn.test.CometTest.main(CometTest.java:25)
>
> Code:
> ---------------------CometTest.java:
> package com.mpn.test;
>
>
>
> import java.io.IOException;
>
>
> import com.sun.grizzly.comet.CometContext;
>
> import com.sun.grizzly.comet.CometEngine;
>
> import com.sun.grizzly.cometd.BayeuxCometHandler;
>
>
>
>
> public class CometTest {
>
>
>
> public CometTest() throws IOException{
>
>
> }
>
>
>
> public static void main(String[] args) throws IOException {
>
>
>
> CometTest test = new CometTest();
>
>
>
> CometEngine cometEngine = CometEngine.getEngine();
>
> CometContext context=cometEngine.register("/comet");
>
> context.addCometHandler(new BayeuxCometHandler());
>
> context.notify("hello world!");
>
>
> }
>
>
> }
>
>
> -----------------------MyBayeuxCometHandler.java: (I just print out
> onXXX(..) events, nothing else is changed)
> package com.mpn.test;
>
> import java.io.IOException;
>
> import com.sun.grizzly.comet.CometEvent;
> import com.sun.grizzly.cometd.BayeuxCometHandler;
>
> public class MyBayeuxCometHandler extends BayeuxCometHandler {
>
> public MyBayeuxCometHandler() {
> // TODO Auto-generated constructor stub
> }
>
> @Override
> public String getChannel() {
> // TODO Auto-generated method stub
> return super.getChannel();
> }
>
> @Override
> public void onConnect(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onConnect(arg0);
> System.out.println("onConnect: "+arg0);
> }
>
> @Override
> public void onDisconnect(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onDisconnect(arg0);
> System.out.println("onDisconnect: "+arg0);
> }
>
> @Override
> public void onHandshake(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onHandshake(arg0);
> System.out.println("onHandshake: "+arg0);
> }
>
> @Override
> public void onInterrupt(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onInterrupt(arg0);
> System.out.println("onInterrupt: "+arg0);
> }
>
> @Override
> public void onPing(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onPing(arg0);
> System.out.println("onPing: "+arg0);
> }
>
> @Override
> public void onPublish(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onPublish(arg0);
> System.out.println("onPublish: "+arg0);
> }
>
> @Override
> public void onReconnect(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onReconnect(arg0);
> System.out.println("onReconnect: "+arg0);
> }
>
> @Override
> public void onStatus(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onStatus(arg0);
> System.out.println("onStatus: "+arg0);
> }
>
> @Override
> public void onSubscribe(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onSubscribe(arg0);
> System.out.println("onSubscribe: "+arg0);
> }
>
> @Override
> public void onTerminate(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onTerminate(arg0);
> System.out.println("onTerminate: "+arg0);
> }
>
> @Override
> public void onUnsubscribe(CometEvent arg0) throws IOException {
> // TODO Auto-generated method stub
> super.onUnsubscribe(arg0);
> System.out.println("onUnsubscribe: "+arg0);
> }
>
> @Override
> public void setChannel(String arg0) {
> // TODO Auto-generated method stub
> super.setChannel(arg0);
> }
>
> }
>
>
> Thanks
> Shahbaz
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>