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
025 import java.util.List;
026
027
030 public class SynchronousPollerChannelListener implements ChannelListener {
031
032 @Override
033 public synchronized void channelListenerRemoved(long channelId) {
034 _complete = true;
035
036 this.notify();
037 }
038
039 public synchronized String getNotificationEvents(
040 long companyId, long userId,
041 JSONObject pollerResponseHeaderJSONObject, long timeout)
042 throws ChannelException {
043
044 try {
045 if (!_complete) {
046 this.wait(timeout);
047 }
048 }
049 catch (InterruptedException ie) {
050 }
051
052 List<NotificationEvent> notificationEvents =
053 ChannelHubManagerUtil.fetchNotificationEvents(
054 companyId, userId, true);
055
056 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
057
058 jsonArray.put(pollerResponseHeaderJSONObject);
059
060 for (NotificationEvent notificationEvent : notificationEvents) {
061 jsonArray.put(notificationEvent.toJSONObject());
062 }
063
064 return jsonArray.toString();
065 }
066
067 @Override
068 public synchronized void notificationEventsAvailable(long channelId) {
069 _complete = true;
070
071 this.notify();
072 }
073
074 private boolean _complete;
075
076 }