Search code examples
javacometcometd

implementation in cometD


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);                  
}

}


Solution

  • 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)