groovycometd

cometd: published message to a channel does not show up in subscribed listener


I am trying to publish a test data into the cometd-demo server channel members/hello/. handshake done, can get a subscribed message on callback and can get published message on publish() callback. But i can't get that published message on subscribe() listener.


Groovy Script:

import org.cometd.bayeux.Message;
import org.cometd.bayeux.Message.Mutable
import org.cometd.bayeux.client.ClientSessionChannel;
import org.cometd.bayeux.client.ClientSessionChannel.MessageListener;
import org.cometd.client.BayeuxClient
import org.cometd.client.transport.ClientTransport
import org.cometd.client.transport.LongPollingTransport
import org.eclipse.jetty.client.HttpClient as MyHttpClient

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)

client.handshake(30000)

def channel = client.getChannel("/members/hello/")

channel.subscribe(mylistener,mylistener)  

while (true)

{

    sleep(5000)

    channel.publish( 'hai' )

}

class Mylistener implements ClientSessionChannel.MessageListener {

        public void onMessage(ClientSessionChannel channel, Message message) {

            println message

        }

    }

While running this script I can't get the published data on listener even JVM not killed with the while loop. What am I missing?


Solution

  • You have specified incorrect channel path in:

    def channel = client.getChannel("/members/hello/")
    

    Channel path cannot end with / - it should be /members/hello.

    Also double check if you use correct URL. I've used very simple CometD server application (https://github.com/wololock/dojo-jetty9-primer) that uses /dojo-jetty9-primer/ context path, so in my case URL to CometD server was:

    def url = "http://localhost:8080/dojo-jetty9-primer/cometd/"
    

    You can also simplify your script to something like that:

    import org.cometd.bayeux.Message
    import org.cometd.bayeux.client.ClientSessionChannel
    import org.cometd.client.BayeuxClient
    import org.cometd.client.transport.LongPollingTransport
    import org.eclipse.jetty.client.HttpClient
    
    final String url = "http://localhost:8080/dojo-jetty9-primer/cometd/"
    
    final HttpClient httpClient = new HttpClient()
    httpClient.start()
    
    final BayeuxClient client = new BayeuxClient(url, new LongPollingTransport([:], httpClient))
    client.handshake()
    client.waitFor(1000, BayeuxClient.State.CONNECTED)
    
    final ClientSessionChannel channel = client.getChannel("/members/hello")
    channel.subscribe(new MyListener())
    
    while (true) {
        sleep(1000)
        channel.publish("test")
    }
    
    class MyListener implements ClientSessionChannel.MessageListener {
        @Override
        void onMessage(ClientSessionChannel channel, Message message) {
            println "[${new Date()}] Received message from channel (${channel.id}): ${message}"
        }
    }
    

    Especially a part client.handshake(30000) can be simplified in your script - you don't have to wait 30 seconds here.

    When you run it you will see a new message showing up in the console every 1 second:

    [Mon Feb 19 10:15:02 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]
    [Mon Feb 19 10:15:03 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]
    [Mon Feb 19 10:15:04 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]
    [Mon Feb 19 10:15:05 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]
    [Mon Feb 19 10:15:06 CET 2018] Received message from channel (/members/hello): [data:test, channel:/members/hello]
    

    Hope it helps.