Message Framework Topic subscribe for javascript/flex client
Requirements Topic, message management Publish topic messages to javascript client Publish topic messages to flex client Subscribe topic by javascript client Subscribe topic by flex client
System Topology JavaScript Client Flex Client Domain A Rest Framework Message Framework Other Component Topic Manager dwr Flex data service subscribe publish subscribe
TopicManager Rest framework or other components can send topic messages by TopicManager API: protected void notifyJSClient(String topic, List messages) Send messages to javascript client Send messages to flex client
Send message to javascript DWR DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible. DWR Reverse Ajax, it gives us the ability to asynchronously send data from a web-server to a browser. DWR Reverse Ajax supports 3 methods of pushing the data from to the browser: Piggyback, Polling and Comet. More detail refer to
Send message to javascript Using dwr to manager the topics, messages and clients Public api in TopicManager: protected void notifyJSClient(String topic, List messages) Dwr configuration, refer to web.xml: dwr-invoker DWR Servlet Direct Web Remoter Servlet com.vitria.dwr.servlet.OSGiDwrServlet …… org.directwebremoting.extend.Compressor org.directwebremoting.impl.LegacyCompressor
activeReverseAjaxEnabled true <!-- This enables full streaming mode. If set to -1, it will using full streaming model totally. If it is set to <500, client will restart a new connection without wait after write which is the same with set to 0. It's probably better to leave this out if you are running across the internet. --> maxWaitAfterWrite 5000 <!-- This enables polling streaming mode, set the wait time before next polling. For polling model only. For "Early Closing Mode", it will compute the disconnectedTime dynamically according the server load--> disconnectedTime 5000 url:/call/plainpoll/ com.vitria.dwr.reverse.TopicPollHandler ……
Send messages to flex client Using flex BlazeDS to implement Public api in TopicManager: protected void notifyASClient(String topic, List messages) BlazeDS configuration, refer to messaging-config.xml: false
Javascript client Based on DWR Reverse Ajax APIs, refer to engine.js New APIs for subscribe topics, refer to reverse.js: /** * Register the callback on one topic and the reverse ajax will be started when register. topic_id, the topic id to subscribe. callback, the listener on the topic, should accept one parameter just like {"topic": "topic_id", "messages": ["message1", "message2"]}. **/ dwr.reverse.register = function(topic_id, callback) { } /** * Unregister the callback on one topic. topic_id, the topic id to unsubscribe. callback, the listener on the topic; if null, the whole topic will unsubscribe. **/ dwr.reverse.unregister = function(topic_id, callback) { }
Flex client Based on Flex Messaging APIs New APIs for subscribe topics, refer to RestMessageManager.as: /** * Register the callback on one topic. topic_id, the topic id to subscribe. callback, the listener on the topic, should accept one parameter of TopicMessage instance. **/ public function register(topic:String, callback:Function):void { } /** * Unregister the callback on one topic. topic_id, the topic id to unsubscribe. callback, the listener on the topic; if null, the whole topic will unsubscribe. **/ public function unregister(topic:String, callback:Function):void { }