1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with |
---|
4 | * this work for additional information regarding copyright ownership. |
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | * (the "License"); you may not use this file except in compliance with |
---|
7 | * the License. You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | // AMQ Ajax handler |
---|
19 | // This class provides the main API for using the Ajax features of AMQ. It |
---|
20 | // allows JMS messages to be sent and received from javascript when used |
---|
21 | // with the org.apache.activemq.web.MessageListenerServlet |
---|
22 | // |
---|
23 | var amq = |
---|
24 | { |
---|
25 | // The URI of the MessageListenerServlet |
---|
26 | uri: '/amq', |
---|
27 | |
---|
28 | // Polling. Set to true (default) if waiting poll for messages is needed |
---|
29 | poll: true, |
---|
30 | |
---|
31 | // Poll delay. if set to positive integer, this is the time to wait in ms before |
---|
32 | // sending the next poll after the last completes. |
---|
33 | _pollDelay: 0, |
---|
34 | |
---|
35 | _first: true, |
---|
36 | _pollEvent: function(first) {}, |
---|
37 | _handlers: new Array(), |
---|
38 | |
---|
39 | _messages:0, |
---|
40 | _messageQueue: '', |
---|
41 | _queueMessages: 0, |
---|
42 | |
---|
43 | _messageHandler: function(request) |
---|
44 | { |
---|
45 | try |
---|
46 | { |
---|
47 | if (request.status == 200) |
---|
48 | { |
---|
49 | var response = request.responseXML.getElementsByTagName("ajax-response"); |
---|
50 | if (response != null && response.length == 1) |
---|
51 | { |
---|
52 | for ( var i = 0 ; i < response[0].childNodes.length ; i++ ) |
---|
53 | { |
---|
54 | var responseElement = response[0].childNodes[i]; |
---|
55 | |
---|
56 | // only process nodes of type element..... |
---|
57 | if ( responseElement.nodeType != 1 ) |
---|
58 | continue; |
---|
59 | |
---|
60 | var id = responseElement.getAttribute('id'); |
---|
61 | |
---|
62 | |
---|
63 | var handler = amq._handlers[id]; |
---|
64 | if (handler!=null) |
---|
65 | { |
---|
66 | for (var j = 0; j < responseElement.childNodes.length; j++) |
---|
67 | { |
---|
68 | handler(responseElement.childNodes[j]); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | catch(e) |
---|
76 | { |
---|
77 | alert(e); |
---|
78 | } |
---|
79 | }, |
---|
80 | |
---|
81 | startBatch: function() |
---|
82 | { |
---|
83 | amq._queueMessages++; |
---|
84 | }, |
---|
85 | |
---|
86 | endBatch: function() |
---|
87 | { |
---|
88 | amq._queueMessages--; |
---|
89 | if (amq._queueMessages==0 && amq._messages>0) |
---|
90 | { |
---|
91 | var body = amq._messageQueue; |
---|
92 | amq._messageQueue=''; |
---|
93 | amq._messages=0; |
---|
94 | amq._queueMessages++; |
---|
95 | new Ajax.Request(amq.uri, { method: 'post', postBody: body, onSuccess: amq.endBatch}); |
---|
96 | } |
---|
97 | }, |
---|
98 | |
---|
99 | _pollHandler: function(request) |
---|
100 | { |
---|
101 | amq.startBatch(); |
---|
102 | try |
---|
103 | { |
---|
104 | amq._messageHandler(request); |
---|
105 | amq._pollEvent(amq._first); |
---|
106 | amq._first=false; |
---|
107 | } |
---|
108 | catch(e) |
---|
109 | { |
---|
110 | alert(e); |
---|
111 | } |
---|
112 | amq.endBatch(); |
---|
113 | |
---|
114 | if (amq._pollDelay>0) |
---|
115 | setTimeout('amq._sendPoll()',amq._pollDelay); |
---|
116 | else |
---|
117 | amq._sendPoll(); |
---|
118 | }, |
---|
119 | |
---|
120 | _sendPoll: function(request) |
---|
121 | { |
---|
122 | new Ajax.Request(amq.uri, { method: 'get', onSuccess: amq._pollHandler }); |
---|
123 | }, |
---|
124 | |
---|
125 | // Add a function that gets called on every poll response, after all received |
---|
126 | // messages have been handled. The poll handler is past a boolean that indicates |
---|
127 | // if this is the first poll for the page. |
---|
128 | addPollHandler : function(func) |
---|
129 | { |
---|
130 | var old = amq._pollEvent; |
---|
131 | amq._pollEvent = function(first) |
---|
132 | { |
---|
133 | old(first); |
---|
134 | func(first); |
---|
135 | } |
---|
136 | }, |
---|
137 | |
---|
138 | // Send a JMS message to a destination (eg topic://MY.TOPIC). Message should be xml or encoded |
---|
139 | // xml content. |
---|
140 | sendMessage : function(destination,message) |
---|
141 | { |
---|
142 | amq._sendMessage(destination,message,'send'); |
---|
143 | }, |
---|
144 | |
---|
145 | // Listen on a channel or topic. handler must be a function taking a message arguement |
---|
146 | addListener : function(id,destination,handler) |
---|
147 | { |
---|
148 | amq._handlers[id]=handler; |
---|
149 | amq._sendMessage(destination,id,'listen'); |
---|
150 | }, |
---|
151 | |
---|
152 | // remove Listener from channel or topic. |
---|
153 | removeListener : function(id,destination) |
---|
154 | { |
---|
155 | amq._handlers[id]=null; |
---|
156 | amq._sendMessage(destination,id,'unlisten'); |
---|
157 | }, |
---|
158 | |
---|
159 | _sendMessage : function(destination,message,type) |
---|
160 | { |
---|
161 | if (amq._queueMessages>0) |
---|
162 | { |
---|
163 | if (amq._messages==0) |
---|
164 | { |
---|
165 | amq._messageQueue='destination='+destination+'&message='+message+'&type='+type; |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | amq._messageQueue+='&d'+amq._messages+'='+destination+'&m'+amq._messages+'='+message+'&t'+amq._messages+'='+type; |
---|
170 | } |
---|
171 | amq._messages++; |
---|
172 | } |
---|
173 | else |
---|
174 | { |
---|
175 | amq.startBatch(); |
---|
176 | new Ajax.Request(amq.uri, { method: 'post', postBody: 'destination='+destination+'&message='+message+'&type='+type, onSuccess: amq.endBatch}); |
---|
177 | } |
---|
178 | }, |
---|
179 | |
---|
180 | _startPolling : function() |
---|
181 | { |
---|
182 | if (amq.poll) |
---|
183 | new Ajax.Request(amq.uri, { method: 'get', parameters: 'timeout=0', onSuccess: amq._pollHandler }); |
---|
184 | } |
---|
185 | }; |
---|
186 | |
---|
187 | Behaviour.addLoadEvent(amq._startPolling); |
---|
188 | |
---|
189 | function getKeyCode(ev) |
---|
190 | { |
---|
191 | var keyc; |
---|
192 | if (window.event) |
---|
193 | keyc=window.event.keyCode; |
---|
194 | else |
---|
195 | keyc=ev.keyCode; |
---|
196 | return keyc; |
---|
197 | } |
---|