users@grizzly.java.net

Re: comet help

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Mon, 15 Oct 2007 20:06:52 -0400

Hi Minh,

The problem seems to be related to the hash code used to notify the
second client. I suspect line 56:

int hashCode = context.addCometHandler(handler);

is not called when you invoke the sendMessage(....):

int hashCode = handler.hashCode();
context.notify(message, CometEvent.NOTIFY, hashCode);

this is probably line 158 that produce the exception. Internally, what
it does is:

> 443 public void notify(final E attachment,final int eventType,final int cometHandlerID)
> 444 throws IOException{
> 445 CometHandler cometHandler = getCometHandler(cometHandlerID);
> 446
> 447 if (cometHandler == null){
> 448 throw new IllegalStateException(INVALID_COMET_HANDLER);
> 449 }

Here the getCometHandler() will return null if the addCometHandler
hasn't been invoked previously. Can you double check your CometHandler
has been added?

You code looks good (and you seems to use the non blocking read/write,
which is something I still need to blog on :-) You will beat me on those
features :-) :-)

Thanks!

-- Jeanfrancois



Minh O wrote:
> Hi Jeanfrancois,
>
> I'm using glassfish-installer-v2-b58g-windows
> <http://java.net/download/javaee5/v2_branch/promoted/WINNT/glassfish-installer-v2-b58g.jar>
>
> For the part where the resume is happening, I assume that you are
> refering to
>
> "//event.getCometContext().resumeCometHandler(this);"
>
> That was in originally but I didn't work for me so I commented it out
> and add the these 2 lines below it and that still didn't work so I left
> them all commented out.
>
> "//event.getCometContext().registerAsyncRead(this);
> //event.getCometContext().registerAsyncWrite(this);"
>
> Thanks for responding so quickly, you probably get tons of emails for
> help. Once I get this working, I'd like to post a tutorial on this as I
> think it would be a great example and provide a lot of insight to how it
> works.
>
> Thanks again,
>
> Minh
>
> > Date: Mon, 15 Oct 2007 09:50:01 -0400
> > From: Jeanfrancois.Arcand_at_Sun.COM
> > Subject: Re: comet help
> > To: users_at_grizzly.dev.java.net
> > CC: minh13_o_at_hotmail.com
> >
> > Hi Minh,
> >
> >
> >
> >
> > Minh O wrote:
> > > Hi Jeanfrancois,
> > >
> > > I've been trying to implement a chat program where a user can send a
> > > message to his/her buddies. I've implemented it so that there are only
> > > 2 people that can send messages to each other but when I send a
> post to
> > > forward to a user, I get this message.
> > >
> > > 'java.lang.IllegalStateException: CometHandler cannot be null. This
> > > CometHandler
> > > was probably resumed and an invalid reference was made to it.'
> > >
> > > Below is the code that is invoked to forward the message to the user.
> > >
> > > {code}
> > > public void sendMessage(Message message, CometHandler handler) {
> > >
> > > if(handler != null) {
> > > try {
> > > CometEngine engine = CometEngine.getEngine();
> > > CometContext context =
> > > engine.getCometContext(CHAT_CONTEXT_PATH);
> > > int hashCode = handler.hashCode();
> > > sLogger.info('[CometChat2]\tSending message to: ' +
> > > message.getTo()
> > > + ' from: ' + message.getFrom() + ' handler
> > > hashcode: ' + hashCode);
> > > context.notify(message, CometEvent.NOTIFY, hashCode);
> > > } catch (IOException e) {
> > > // TODO Auto-generated catch block
> > > e.printStackTrace();
> > > }
> > > } else {
> > > sLogger.severe('[CometChat]\tHandler is null for sending
> > > message to: ' + message.getTo());
> > > }
> > > }
> > > {code}
> > >
> > >
> > > This is what is in the CometHandler implementation.
> > > {code}
> > > @Override
> > > public void onEvent(CometEvent event) throws IOException {
> > > // TODO Auto-generated method stub
> > > sLogger.info('[CometChatHandler]\tEvent type is: ' +
> > > event.getType());
> > > if(event.getType() == CometEvent.NOTIFY) {
> > > Object obj = event.attachment();
> > >
> > > if(obj != null && obj instanceof Message) {
> > > PrintWriter writer = response.getWriter();
> > > writer.write(XStreamUtil.objectToJSON('jsonMSG', obj));
> > > writer.flush();
> > >
> > > //event.getCometContext().resumeCometHandler(this);
> > > //event.getCometContext().registerAsyncRead(this);
> > > //event.getCometContext().registerAsyncWrite(this);
> > >
> > > } else {
> > > sLogger.severe('[CometChatHandler]\tEvent attachment is
> > > not a Message obj: ' + obj.getClass().getName());
> > > }
> > > }
> > > }
> > > {code}
> > >
> > > I've attach the servlet code and the handler code. If you could point
> > > me to the right direction, it would be greatly appreciated.
> >
> > Hum...this is strange as your code that resume the CometHandler is
> > commented out. Let me take a look . Are you using GlassFish or Grizzly
> > standalone?
> >
> > Thanks
> >
> > -- Jeanfrancois
> >
> >
> >
> > >
> > > Minh
> > >
> > >
> ------------------------------------------------------------------------
> > > Climb to the top of the charts! Play Star Shuffle: the word scramble
> > > challenge with star power. Play Now!
> > >
> <http://club.live.com/star_shuffle.aspx?icid=starshuffle_wlmailtextlink_oct>
> > >
> ------------------------------------------------------------------------
> > >
> > > Here is a simple AJAX Request.
> > >
> > >
> > >
> ------------------------------------------------------------------------
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> > > For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>
> ------------------------------------------------------------------------
> Climb to the top of the charts! Play Star Shuffle: the word scramble
> challenge with star power. Play Now!
> <http://club.live.com/star_shuffle.aspx?icid=starshuffle_wlmailtextlink_oct>