4

I am new to CometD, Is there any simple example to implement service channel model in case of response/request model. I have seen cometd.org but there is no such example to how to send the response back if i publish to any channel.

This is the client side

alert("channel published1");
    dojox.cometd.publish('/service/getlist');   
    alert("channel published");
    dojox.cometd.subscribe('/service/getlist', function(message) {
        alert(message);
    });

this is server side "ConfigurationServlet"

bayeux.createIfAbsent("/service/getlist", new ConfigurableServerChannel.Initializer() {

        //new EchoService(bayeux);
        @Override
        public void configureChannel(ConfigurableServerChannel channel) {
            /*channel.setPersistent(true);
            GetListChannelListener channelListner = new GetOrderListChannelListener();
            channel.addListener(channelListner);*/
            new EchoService(bayeux);
        }
    });

EchoService

public class EchoService extends AbstractService{
public EchoService(BayeuxServer bayeuxServer)                                 
{
    super(bayeuxServer, "getlist");                                              
    addService("/service/getlist", "processEcho");                                       
}

public void processEcho(ServerSession remote,Map<String, Object> data)
{       
    try{
    System.out.println("Start Process Echo");
    getBayeux().getChannel("/service/getlist").publish(getServerSession(), "Hello", null);
    System.out.println("End Process Echo");
    }catch(Exception exp){
        exp.printStackTrace();
    }
    //remote.deliver(getServerSession(), "/service/getlist", data, null);                  
}

}

JN_newbie
  • 5,492
  • 14
  • 59
  • 97

1 Answers1

4

On http://cometd.org there is everything you need.

In order to build a very simple example (web application with Javascript client) you need to read in particular:

  • This for the client side
  • This for the server side (configuration)
  • This for the server side (code). From this menu you may want to start with using the first and third bullet points: Inherited Services for the code that echoes the inputed message, and Server Services Integration for the setup of the Bayeux Server through a configuration servlet.

In the pages that I have linked there is all the code necessary, just copy and paste it. In case, come back with more specific questions.


EDITED

After looking at your code, I see that for the service configuration you need to copy the code for ConfigurationServlet class from here and for the EchoService class you need to modify the processEcho method as follows:

remote.deliver(getServerSession(), "/echo", data, null);

with data being an HashMap defined as explained here (first example).

On the client side, I'd subscribe to the channel before publishing your request (I am not sure if it works in your way either)

perissf
  • 15,979
  • 14
  • 80
  • 117
  • thanks for your comment. I am having a problem rite now while studying i.e. suppose if i have publish and channel in javascript and also subscribe. while publish i am calling a servlet. now i am little bit confuse over here to how to send a msg back to the client(javascript) from the servlet. i have seen example, but i am not getting the point over here. – JN_newbie Dec 13 '11 at 10:14
  • Please update your question by adding the code you have written so far – perissf Dec 13 '11 at 10:22
  • now i want how to send a response back to a javascript subscription callback function. – JN_newbie Dec 13 '11 at 13:13
  • Thanks perissf for your precious time. Will get more clearity of concepts, if I got stuck somewhere in this. Thanks once again. – JN_newbie Dec 14 '11 at 06:00
  • Perissf i am now getting a problem again i have posted a question on this http://stackoverflow.com/questions/8519878/cometd-publish-a-message-back-to-a-client. Have a look. Thanks – JN_newbie Dec 15 '11 at 12:08