]> 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
792b87c164c2e5b5b6f7f97cb35b0d1c531cab38
[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 : '/org.argeo.slc.webapp/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 NSMap = {slc:"http://argeo.org/projects/slc/schemas"};
29 var messages = org.argeo.ria.util.Element.selectNodes(doc, "//response", NSMap);
30 for(var i=0;i<messages.length;i++){
31 var id = messages[i].getAttribute("id");
32 if(id && this._handlers[id]){
33 this._handlers[id](messages[i]);
34 }
35 }
36 },
37
38 _pollHandler : function(response) {
39 try {
40 this._messageHandler(response);
41 this._pollEvent(this._first);
42 this._first = false;
43 } catch (e) {
44 alert(e);
45 }
46
47 if (this._pollDelay > 0)
48 qx.event.Timer.once(this._sendPoll, this, this._pollDelay);
49 else
50 this._sendPoll();
51 },
52
53 _sendPoll : function(request) {
54 if(this.interrupt) return;
55 var request = new qx.io.remote.Request(this.uri, "GET", "application/xml");
56 request.setTimeout(this.pollTimeout*1000+5000);
57 request.addListener("completed", this._pollHandler, this);
58 request.send();
59 },
60
61 // Add a function that gets called on every poll response, after all received
62 // messages have been handled. The poll handler is past a boolean that indicates
63 // if this is the first poll for the page.
64 addPollHandler : function(func) {
65 var old = this._pollEvent;
66 this._pollEvent = function(first) {
67 old(first);
68 func(first);
69 }
70 },
71
72 // Send a JMS message to a destination (eg topic://MY.TOPIC). Message should be xml or encoded
73 // xml content.
74 sendMessage : function(destination, message) {
75 this._sendMessage(destination, message, 'send');
76 },
77
78 // Listen on a channel or topic. handler must be a function taking a message arguement
79 addListener : function(id, destination, handler, context) {
80 this._handlers[id] = qx.lang.Function.bind(handler, context);
81 this._sendMessage(destination, id, 'listen');
82 },
83
84 // remove Listener from channel or topic.
85 removeListener : function(id, destination) {
86 this._handlers[id] = null;
87 this._sendMessage(destination, id, 'unlisten');
88 },
89
90 _sendMessage : function(destination, message, type) {
91 var req = new qx.io.remote.Request(this.uri, "POST", "text/plain");
92 req.setParameter("destination", destination);
93 req.setParameter("message", message);
94 req.setParameter("type", type);
95 //req.addListener("completed", this.endBatch, this);
96 req.send();
97 },
98
99 startPolling : function() {
100 if (this.poll){
101 this.interrupt = false;
102 var req = new qx.io.remote.Request(this.uri, "GET", "application/xml");
103 req.setParameter("timeout", "10");
104 req.addListener("completed", this._pollHandler, this);
105 req.send();
106 }
107 },
108
109 stopPolling : function(){
110 this.interrupt = true;
111 }
112 }
113 });