Publish messages using CometD Java client that can be consumed by Javascript subscribers -
i have java web application uses cometd. workflow simple:
- i have defined service acts upon receiving messages on channel "/service/hello". service expects parameter "name". based on creates channel named:
"/"+message.getdataasmap().get("name")
. channel attaches callback method send message subscribers. - the javascript client (uses dojo) publishes message channel "/service/hello" , subscribes channel name has sent "/service/hello" parameter. lets take example:
.... cometd.subscribe('/1234', function(message) { //do smth on message received; }); cometd.publish('/service/hello', { name: '1234' }); ....
this works fine. ,what want achieve following: have javascript clients subscribers , java client publishing. have tried using examples given in cometd2 documentation java client api, doesn't work expected. seems service called messages aren't seen javascript consumers.
is possible achieve this? ideas of wrong? thanks.
here code on server side:
public class helloservice extends abstractservice { public helloservice(bayeuxserver bayeux) { super(bayeux, "hello"); addservice("/service/hello", "processhello"); }
public void processhello(serversession remote, message message) { map<string, object> input = message.getdataasmap(); string name = (string)input.get("name"); string channelid = "/"+name; addservice(channelid, "processid"); processid(remote, message); } public void processid(serversession remote, message message) { map<string, object> input = message.getdataasmap(); string name = (string)input.get("name"); int = 0; map<string, object> output = new hashmap<string, object>(); while(i<1){ i++; output.put("greeting", "hello, " + name); remote.deliver(getserversession(), "/"+name, output, null); try { thread.sleep(1000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
remote.deliver()
sends "answer" remote
session (i.e. client) published on service channel.
you should publish unsolicited message normal channel (which on server side not exist yet). so, should write like
string channelname = "whatever - not beginning /service"; getbayeux().createifabsent(channelname); getbayeux().getchannel(channelname).publish(getserversession(), output, null);
Comments
Post a Comment