1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.messaging; |
17 | |
|
18 | |
import org.apache.log4j.Logger; |
19 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
20 | |
import org.kuali.rice.ksb.messaging.service.MessageQueueService; |
21 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
22 | |
import org.kuali.rice.ksb.util.KSBConstants; |
23 | |
import org.kuali.rice.ksb.util.KSBConstants.Config; |
24 | |
import org.springframework.transaction.TransactionStatus; |
25 | |
import org.springframework.transaction.support.TransactionCallback; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | public class MessageFetcher implements Runnable { |
34 | |
|
35 | 0 | private static final Logger LOG = Logger.getLogger(MessageFetcher.class); |
36 | |
|
37 | |
private Integer maxMessages; |
38 | |
private Long routeQueueId; |
39 | |
|
40 | 0 | public MessageFetcher(Integer maxMessages) { |
41 | 0 | this.maxMessages = maxMessages; |
42 | 0 | } |
43 | |
|
44 | 0 | public MessageFetcher(Long routeQueueId) { |
45 | 0 | this.routeQueueId = routeQueueId; |
46 | 0 | } |
47 | |
|
48 | |
public void run() { |
49 | 0 | if (ConfigContext.getCurrentContextConfig().getBooleanProperty(KSBConstants.Config.MESSAGE_PERSISTENCE, false)) { |
50 | |
try { |
51 | 0 | requeueDocument(); |
52 | 0 | requeueMessages(); |
53 | 0 | } catch (Throwable t) { |
54 | 0 | LOG.error("Failed to fetch messages.", t); |
55 | 0 | } |
56 | |
} |
57 | 0 | } |
58 | |
|
59 | |
private void requeueMessages() { |
60 | 0 | if (this.routeQueueId == null) { |
61 | |
try { |
62 | 0 | for (final PersistedMessageBO message : getRouteQueueService().getNextDocuments(maxMessages)) { |
63 | 0 | markEnrouteAndSaveMessage(message); |
64 | 0 | executeMessage(message); |
65 | |
} |
66 | 0 | } catch (Throwable t) { |
67 | 0 | LOG.error("Failed to fetch or process some messages during requeueMessages", t); |
68 | 0 | } |
69 | |
} |
70 | 0 | } |
71 | |
|
72 | |
private void requeueDocument() { |
73 | |
try { |
74 | 0 | if (this.routeQueueId != null) { |
75 | 0 | PersistedMessageBO message = getRouteQueueService().findByRouteQueueId(this.routeQueueId); |
76 | 0 | message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING); |
77 | 0 | getRouteQueueService().save(message); |
78 | 0 | executeMessage(message); |
79 | |
} |
80 | 0 | } catch (Throwable t) { |
81 | 0 | LOG.error("Failed to fetch or process some messages during requeueDocument", t); |
82 | 0 | } |
83 | 0 | } |
84 | |
|
85 | |
private void executeMessage(PersistedMessageBO message) { |
86 | |
try { |
87 | 0 | KSBServiceLocator.getThreadPool().execute(new MessageServiceInvoker(message)); |
88 | 0 | } catch (Throwable t) { |
89 | 0 | LOG.error("Failed to place message " + message + " in thread pool for execution", t); |
90 | 0 | } |
91 | 0 | } |
92 | |
|
93 | |
private void markEnrouteAndSaveMessage(final PersistedMessageBO message) { |
94 | |
try { |
95 | 0 | KSBServiceLocator.getTransactionTemplate().execute(new TransactionCallback() { |
96 | |
public Object doInTransaction(TransactionStatus status) { |
97 | 0 | message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING); |
98 | 0 | getRouteQueueService().save(message); |
99 | 0 | return null; |
100 | |
} |
101 | |
}); |
102 | 0 | } catch (Throwable t) { |
103 | 0 | LOG.error("Caught error attempting to mark message " + message + " as R", t); |
104 | 0 | } |
105 | 0 | } |
106 | |
|
107 | |
private MessageQueueService getRouteQueueService() { |
108 | 0 | return KSBServiceLocator.getMessageQueueService(); |
109 | |
} |
110 | |
|
111 | |
} |