Cometd and Jetty

36 views
Skip to first unread message

JigarPrajapati

unread,
Aug 8, 2008, 9:10:15 AM8/8/08
to cometd-users
Hi,

I have run cometd chat application in jetty successfully. I have
tried to understand that implementation but i did not get any details
for that on net. I have below questions. if someone should be able to
guide me.

my requirement is, My client(browser should work as just subscriber
who will listen for messages using dojo client side implementation).
Jetty server will have component which will process message coming
from MQ and will send that message to specific channel from jetty
server only not from browser using dojo publish method.


can anybody tell me that which Bayeux code as well jetty cometd
classes/method should i use at server side so i can send message to
specific channel from server only. instread of getting from browser
and sending back to browser.

Your help will be highly appriciated.

Regards,
Jigar

Geoffrey Lee

unread,
Aug 8, 2008, 11:09:34 PM8/8/08
to cometd...@googlegroups.com
Look at the example client code in jetty/contrib/cometd/client/src/main/java/org/mortbay/cometd/client/BayeauxClient.java

-Geoffrey Lee

JigarPrajapati

unread,
Aug 10, 2008, 4:54:17 AM8/10/08
to cometd-users
Hi,

I have gone through this class but they are not using this class
anywhere. Please provide me more details.

Regards,
Jigar


On Aug 9, 8:09 am, "Geoffrey Lee" <geoffreyj....@gmail.com> wrote:
> Look at the example client code in
> jetty/contrib/cometd/client/src/main/java/org/mortbay/cometd/client/BayeauxClient.java
>
> -Geoffrey Lee
>

Geoffrey Lee

unread,
Aug 10, 2008, 9:25:20 PM8/10/08
to cometd...@googlegroups.com
The BayeuxClient example class is used by executing: jetty/contrib/cometd/client/bin/run.sh

More specifically, the above shell script runs jetty/contrib/cometd/client/src/test/java/org/mortbay/cometd/client/BayeuxLoadGenerator.java class which utilizes the BayeuxClient class to perform a stress test on the server.

-Geoffrey Lee

Greg Wilkins

unread,
Aug 11, 2008, 1:25:16 AM8/11/08
to cometd...@googlegroups.com

Jigar,

you need to use the API of the Bayeux object.


Something like:

Client myClient = bayeux.newClient("myService");

Channel channel = bayeux.getChannel("/my/channel",true);

Object myMessage = getObjectFromMQ();

channel.publish(myClient,myMessage,null);


cheers

JigarPrajapati

unread,
Aug 11, 2008, 10:38:52 AM8/11/08
to cometd-users
Hi Greg,


Thanks for quick guide. I just have below question.

1) if i keep client name as myService then does it matter?
2) only channel is important for delivering message to Cometd?
3) can you provide me api for creating bayeux object?
4) is it mandatory to subscribe for publishing message, or we can just
use publish method?

Hope your answer for my above question will help me in creating
server side cometd component.

Thanks in advance.

Regards,
Jigar

Dave Crane

unread,
Aug 12, 2008, 4:16:07 AM8/12/08
to cometd...@googlegroups.com
Hi Jigar,

Rather than creating a Bayeux object, you'll want to grab a reference to the
one being used by the cometd servlets. In the current Jetty implementation,
these are accessed via the ServletContext, so something like this:

Bayeux bayeux =
(Bayeux)servletContext.getAttribute(dojox.cometd.Bayeux.DOJOX_COMETD_BAYEUX);

Have a look at the code for the abstract cometd servlet, you'll see it creatin
a bayeux and registering it as an attribute of the servlet context under this
name.

ServletContexts are one-per-JVM, so this approach scales up to the point at
which you want to do clustering. I think I've seen some discussion on the
next step up being to store the Bayeux client in a JNDI, a la EJB2, but I
think that was speculative rather than an actual implementation.

In the code I've been writing, I'm creating one client per servlet instance
(actually, per controller instance, as I'm using Groovy on Grails, but same
principle), and give them a unique name by appending a random number on the
end of a standard prefix such as "myService_". The bayeux client is an
instance member of the servlet, and gets created on demand first time the
server performs a publish operation.

Greg, I'd be interested to know if you've any comments on this approach, or do
you suggest creating a new client for every publish operation? (I guess in
part it depends on whether you want the server-side client to also subscribe
to channels - I'm still using standard 'old-fashioned' Ajax when I want the
client to tell the server anything.) Is it expensive to create a new client?

Cheers, HTH

Dave

JigarPrajapati

unread,
Aug 13, 2008, 12:27:39 AM8/13/08
to cometd-users
Hi Dave,

I had tried to get Bayeux object from servletcontext but i was
getting as null. Can you provide me code which is working fine in
Jetty so i can try.

is there any other way to create Bayeux object so that wd can follow
method which was provided by Greg.

Please help me.

Regards,
Jigar

Greg Wilkins

unread,
Aug 13, 2008, 7:17:55 PM8/13/08
to cometd...@googlegroups.com
JigarPrajapati wrote:
> Hi Dave,
>
> I had tried to get Bayeux object from servletcontext but i was
> getting as null. Can you provide me code which is working fine in
> Jetty so i can try.
>
> is there any other way to create Bayeux object so that wd can follow
> method which was provided by Greg.

the following code works

http://svn.codehaus.org/jetty/jetty/trunk/modules/extra/cometd/demo/src/main/java/org/cometd/demo/CometdDemo.java

Note that it uses a Context Attribute Listener to detect when the Bayeux object
is set on the context. If you try to get it too soon, it will be null.

cheers

Reply all
Reply to author
Forward
0 new messages