]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/remote/AmqClient.js
f471d2670673abd0cf5c1f20328906c718deefc1
1 qx
.Class
.define("org.argeo.ria.remote.AmqClient", {
3 extend
: qx
.core
.Object
,
4 construct : function(){
8 // The URI of the MessageListenerServlet
9 uri
: '/org.argeo.slc.webapp/amq',
11 // Polling. Set to true (default) if waiting poll for messages is needed
14 // Poll delay. if set to positive integer, this is the time to wait in ms before
15 // sending the next poll after the last completes.
19 _pollEvent : function(first
) {
21 _handlers
: new Array(),
27 _messageHandler : function(response
) {
28 var doc
= response
.getContent();
29 var NSMap
= {slc
:"http://argeo.org/projects/slc/schemas"};
30 var messages
= org
.argeo
.ria
.util
.Element
.selectNodes(doc
, "//response", NSMap
);
31 //this.info("Received " + messages.length + " messages");
32 for(var i
=0;i
<messages
.length
;i
++){
33 var id
= messages
[i
].getAttribute("id");
34 var slcExec
= org
.argeo
.ria
.util
.Element
.selectSingleNode(messages
[i
], "slc:slc-execution", NSMap
);
35 if(id
&& this._handlers
[id
] && slcExec
){
36 this._handlers
[id
](slcExec
);
41 startBatch : function() {
42 this._queueMessages
++;
45 endBatch : function() {
46 this._queueMessages
--;
47 if (this._queueMessages
== 0 && this._messages
> 0) {
48 var body
= this._messageQueue
;
49 this._messageQueue
= '';
51 this._queueMessages
++;
52 var request
= new qx
.io
.remote
.Request(this.uri
, "POST", "text/plain");
53 request
.addListener("completed", this.endBatch
, this);
58 _pollHandler : function(response
) {
61 this._messageHandler(response
);
62 this._pollEvent(this._first
);
69 if (this._pollDelay
> 0)
70 qx
.event
.Timer
.once(this._sendPoll
, this, this._pollDelay
);
75 _sendPoll : function(request
) {
76 var request
= new qx
.io
.remote
.Request(this.uri
, "GET", "application/xml");
77 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.
84 addPollHandler : function(func
) {
85 var old
= this._pollEvent
;
86 this._pollEvent = function(first
) {
92 // Send a JMS message to a destination (eg topic://MY.TOPIC). Message should be xml or encoded
94 sendMessage : function(destination
, message
) {
95 this._sendMessage(destination
, message
, 'send');
98 // Listen on a channel or topic. handler must be a function taking a message arguement
99 addListener : function(id
, destination
, handler
, context
) {
100 this._handlers
[id
] = qx
.lang
.Function
.bind(handler
, context
);
101 this._sendMessage(destination
, id
, 'listen');
104 // remove Listener from channel or topic.
105 removeListener : function(id
, destination
) {
106 this._handlers
[id
] = null;
107 this._sendMessage(destination
, id
, 'unlisten');
110 _sendMessage : function(destination
, message
, type
) {
111 if (this._queueMessages
> 0) {
112 if (this._messages
== 0) {
113 this._messageQueue
= 'destination=' + destination
114 + '&message=' + message
+ '&type=' + type
;
116 this._messageQueue
+= '&d' + this._messages
+ '='
117 + destination
+ '&m' + this._messages
+ '='
118 + message
+ '&t' + this._messages
+ '=' + type
;
123 var req
= new qx
.io
.remote
.Request(this.uri
, "POST", "text/plain");
124 req
.setParameter("destination", destination
);
125 req
.setParameter("message", message
);
126 req
.setParameter("type", type
);
127 req
.addListener("completed", this.endBatch
, this);
132 startPolling : function() {
134 var req
= new qx
.io
.remote
.Request(this.uri
, "GET", "application/xml");
135 req
.setParameter("timeout", "10");
136 req
.addListener("completed", this._pollHandler
, this);