+/**\r
+ * A standard client for sending/receiving JMS message.\r
+ * It is based on ActiveMQ Ajax implementation.\r
+ */\r
qx.Class.define("org.argeo.ria.remote.JmsClient", {\r
\r
type : "singleton",\r
},\r
members : {\r
// The URI of the MessageListenerServlet\r
- uri : '/org.argeo.slc.webapp/amq', \r
+ uri : '../amq', \r
\r
// Polling. Set to true (default) if waiting poll for messages is needed\r
poll : true,\r
_pollDelay : 0,\r
\r
_first : true,\r
+ /**\r
+ * Trigger at each poll event.\r
+ * @param first {Boolean} Whether it is the first event to be triggered. \r
+ */\r
_pollEvent : function(first) {},\r
_handlers : new Array(),\r
\r
+ /**\r
+ * Parses the XML response to a message POST.\r
+ * @param response {qx.io.remote.Response} The query response\r
+ */\r
_messageHandler : function(response) {\r
- var doc = response.getContent();\r
- var NSMap = {slc:"http://argeo.org/projects/slc/schemas"};\r
- var messages = org.argeo.ria.util.Element.selectNodes(doc, "//response", NSMap);\r
+ var doc = response.getContent(); \r
+ var messages = org.argeo.ria.util.Element.selectNodes(doc, "//response");\r
for(var i=0;i<messages.length;i++){\r
var id = messages[i].getAttribute("id");\r
if(id && this._handlers[id]){\r
}\r
},\r
\r
+ /**\r
+ * Parses the empty response of a poll GET query.\r
+ * @param response {qx.io.remote.Response} The query response\r
+ */\r
_pollHandler : function(response) {\r
try {\r
this._messageHandler(response);\r
this._sendPoll();\r
},\r
\r
+ /**\r
+ * Send a poll query : GET query and no paramter at all. \r
+ * @param request {qx.io.remote.Request} A request object\r
+ */\r
_sendPoll : function(request) {\r
if(this.interrupt) return;\r
var request = new qx.io.remote.Request(this.uri, "GET", "application/xml");\r
request.send();\r
},\r
\r
- // Add a function that gets called on every poll response, after all received\r
- // messages have been handled. The poll handler is past a boolean that indicates\r
- // if this is the first poll for the page.\r
+ /**\r
+ * Add a function that gets called on every poll response, after all received\r
+ * messages have been handled. The poll handler is past a boolean that indicates\r
+ * if this is the first poll for the page.\r
+ * \r
+ * @param func {Function} The handler to be called. \r
+ */\r
addPollHandler : function(func) {\r
var old = this._pollEvent;\r
this._pollEvent = function(first) {\r
}\r
},\r
\r
- // Send a JMS message to a destination (eg topic://MY.TOPIC). Message should be xml or encoded\r
- // xml content.\r
- sendMessage : function(destination, message) {\r
- this._sendMessage(destination, message, 'send');\r
+ /**\r
+ * Send a JMS message to a destination (eg topic://MY.TOPIC). \r
+ * Message should be xml or encoded xml content.\r
+ * \r
+ * @param destination {String} The topic destination\r
+ * @param message {String} XML encoded message\r
+ * @param properties {Map} A map of additional parameters to add to the query.\r
+ */\r
+ sendMessage : function(destination, message, properties) {\r
+ this._sendMessage(destination, message, 'send', properties);\r
},\r
\r
- // Listen on a channel or topic. handler must be a function taking a message arguement\r
+ /**\r
+ * Listen on a channel or topic. handler must be a function taking a message arguement\r
+ * @param id {String} A unique identifier for this handler\r
+ * @param destination {String} The topic to listen to (topic://MY.TOPIC) \r
+ * @param handler {Function} The handler to trigger when receiving a message \r
+ * @param context {Object} An object to bind on the handler.\r
+ */\r
addListener : function(id, destination, handler, context) {\r
this._handlers[id] = qx.lang.Function.bind(handler, context);\r
this._sendMessage(destination, id, 'listen');\r
},\r
\r
- // remove Listener from channel or topic.\r
+ /**\r
+ * Remove Listener from channel or topic.\r
+ * @param id {String} identifier of the handler to remove.\r
+ * @param destination {String} The topic to listen to (topic://MY.TOPIC) \r
+ */ \r
removeListener : function(id, destination) {\r
this._handlers[id] = null;\r
this._sendMessage(destination, id, 'unlisten');\r
},\r
-\r
- _sendMessage : function(destination, message, type) {\r
+ \r
+ /**\r
+ * Send a message of a given type.\r
+ * @param destination {String} The topic to listen to (topic://MY.TOPIC) \r
+ * @param message {String} XML encoded message\r
+ * @param type {String} The JMS-Type of message (listen, unlisten, send).\r
+ * @param properties {Map} A map of additional parameters to add to the query.\r
+ */\r
+ _sendMessage : function(destination, message, type, properties) {\r
var req = new qx.io.remote.Request(this.uri, "POST", "text/plain");\r
- req.setParameter("destination", destination);\r
- req.setParameter("message", message);\r
- req.setParameter("type", type);\r
+ if(!properties) properties = {}; \r
+ properties["destination"] = destination;\r
+ properties["message"] = message;\r
+ properties["type"] = type;\r
+ var vParametersList = [];\r
+ \r
+ for (var vId in properties)\r
+ {\r
+ var value = properties[vId];\r
+ if (value instanceof Array)\r
+ {\r
+ for (var i=0; i<value.length; i++)\r
+ {\r
+ vParametersList.push(encodeURIComponent(vId) +\r
+ "=" +\r
+ encodeURIComponent(value[i]));\r
+ }\r
+ }\r
+ else\r
+ {\r
+ vParametersList.push(encodeURIComponent(vId) +\r
+ "=" +\r
+ encodeURIComponent(value));\r
+ }\r
+ } \r
+ if (vParametersList.length > 0)\r
+ {\r
+ req.setData(vParametersList.join("&"));\r
+ }\r
+ \r
//req.addListener("completed", this.endBatch, this);\r
req.send();\r
},\r
\r
+ /**\r
+ * Starts a poll on the JMS server.\r
+ */\r
startPolling : function() {\r
if (this.poll){\r
this.interrupt = false;\r
}\r
},\r
\r
+ /**\r
+ * Stops polling the JMS server.\r
+ */\r
stopPolling : function(){\r
this.interrupt = true;\r
}\r