]>
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
4952561d5c18c3b600c054dff93ee4c8aaa4782d
1 qx
.Class
.define("org.argeo.ria.remote.AmqClient", {
3 extend
: qx
.core
.Object
,
4 construct : function(){
8 // The URI of the MessageListenerServlet
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(request
) {
29 if (request
.status
== 200) {
30 var response
= request
.responseXML
31 .getElementsByTagName("ajax-response");
32 if (response
!= null && response
.length
== 1) {
33 for (var i
= 0; i
< response
[0].childNodes
.length
; i
++) {
34 var responseElement
= response
[0].childNodes
[i
];
36 // only process nodes of type element.....
37 if (responseElement
.nodeType
!= 1)
40 var id
= responseElement
.getAttribute('id');
42 var handler
= this._handlers
[id
];
43 if (handler
!= null) {
44 for (var j
= 0; j
< responseElement
.childNodes
.length
; j
++) {
45 handler(responseElement
.childNodes
[j
]);
56 startBatch : function() {
57 this._queueMessages
++;
60 endBatch : function() {
61 this._queueMessages
--;
62 if (this._queueMessages
== 0 && this._messages
> 0) {
63 var body
= this._messageQueue
;
64 this._messageQueue
= '';
66 this._queueMessages
++;
67 var request
= new qx
.io
.remote
.Request(this.uri
, "post", "text/plain");
68 request
.addListener("completed", this.endBatch
, this);
73 _pollHandler : function(request
) {
76 this._messageHandler(request
);
77 this._pollEvent(this._first
);
84 if (this._pollDelay
> 0)
85 qx
.event
.Timer
.once(this._sendPoll
, this, this._pollDelay
);
90 _sendPoll : function(request
) {
91 var request
= new qx
.io
.remote
.Request(this.uri
, "get", "application/xml");
92 request
.addListener("completed", this._pollHandler
, this);
96 // Add a function that gets called on every poll response, after all received
97 // messages have been handled. The poll handler is past a boolean that indicates
98 // if this is the first poll for the page.
99 addPollHandler : function(func
) {
100 var old
= this._pollEvent
;
101 this._pollEvent = function(first
) {
107 // Send a JMS message to a destination (eg topic://MY.TOPIC). Message should be xml or encoded
109 sendMessage : function(destination
, message
) {
110 this._sendMessage(destination
, message
, 'send');
113 // Listen on a channel or topic. handler must be a function taking a message arguement
114 addListener : function(id
, destination
, handler
) {
115 this._handlers
[id
] = handler
;
116 this._sendMessage(destination
, id
, 'listen');
119 // remove Listener from channel or topic.
120 removeListener : function(id
, destination
) {
121 this._handlers
[id
] = null;
122 this._sendMessage(destination
, id
, 'unlisten');
125 _sendMessage : function(destination
, message
, type
) {
126 if (this._queueMessages
> 0) {
127 if (this._messages
== 0) {
128 this._messageQueue
= 'destination=' + destination
129 + '&message=' + message
+ '&type=' + type
;
131 this._messageQueue
+= '&d' + this._messages
+ '='
132 + destination
+ '&m' + this._messages
+ '='
133 + message
+ '&t' + this._messages
+ '=' + type
;
138 var req
= new qx
.io
.remote
.Request(this.uri
, "post", "application/xml");
139 req
.setParameter("destination", destination
);
140 req
.setParameter("message", message
);
141 req
.setParameter("type", type
);
142 req
.addListener("completed", this.endBatch
, this);
147 _startPolling : function() {
149 var req
= new qx
.io
.remote
.Request(this.uri
, "get", "application/xml");
150 req
.setParameter("timeout", "10");
151 req
.addListener("completed", this._pollHandler
, this);