]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/remote/JmsClient.js
Set DEFAULT_NAMESPACE_MAP instead of passing it in each function call
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / argeo-ria-src / class / org / argeo / ria / remote / JmsClient.js
1 qx.Class.define("org.argeo.ria.remote.JmsClient", {
2
3 type : "singleton",
4 extend : qx.core.Object,
5
6 construct : function(){
7 this.base(arguments);
8 },
9 members : {
10 // The URI of the MessageListenerServlet
11 uri : '../amq',
12
13 // Polling. Set to true (default) if waiting poll for messages is needed
14 poll : true,
15 pollTimeout : 25,
16 interrupt : false,
17
18 // Poll delay. if set to positive integer, this is the time to wait in ms before
19 // sending the next poll after the last completes.
20 _pollDelay : 0,
21
22 _first : true,
23 _pollEvent : function(first) {},
24 _handlers : new Array(),
25
26 _messageHandler : function(response) {
27 var doc = response.getContent();
28 var messages = org.argeo.ria.util.Element.selectNodes(doc, "//response");
29 for(var i=0;i<messages.length;i++){
30 var id = messages[i].getAttribute("id");
31 if(id && this._handlers[id]){
32 this._handlers[id](messages[i]);
33 }
34 }
35 },
36
37 _pollHandler : function(response) {
38 try {
39 this._messageHandler(response);
40 this._pollEvent(this._first);
41 this._first = false;
42 } catch (e) {
43 alert(e);
44 }
45
46 if (this._pollDelay > 0)
47 qx.event.Timer.once(this._sendPoll, this, this._pollDelay);
48 else
49 this._sendPoll();
50 },
51
52 _sendPoll : function(request) {
53 if(this.interrupt) return;
54 var request = new qx.io.remote.Request(this.uri, "GET", "application/xml");
55 request.setTimeout(this.pollTimeout*1000+5000);
56 request.addListener("completed", this._pollHandler, this);
57 request.send();
58 },
59
60 // Add a function that gets called on every poll response, after all received
61 // messages have been handled. The poll handler is past a boolean that indicates
62 // if this is the first poll for the page.
63 addPollHandler : function(func) {
64 var old = this._pollEvent;
65 this._pollEvent = function(first) {
66 old(first);
67 func(first);
68 }
69 },
70
71 // Send a JMS message to a destination (eg topic://MY.TOPIC). Message should be xml or encoded
72 // xml content.
73 sendMessage : function(destination, message) {
74 this._sendMessage(destination, message, 'send');
75 },
76
77 // Listen on a channel or topic. handler must be a function taking a message arguement
78 addListener : function(id, destination, handler, context) {
79 this._handlers[id] = qx.lang.Function.bind(handler, context);
80 this._sendMessage(destination, id, 'listen');
81 },
82
83 // remove Listener from channel or topic.
84 removeListener : function(id, destination) {
85 this._handlers[id] = null;
86 this._sendMessage(destination, id, 'unlisten');
87 },
88
89 _sendMessage : function(destination, message, type) {
90 var req = new qx.io.remote.Request(this.uri, "POST", "text/plain");
91 req.setParameter("destination", destination);
92 req.setParameter("message", message);
93 req.setParameter("type", type);
94 //req.addListener("completed", this.endBatch, this);
95 req.send();
96 },
97
98 startPolling : function() {
99 if (this.poll){
100 this.interrupt = false;
101 var req = new qx.io.remote.Request(this.uri, "GET", "application/xml");
102 req.setParameter("timeout", "10");
103 req.addListener("completed", this._pollHandler, this);
104 req.send();
105 }
106 },
107
108 stopPolling : function(){
109 this.interrupt = true;
110 }
111 }
112 });