001
014
015 package com.liferay.portal.poller;
016
017 import com.liferay.portal.kernel.json.JSONArray;
018 import com.liferay.portal.kernel.json.JSONFactoryUtil;
019 import com.liferay.portal.kernel.json.JSONObject;
020 import com.liferay.portal.kernel.notifications.ChannelException;
021 import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
022 import com.liferay.portal.kernel.notifications.ChannelListener;
023 import com.liferay.portal.kernel.notifications.NotificationEvent;
024 import com.liferay.portal.util.PropsValues;
025
026 import java.util.List;
027
028
031 public class SynchronousPollerChannelListener implements ChannelListener {
032
033 public SynchronousPollerChannelListener(
034 long companyId, long userId,
035 JSONObject pollerResponseHeaderJSONObject) {
036
037 _companyId = companyId;
038 _userId = userId;
039 _pollerResponseHeaderJSONObject = pollerResponseHeaderJSONObject;
040 }
041
042 @Override
043 public synchronized void channelListenerRemoved(long channelId) {
044 _complete = true;
045
046 this.notify();
047 }
048
049 public synchronized String getNotificationEvents(long timeout)
050 throws ChannelException {
051
052 try {
053 if (!_complete) {
054 this.wait(timeout);
055 }
056 }
057 catch (InterruptedException ie) {
058 }
059
060 try {
061 Thread.sleep(PropsValues.POLLER_NOTIFICATIONS_TIMEOUT);
062 }
063 catch (InterruptedException ie) {
064 }
065
066 List<NotificationEvent> notificationEvents =
067 ChannelHubManagerUtil.fetchNotificationEvents(
068 _companyId, _userId, true);
069
070 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
071
072 jsonArray.put(_pollerResponseHeaderJSONObject);
073
074 for (NotificationEvent notificationEvent : notificationEvents) {
075 jsonArray.put(notificationEvent.toJSONObject());
076 }
077
078 return jsonArray.toString();
079 }
080
081 @Override
082 public synchronized void notificationEventsAvailable(long channelId) {
083 _complete = true;
084
085 this.notify();
086 }
087
088 private long _companyId;
089 private boolean _complete;
090 private JSONObject _pollerResponseHeaderJSONObject;
091 private long _userId;
092
093 }