1
22
23 package com.liferay.portal.poller;
24
25 import com.liferay.portal.kernel.json.JSONArray;
26 import com.liferay.portal.kernel.messaging.Message;
27 import com.liferay.portal.kernel.messaging.MessageBusUtil;
28 import com.liferay.portal.kernel.messaging.MessageListener;
29 import com.liferay.portal.kernel.poller.PollerRequest;
30 import com.liferay.portal.kernel.poller.PollerResponse;
31 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
32
33 import java.util.HashMap;
34 import java.util.Map;
35
36
41 public class PollerRequestManager implements MessageListener {
42
43 public PollerRequestManager(
44 JSONArray pollerResponseChunksJSON, String destinationName,
45 String responseDestinationName, long timeout) {
46
47 _pollerResponseChunksJSON = pollerResponseChunksJSON;
48 _destinationName = destinationName;
49 _responseDestinationName = responseDestinationName;
50 _timeout = timeout;
51 }
52
53 public void addPollerRequest(PollerRequest pollerRequest) {
54 if (pollerRequest == null) {
55 return;
56 }
57
58 _pollerRequests.put(pollerRequest.getPortletId(), pollerRequest);
59 }
60
61 public void clearRequests() {
62 _pollerRequests.clear();
63 _responseIds.clear();
64 _responseCount = 0;
65 }
66
67 public JSONArray getPollerResponse() {
68 return _pollerResponseChunksJSON;
69 }
70
71 public void processRequests() {
72 MessageBusUtil.registerMessageListener(_responseDestinationName, this);
73
74 try {
75 for (PollerRequest pollerRequest : _pollerRequests.values()) {
76 Message message = new Message();
77
78 message.setPayload(pollerRequest);
79 message.setResponseDestinationName(_responseDestinationName);
80
81 String responseId = PortalUUIDUtil.generate();
82
83 message.setResponseId(responseId);
84
85 _responseIds.put(responseId, responseId);
86
87 MessageBusUtil.sendMessage(_destinationName, message);
88 }
89
90 synchronized (this) {
91 if (_responseCount != _pollerRequests.size()) {
92 try {
93 this.wait(_timeout);
94 }
95 catch (InterruptedException ie) {
96 }
97 }
98 }
99 }
100 finally {
101 MessageBusUtil.unregisterMessageListener(
102 _responseDestinationName, this);
103 }
104 }
105
106 public void receive(Message message) {
107 if (!_responseIds.containsKey(message.getResponseId())) {
108 return;
109 }
110
111 if (_pollerResponseChunksJSON != null) {
112 PollerResponse pollerResponse =
113 (PollerResponse)message.getPayload();
114
115 if (pollerResponse != null) {
116 _pollerResponseChunksJSON.put(pollerResponse.toJSONObject());
117 }
118 }
119
120 synchronized (this) {
121 _responseCount++;
122
123 if (_responseCount == _pollerRequests.size()) {
124 notify();
125 }
126 }
127 }
128
129 private String _destinationName;
130 private Map<String, PollerRequest> _pollerRequests =
131 new HashMap<String, PollerRequest>();
132 private JSONArray _pollerResponseChunksJSON;
133 private int _responseCount;
134 private String _responseDestinationName;
135 private Map<String, String> _responseIds = new HashMap<String, String>();
136 private long _timeout;
137
138 }