]> git.argeo.org Git - gpl/argeo-slc.git/blob - server/org.argeo.slc.ria/src/main/webapp/argeo-ria-src/class/org/argeo/ria/remote/JmsClient.js
Create Argeo SLC RIA project
[gpl/argeo-slc.git] / server / org.argeo.slc.ria / src / main / webapp / argeo-ria-src / class / org / argeo / ria / remote / JmsClient.js
1 /**
2 * A standard client for sending/receiving JMS message.
3 * It is based on ActiveMQ Ajax implementation.
4 */
5 qx.Class.define("org.argeo.ria.remote.JmsClient", {
6
7 type : "singleton",
8 extend : qx.core.Object,
9
10 construct : function(){
11 this.base(arguments);
12 },
13 members : {
14 // The URI of the MessageListenerServlet
15 uri : '../amq',
16
17 // Polling. Set to true (default) if waiting poll for messages is needed
18 poll : true,
19 pollTimeout : 25,
20 interrupt : false,
21
22 // Poll delay. if set to positive integer, this is the time to wait in ms before
23 // sending the next poll after the last completes.
24 _pollDelay : 0,
25
26 _first : true,
27 /**
28 * Trigger at each poll event.
29 * @param first {Boolean} Whether it is the first event to be triggered.
30 */
31 _pollEvent : function(first) {},
32 _handlers : new Array(),
33
34 /**
35 * Parses the XML response to a message POST.
36 * @param response {qx.io.remote.Response} The query response
37 */
38 _messageHandler : function(response) {
39 var doc = response.getContent();
40 var messages = org.argeo.ria.util.Element.selectNodes(doc, "//response");
41 for(var i=0;i<messages.length;i++){
42 var id = messages[i].getAttribute("id");
43 if(id && this._handlers[id]){
44 this._handlers[id](messages[i]);
45 }
46 }
47 },
48
49 /**
50 * Parses the empty response of a poll GET query.
51 * @param response {qx.io.remote.Response} The query response
52 */
53 _pollHandler : function(response) {
54 try {
55 this._messageHandler(response);
56 this._pollEvent(this._first);
57 this._first = false;
58 } catch (e) {
59 alert(e);
60 }
61
62 if (this._pollDelay > 0)
63 qx.event.Timer.once(this._sendPoll, this, this._pollDelay);
64 else
65 this._sendPoll();
66 },
67
68 /**
69 * Send a poll query : GET query and no paramter at all.
70 * @param request {qx.io.remote.Request} A request object
71 */
72 _sendPoll : function(request) {
73 if(this.interrupt) return;
74 var request = new qx.io.remote.Request(this.uri, "GET", "application/xml");
75 request.setTimeout(this.pollTimeout*1000+5000);
76 request.addListener("completed", this._pollHandler, this);
77 request.send();
78 },
79
80 /**
81 * Add a function that gets called on every poll response, after all received
82 * messages have been handled. The poll handler is past a boolean that indicates
83 * if this is the first poll for the page.
84 *
85 * @param func {Function} The handler to be called.
86 */
87 addPollHandler : function(func) {
88 var old = this._pollEvent;
89 this._pollEvent = function(first) {
90 old(first);
91 func(first);
92 }
93 },
94
95 /**
96 * Send a JMS message to a destination (eg topic://MY.TOPIC).
97 * Message should be xml or encoded xml content.
98 *
99 * @param destination {String} The topic destination
100 * @param message {String} XML encoded message
101 * @param properties {Map} A map of additional parameters to add to the query.
102 */
103 sendMessage : function(destination, message, properties) {
104 this._sendMessage(destination, message, 'send', properties);
105 },
106
107 /**
108 * Listen on a channel or topic. handler must be a function taking a message arguement
109 * @param id {String} A unique identifier for this handler
110 * @param destination {String} The topic to listen to (topic://MY.TOPIC)
111 * @param handler {Function} The handler to trigger when receiving a message
112 * @param context {Object} An object to bind on the handler.
113 */
114 addListener : function(id, destination, handler, context) {
115 this._handlers[id] = qx.lang.Function.bind(handler, context);
116 this._sendMessage(destination, id, 'listen');
117 },
118
119 /**
120 * Remove Listener from channel or topic.
121 * @param id {String} identifier of the handler to remove.
122 * @param destination {String} The topic to listen to (topic://MY.TOPIC)
123 */
124 removeListener : function(id, destination) {
125 this._handlers[id] = null;
126 this._sendMessage(destination, id, 'unlisten');
127 },
128
129 /**
130 * Send a message of a given type.
131 * @param destination {String} The topic to listen to (topic://MY.TOPIC)
132 * @param message {String} XML encoded message
133 * @param type {String} The JMS-Type of message (listen, unlisten, send).
134 * @param properties {Map} A map of additional parameters to add to the query.
135 */
136 _sendMessage : function(destination, message, type, properties) {
137 var req = new qx.io.remote.Request(this.uri, "POST", "text/plain");
138 if(!properties) properties = {};
139 properties["destination"] = destination;
140 properties["message"] = message;
141 properties["type"] = type;
142 var vParametersList = [];
143
144 for (var vId in properties)
145 {
146 var value = properties[vId];
147 if (value instanceof Array)
148 {
149 for (var i=0; i<value.length; i++)
150 {
151 vParametersList.push(encodeURIComponent(vId) +
152 "=" +
153 encodeURIComponent(value[i]));
154 }
155 }
156 else
157 {
158 vParametersList.push(encodeURIComponent(vId) +
159 "=" +
160 encodeURIComponent(value));
161 }
162 }
163 if (vParametersList.length > 0)
164 {
165 req.setData(vParametersList.join("&"));
166 }
167
168 //req.addListener("completed", this.endBatch, this);
169 req.send();
170 },
171
172 /**
173 * Starts a poll on the JMS server.
174 */
175 startPolling : function() {
176 if (this.poll){
177 this.interrupt = false;
178 var req = new qx.io.remote.Request(this.uri, "GET", "application/xml");
179 req.setParameter("timeout", "10");
180 req.addListener("completed", this._pollHandler, this);
181 req.send();
182 }
183 },
184
185 /**
186 * Stops polling the JMS server.
187 */
188 stopPolling : function(){
189 this.interrupt = true;
190 }
191 }
192 });