1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.ksb.messaging.web; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.sql.Timestamp; |
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Calendar; |
23 | |
import java.util.Collections; |
24 | |
import java.util.Comparator; |
25 | |
import java.util.Date; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
|
31 | |
import javax.servlet.ServletException; |
32 | |
import javax.servlet.http.HttpServletRequest; |
33 | |
import javax.servlet.http.HttpServletResponse; |
34 | |
|
35 | |
import org.apache.commons.collections.comparators.ComparableComparator; |
36 | |
import org.apache.commons.lang.StringUtils; |
37 | |
import org.apache.commons.lang.math.NumberUtils; |
38 | |
import org.apache.struts.action.ActionForm; |
39 | |
import org.apache.struts.action.ActionForward; |
40 | |
import org.apache.struts.action.ActionMapping; |
41 | |
import org.apache.struts.action.ActionMessage; |
42 | |
import org.apache.struts.action.ActionMessages; |
43 | |
import org.kuali.rice.core.api.config.CoreConfigHelper; |
44 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
45 | |
import org.kuali.rice.core.util.ConcreteKeyValue; |
46 | |
import org.kuali.rice.core.util.RiceConstants; |
47 | |
import org.kuali.rice.core.util.RiceUtilities; |
48 | |
import org.kuali.rice.ksb.api.KsbApiServiceLocator; |
49 | |
import org.kuali.rice.ksb.api.messaging.AsynchronousCall; |
50 | |
import org.kuali.rice.ksb.api.registry.ServiceInfo; |
51 | |
import org.kuali.rice.ksb.messaging.MessageFetcher; |
52 | |
import org.kuali.rice.ksb.messaging.MessageServiceInvoker; |
53 | |
import org.kuali.rice.ksb.messaging.PersistedMessageBO; |
54 | |
import org.kuali.rice.ksb.messaging.service.MessageQueueService; |
55 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
56 | |
import org.kuali.rice.ksb.util.KSBConstants; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | public class MessageQueueAction extends KSBAction { |
65 | |
|
66 | |
@Override |
67 | |
public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
68 | |
HttpServletResponse response) throws IOException, ServletException { |
69 | 0 | return mapping.findForward("report"); |
70 | |
} |
71 | |
|
72 | |
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
73 | |
HttpServletResponse response) throws Exception { |
74 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
75 | 0 | save(routeQueueForm); |
76 | |
|
77 | 0 | routeQueueForm.getMessageQueueFromForm().getRouteQueueId(); |
78 | 0 | ActionMessages messages = new ActionMessages(); |
79 | 0 | messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("routequeue.RouteQueueService.saved")); |
80 | 0 | saveMessages(request, messages); |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | 0 | return mapping.findForward("report"); |
93 | |
} |
94 | |
|
95 | |
public ActionForward saveAndResubmit(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
96 | |
HttpServletResponse response) throws Exception { |
97 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
98 | 0 | PersistedMessageBO message = save(routeQueueForm); |
99 | 0 | KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message)); |
100 | |
|
101 | 0 | ActionMessages messages = new ActionMessages(); |
102 | 0 | messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("routequeue.RouteQueueService.queued")); |
103 | 0 | saveMessages(request, messages); |
104 | |
|
105 | 0 | routeQueueForm.setMessageId(null); |
106 | 0 | routeQueueForm.setMessageQueueFromDatabase(null); |
107 | 0 | routeQueueForm.setMessageQueueFromForm(null); |
108 | 0 | routeQueueForm.setShowEdit("yes"); |
109 | 0 | routeQueueForm.setMethodToCall(""); |
110 | 0 | establishRequiredState(request, form); |
111 | 0 | routeQueueForm.setMessageId(message.getRouteQueueId()); |
112 | 0 | routeQueueForm.setMessageQueueFromForm(message); |
113 | 0 | routeQueueForm.setNewQueueDate(routeQueueForm.getExistingQueueDate()); |
114 | 0 | routeQueueForm.getMessageQueueFromForm().setMethodCall(unwrapPayload(message)); |
115 | 0 | return mapping.findForward("report"); |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
private PersistedMessageBO save(MessageQueueForm routeQueueForm) { |
146 | 0 | Long routeQueueId = routeQueueForm.getMessageQueueFromForm().getRouteQueueId(); |
147 | 0 | if ((routeQueueId == null) || (routeQueueId.longValue() <= 0)) { |
148 | 0 | throw new IllegalArgumentException("Invalid routeQueueId passed in. Cannot save"); |
149 | |
} |
150 | |
|
151 | 0 | PersistedMessageBO existingMessage = KSBServiceLocator.getMessageQueueService().findByRouteQueueId(routeQueueId); |
152 | 0 | PersistedMessageBO message = routeQueueForm.getMessageQueueFromForm(); |
153 | |
|
154 | 0 | if (existingMessage == null) { |
155 | 0 | throw new RuntimeException("Could locate the existing message, it may have already been processed."); |
156 | |
} |
157 | |
|
158 | 0 | existingMessage.setQueuePriority(message.getQueuePriority()); |
159 | 0 | existingMessage.setIpNumber(message.getIpNumber()); |
160 | 0 | existingMessage.setLockVerNbr(message.getLockVerNbr()); |
161 | 0 | existingMessage.setApplicationId(message.getApplicationId()); |
162 | 0 | existingMessage.setMethodName(message.getMethodName()); |
163 | 0 | existingMessage.setQueueStatus(message.getQueueStatus()); |
164 | 0 | existingMessage.setRetryCount(message.getRetryCount()); |
165 | 0 | existingMessage.setServiceName(message.getServiceName()); |
166 | 0 | existingMessage.setValue1(message.getValue1()); |
167 | 0 | existingMessage.setValue2(message.getValue2()); |
168 | 0 | KSBServiceLocator.getMessageQueueService().save(existingMessage); |
169 | 0 | return existingMessage; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
protected void quickRequeueMessage(PersistedMessageBO message) { |
203 | 0 | message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING); |
204 | 0 | message.setQueueDate(new Timestamp(Calendar.getInstance().getTimeInMillis())); |
205 | 0 | message.setRetryCount(new Integer(0)); |
206 | 0 | getRouteQueueService().save(message); |
207 | 0 | } |
208 | |
|
209 | |
public ActionForward quickRequeueMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
210 | |
HttpServletResponse response) throws Exception { |
211 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
212 | 0 | if (routeQueueForm.getMessageQueueFromDatabase() == null) { |
213 | 0 | throw new IllegalArgumentException("No messageId passed in with the Request."); |
214 | |
} |
215 | |
|
216 | 0 | PersistedMessageBO message = routeQueueForm.getMessageQueueFromDatabase(); |
217 | 0 | quickRequeueMessage(message); |
218 | 0 | KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message)); |
219 | |
|
220 | 0 | ActionMessages messages = new ActionMessages(); |
221 | 0 | messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("routequeue.RouteQueueService.requeued")); |
222 | 0 | saveMessages(request, messages); |
223 | |
|
224 | 0 | routeQueueForm.setMessageQueueFromDatabase(null); |
225 | 0 | routeQueueForm.setMessageQueueFromForm(null); |
226 | 0 | routeQueueForm.setMessageId(null); |
227 | 0 | routeQueueForm.setMethodToCall(""); |
228 | |
|
229 | |
|
230 | 0 | establishRequiredState(request, form); |
231 | 0 | return mapping.findForward("report"); |
232 | |
} |
233 | |
|
234 | |
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
235 | |
HttpServletResponse response) throws IOException, ServletException { |
236 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
237 | 0 | routeQueueForm.setShowEdit("yes"); |
238 | 0 | routeQueueForm.setMessageQueueFromForm(routeQueueForm.getMessageQueueFromDatabase()); |
239 | 0 | routeQueueForm.setNewQueueDate(routeQueueForm.getExistingQueueDate()); |
240 | 0 | routeQueueForm.getMessageQueueFromForm().setMethodCall(unwrapPayload(routeQueueForm.getMessageQueueFromForm())); |
241 | 0 | return mapping.findForward("basic"); |
242 | |
} |
243 | |
|
244 | |
public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
245 | |
HttpServletResponse response) throws Exception { |
246 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
247 | 0 | routeQueueForm.setShowEdit("no"); |
248 | 0 | routeQueueForm.setMessageQueueFromForm(routeQueueForm.getMessageQueueFromDatabase()); |
249 | 0 | routeQueueForm.setNewQueueDate(routeQueueForm.getExistingQueueDate()); |
250 | 0 | AsynchronousCall messagePayload = unwrapPayload(routeQueueForm.getMessageQueueFromDatabase()); |
251 | 0 | routeQueueForm.getMessageQueueFromForm().setMethodCall(messagePayload); |
252 | 0 | return mapping.findForward("payload"); |
253 | |
} |
254 | |
|
255 | |
public ActionForward reset(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
256 | |
HttpServletResponse response) throws Exception { |
257 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
258 | 0 | if (routeQueueForm.getShowEdit().equals("yes")) { |
259 | 0 | routeQueueForm.setMessageQueueFromForm(routeQueueForm.getMessageQueueFromDatabase()); |
260 | |
} |
261 | 0 | return mapping.findForward("basic"); |
262 | |
} |
263 | |
|
264 | |
public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
265 | |
HttpServletResponse response) throws Exception { |
266 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
267 | 0 | routeQueueForm.getMessageQueueFromForm().setQueuePriority(null); |
268 | 0 | routeQueueForm.getMessageQueueFromForm().setQueueStatus(null); |
269 | 0 | routeQueueForm.getMessageQueueFromForm().setQueueDate(null); |
270 | 0 | routeQueueForm.getMessageQueueFromForm().setExpirationDate(null); |
271 | 0 | routeQueueForm.getMessageQueueFromForm().setRetryCount(null); |
272 | 0 | routeQueueForm.getMessageQueueFromForm().setIpNumber(null); |
273 | 0 | routeQueueForm.getMessageQueueFromForm().setServiceName(null); |
274 | 0 | routeQueueForm.getMessageQueueFromForm().setApplicationId(null); |
275 | 0 | routeQueueForm.getMessageQueueFromForm().setMethodName(null); |
276 | 0 | routeQueueForm.getMessageQueueFromForm().setPayload(null); |
277 | 0 | routeQueueForm.getMessageQueueFromForm().setMethodCall(null); |
278 | 0 | routeQueueForm.setExistingQueueDate(null); |
279 | 0 | routeQueueForm.setNewQueueDate(null); |
280 | 0 | return mapping.findForward("basic"); |
281 | |
} |
282 | |
|
283 | |
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
284 | |
HttpServletResponse response) throws Exception { |
285 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
286 | 0 | routeQueueForm.setMessageQueueFromForm(routeQueueForm.getMessageQueueFromDatabase()); |
287 | 0 | routeQueueForm.setMessageQueueFromDatabase(null); |
288 | 0 | getRouteQueueService().delete(routeQueueForm.getMessageQueueFromForm()); |
289 | 0 | ActionMessages messages = new ActionMessages(); |
290 | 0 | messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("routequeue.RouteQueueService.deleted", routeQueueForm |
291 | |
.getMessageQueueFromForm().getRouteQueueId().toString())); |
292 | 0 | saveMessages(request, messages); |
293 | 0 | routeQueueForm.setMessageId(null); |
294 | 0 | establishRequiredState(request, form); |
295 | 0 | return mapping.findForward("report"); |
296 | |
} |
297 | |
|
298 | |
public ActionForward executeMessageFetcher(ActionMapping mapping, ActionForm form, HttpServletRequest request, |
299 | |
HttpServletResponse response) throws Exception { |
300 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
301 | 0 | ActionMessages messages = new ActionMessages(); |
302 | 0 | if (routeQueueForm.getMaxMessageFetcherMessages() == null || routeQueueForm.getMaxMessageFetcherMessages() <= 0) { |
303 | 0 | messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("routequeue.RouteQueueService.invalidMessages", routeQueueForm.getMaxMessageFetcherMessages())); |
304 | |
} |
305 | 0 | if (!messages.isEmpty()) { |
306 | 0 | saveMessages(request, messages); |
307 | 0 | return mapping.findForward("report"); |
308 | |
} |
309 | 0 | new MessageFetcher(routeQueueForm.getMaxMessageFetcherMessages()).run(); |
310 | 0 | return mapping.findForward("report"); |
311 | |
} |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
@Override |
320 | |
public ActionMessages establishRequiredState(HttpServletRequest request, ActionForm form) throws Exception { |
321 | 0 | request.setAttribute("rice_constant", getServlet().getServletContext().getAttribute("RiceConstants")); |
322 | 0 | request.setAttribute("ksb_constant", getServlet().getServletContext().getAttribute("KSBConstants")); |
323 | 0 | MessageQueueForm routeQueueForm = (MessageQueueForm) form; |
324 | 0 | routeQueueForm.setMyIpAddress(RiceUtilities.getIpNumber()); |
325 | 0 | routeQueueForm.setMyApplicationId(CoreConfigHelper.getApplicationId()); |
326 | 0 | routeQueueForm.setMessagePersistence(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.MESSAGE_PERSISTENCE)); |
327 | 0 | routeQueueForm.setMessageDelivery(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.MESSAGE_DELIVERY)); |
328 | 0 | routeQueueForm.setMessageOff(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.MESSAGING_OFF)); |
329 | 0 | List<ServiceInfo> services = KsbApiServiceLocator.getServiceRegistry().getAllOnlineServices(); |
330 | 0 | if (routeQueueForm.getMessageId() != null) { |
331 | 0 | PersistedMessageBO rq = getRouteQueueService().findByRouteQueueId(routeQueueForm.getMessageId()); |
332 | 0 | if (rq != null) { |
333 | 0 | routeQueueForm.setExistingQueueDate(RiceConstants.getDefaultDateFormat().format(new Date())); |
334 | 0 | routeQueueForm.setMessageQueueFromDatabase(rq); |
335 | |
|
336 | 0 | String serviceName = rq.getServiceName(); |
337 | 0 | for (ServiceInfo serviceInfo : services) { |
338 | 0 | if (serviceInfo.getServiceName().equals(serviceName)) { |
339 | 0 | routeQueueForm.getIpAddresses().add( |
340 | |
new ConcreteKeyValue(serviceInfo.getServerIpAddress(), serviceInfo.getServerIpAddress())); |
341 | |
} |
342 | |
} |
343 | 0 | } else { |
344 | 0 | ActionMessages messages = new ActionMessages(); |
345 | 0 | messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( |
346 | |
"messagequeue.RouteQueueService.queuedDocumentNotFound", routeQueueForm.getMessageId().toString())); |
347 | 0 | return messages; |
348 | |
} |
349 | 0 | routeQueueForm.setMessageId(null); |
350 | 0 | } else if (!"clear".equalsIgnoreCase(request.getParameter("methodToCall"))) { |
351 | 0 | List<PersistedMessageBO> queueEntries = findRouteQueues(request, routeQueueForm, routeQueueForm.getMaxRows() + 1); |
352 | 0 | if (queueEntries.size() > 0) { |
353 | 0 | Collections.sort(queueEntries, new Comparator() { |
354 | 0 | private Comparator comp = new ComparableComparator(); |
355 | |
|
356 | |
@Override |
357 | |
public int compare(Object object1, Object object2) { |
358 | 0 | if (object1 == null && object2 == null) { |
359 | 0 | return 0; |
360 | 0 | } else if (object1 == null) { |
361 | 0 | return 1; |
362 | 0 | } else if (object2 == null) { |
363 | 0 | return -1; |
364 | |
} |
365 | 0 | Long id1 = ((PersistedMessageBO) object1).getRouteQueueId(); |
366 | 0 | Long id2 = ((PersistedMessageBO) object2).getRouteQueueId(); |
367 | |
|
368 | |
try { |
369 | 0 | return this.comp.compare(id1, id2); |
370 | 0 | } catch (Exception e) { |
371 | 0 | return 0; |
372 | |
} |
373 | |
} |
374 | |
}); |
375 | |
} |
376 | 0 | routeQueueForm.setMessageQueueRows(queueEntries); |
377 | |
} |
378 | 0 | return null; |
379 | |
} |
380 | |
|
381 | |
protected List<PersistedMessageBO> findRouteQueues(HttpServletRequest request, MessageQueueForm routeQueueForm, int maxRows) { |
382 | 0 | List<PersistedMessageBO> routeQueues = new ArrayList<PersistedMessageBO>(); |
383 | |
|
384 | |
|
385 | 0 | if (StringUtils.isBlank(routeQueueForm.getFilterApplied())) { |
386 | 0 | routeQueues.addAll(getRouteQueueService().findAll(maxRows)); |
387 | |
} |
388 | |
|
389 | |
|
390 | |
else { |
391 | 0 | if (!StringUtils.isBlank(routeQueueForm.getRouteQueueIdFilter())) { |
392 | 0 | if (!NumberUtils.isNumber(routeQueueForm.getRouteQueueIdFilter())) { |
393 | |
|
394 | 0 | throw new RuntimeException("Message Id must be a number."); |
395 | |
} |
396 | |
} |
397 | |
|
398 | 0 | Map<String, String> criteriaValues = new HashMap<String, String>(); |
399 | 0 | String key = null; |
400 | 0 | String value = null; |
401 | 0 | String trimmedKey = null; |
402 | 0 | for (Iterator iter = request.getParameterMap().keySet().iterator(); iter.hasNext();) { |
403 | 0 | key = (String) iter.next(); |
404 | 0 | if (key.endsWith(KSBConstants.ROUTE_QUEUE_FILTER_SUFFIX)) { |
405 | 0 | value = request.getParameter(key); |
406 | 0 | if (StringUtils.isNotBlank(value)) { |
407 | 0 | trimmedKey = key.substring(0, key.indexOf(KSBConstants.ROUTE_QUEUE_FILTER_SUFFIX)); |
408 | 0 | criteriaValues.put(trimmedKey, value); |
409 | |
} |
410 | |
} |
411 | |
} |
412 | 0 | routeQueues.addAll(getRouteQueueService().findByValues(criteriaValues, maxRows)); |
413 | |
} |
414 | 0 | return routeQueues; |
415 | |
} |
416 | |
|
417 | |
private MessageQueueService getRouteQueueService() { |
418 | 0 | return KSBServiceLocator.getMessageQueueService(); |
419 | |
} |
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
protected AsynchronousCall unwrapPayload(PersistedMessageBO message) { |
432 | 0 | if (message == null || message.getPayload() == null) { |
433 | 0 | return null; |
434 | |
} |
435 | 0 | String encodedPayload = message.getPayload().getPayload(); |
436 | 0 | if (StringUtils.isBlank(encodedPayload)) { |
437 | 0 | return null; |
438 | |
} |
439 | 0 | Object decodedPayload = null; |
440 | 0 | if (encodedPayload != null) { |
441 | 0 | decodedPayload = KsbApiServiceLocator.getMessageHelper().deserializeObject(encodedPayload); |
442 | |
} |
443 | |
|
444 | 0 | if ((decodedPayload != null) && !(decodedPayload instanceof AsynchronousCall)) { |
445 | 0 | throw new IllegalArgumentException("PersistedMessageBO payload was not of the expected class. " + "Expected was [" |
446 | |
+ AsynchronousCall.class.getName() + "], actual was: [" + decodedPayload.getClass().getName() + "]"); |
447 | |
} |
448 | 0 | return (AsynchronousCall) decodedPayload; |
449 | |
} |
450 | |
|
451 | |
} |