]>
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
2 * A standard client for sending/receiving JMS message.
3 * It is based on ActiveMQ Ajax implementation.
5 qx
.Class
.define("org.argeo.ria.remote.JmsClient", {
8 extend
: qx
.core
.Object
,
10 construct : function(){
14 // The URI of the MessageListenerServlet
17 // Polling. Set to true (default) if waiting poll for messages is needed
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.
28 * Trigger at each poll event.
29 * @param first {Boolean} Whether it is the first event to be triggered.
31 _pollEvent : function(first
) {},
32 _handlers
: new Array(),
35 * Parses the XML response to a message POST.
36 * @param response {qx.io.remote.Response} The query response
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
]);
50 * Parses the empty response of a poll GET query.
51 * @param response {qx.io.remote.Response} The query response
53 _pollHandler : function(response
) {
55 this._messageHandler(response
);
56 this._pollEvent(this._first
);
62 if (this._pollDelay
> 0)
63 qx
.event
.Timer
.once(this._sendPoll
, this, this._pollDelay
);
69 * Send a poll query : GET query and no paramter at all.
70 * @param request {qx.io.remote.Request} A request object
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);
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.
85 * @param func {Function} The handler to be called.
87 addPollHandler : function(func
) {
88 var old
= this._pollEvent
;
89 this._pollEvent = function(first
) {
96 * Send a JMS message to a destination (eg topic://MY.TOPIC).
97 * Message should be xml or encoded xml content.
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.
103 sendMessage : function(destination
, message
, properties
) {
104 this._sendMessage(destination
, message
, 'send', properties
);
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.
114 addListener : function(id
, destination
, handler
, context
) {
115 this._handlers
[id
] = qx
.lang
.Function
.bind(handler
, context
);
116 this._sendMessage(destination
, id
, 'listen');
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)
124 removeListener : function(id
, destination
) {
125 this._handlers
[id
] = null;
126 this._sendMessage(destination
, id
, 'unlisten');
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.
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
= [];
144 for (var vId
in properties
)
146 var value
= properties
[vId
];
147 if (value
instanceof Array
)
149 for (var i
=0; i
<value
.length
; i
++)
151 vParametersList
.push(encodeURIComponent(vId
) +
153 encodeURIComponent(value
[i
]));
158 vParametersList
.push(encodeURIComponent(vId
) +
160 encodeURIComponent(value
));
163 if (vParametersList
.length
> 0)
165 req
.setData(vParametersList
.join("&"));
168 //req.addListener("completed", this.endBatch, this);
173 * Starts a poll on the JMS server.
175 startPolling : function() {
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);
186 * Stops polling the JMS server.
188 stopPolling : function(){
189 this.interrupt
= true;