001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.cluster;
016    
017    import com.liferay.portal.kernel.cluster.ClusterEvent;
018    import com.liferay.portal.kernel.cluster.ClusterEventListener;
019    import com.liferay.portal.kernel.cluster.ClusterEventType;
020    import com.liferay.portal.kernel.cluster.ClusterLink;
021    import com.liferay.portal.kernel.cluster.ClusterNode;
022    import com.liferay.portal.kernel.json.JSONFactoryUtil;
023    import com.liferay.portal.kernel.json.JSONObject;
024    import com.liferay.portal.kernel.messaging.DestinationNames;
025    import com.liferay.portal.kernel.messaging.Message;
026    import com.liferay.portal.kernel.messaging.MessageBusUtil;
027    
028    import java.util.List;
029    
030    /**
031     * @author Amos Fong
032     */
033    public class LiveUsersClusterEventListenerImpl implements ClusterEventListener {
034    
035            @Override
036            public void processClusterEvent(ClusterEvent clusterEvent) {
037                    List<ClusterNode> clusterNodes = clusterEvent.getClusterNodes();
038    
039                    ClusterEventType clusterEventType = clusterEvent.getClusterEventType();
040    
041                    if (clusterEventType.equals(ClusterEventType.DEPART)) {
042                            for (ClusterNode clusterNode : clusterNodes) {
043                                    _processDepartEvent(clusterNode);
044                            }
045                    }
046                    else if (clusterEventType.equals(ClusterEventType.JOIN)) {
047                            for (ClusterNode clusterNode : clusterNodes) {
048                                    _processJoinEvent(clusterNode);
049                            }
050                    }
051            }
052    
053            private void _processDepartEvent(ClusterNode clusterNode) {
054                    Message message = new Message();
055    
056                    message.put(ClusterLink.CLUSTER_FORWARD_MESSAGE, true);
057    
058                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
059    
060                    jsonObject.put("clusterNodeId", clusterNode.getClusterNodeId());
061                    jsonObject.put("command", "removeClusterNode");
062    
063                    message.setPayload(jsonObject.toString());
064    
065                    MessageBusUtil.sendMessage(DestinationNames.LIVE_USERS, message);
066            }
067    
068            private void _processJoinEvent(ClusterNode clusterNode) {
069                    Message message = new Message();
070    
071                    message.put(ClusterLink.CLUSTER_FORWARD_MESSAGE, true);
072    
073                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
074    
075                    jsonObject.put("clusterNodeId", clusterNode.getClusterNodeId());
076                    jsonObject.put("command", "addClusterNode");
077    
078                    message.setPayload(jsonObject.toString());
079    
080                    MessageBusUtil.sendMessage(DestinationNames.LIVE_USERS, message);
081            }
082    
083    }