groovycometd

cometd bayeux client can't get subscribed acknowledgement?


Actually i am running cometd-demo server in my local using maven jetty run shown in the doc https://docs.cometd.org/current/reference/ and trying to subscribe and publish something in a broadcast channel. Using Groovy script shown below,

ClientSessionChannel.MessageListener mylistener = new Mylistener();  

def myurl = "http://localhost:8080/cometd/"

MyHttpClient httpClient = new MyHttpClient();

httpClient.start()

Map<String, Object> options = new HashMap<String, Object>();

ClientTransport transport = new LongPollingTransport(options, httpClient);

BayeuxClient client = new BayeuxClient(myurl, transport)

println 'client started on URL : '+ client.getURL()


client.handshake ( new ClientSessionChannel.MessageListener() {

    public void onMessage(ClientSessionChannel channel, Message message) {
        if (message.isSuccessful()) {   
            println 'Handshake Message : ' + message 
        }
    }
})

boolean handshakecheck = client.waitFor(1000, BayeuxClient.State.CONNECTED);

println 'Handshake check : '+ handshakecheck

    client.batch( new Runnable() {

        public void run() {

            client.getChannel("/foo/hello").subscribe(

                    new ClientSessionChannel.MessageListener() {

                        public void onMessage(ClientSessionChannel channel,
                                              Message message) {
                                println "subscribed : "+ message
                        }
                    })
        }

    });

The program Output :

client started on URL : http://localhost:8080/cometd/
Handshake Message : [minimumVersion:1.0, clientId:fv0ozxw8cb5e11vtlwpacm7afp, supportedConnectionTypes:[websocket, long-polling, callback-polling], advice:[reconnect:retry, interval:0, maxInterval:10000, timeout:20000], channel:/meta/handshake, id:1, version:1.0, successful:true]
Handshake check : true

Here I can't get the subscribed message as in the code. But in server log It prints like shown below,

2018-02-12 20:30:32,687 qtp2069584894-17 [ INFO][examples.CometDDemoServlet] Monitored Subscribe from fv0ozxw8cb5e11vtlwpacm7afp,last=0,expire=0 for /foo/hello

Update 1: Also i can't subscribe with callback method, i get the message as [channel:/meta/subscribe, id:4, subscription:/foo/hello, error:403:denied_by_not_granting:create_denied, successful:false]. I don't know what i am doing wrong ? I am just following the documentation steps. Thanks in advance.


Solution

  • The ClientSessionChannel.MessageListener that you pass to the subscribe(...) method will be invoked whenever a message will be published on channel /foo/hello.

    Your program never publishes a message on that channel, so the listener is never invoked, therefore in your code subscribed is never printed.

    You want to double check what version of the subscribe() method you want to use, as there are 2 versions.

    The single parameter version takes a listener, while the two parameter version takes a listener and a callback.

    Guessing from your code, you want the subscribed log line be in the callback not in the listener, so you just need to change your code to use the two parameter version of the subscribe() method.

    Also, pay attention to the fact that if the JVM exits at the end of your groovy script, then that client will be gone and will never receive any message.