001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }