001    /**
002     * Copyright (c) 2000-2012 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.ClusterLinkUtil;
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            public void processClusterEvent(ClusterEvent clusterEvent) {
036                    List<ClusterNode> clusterNodes = clusterEvent.getClusterNodes();
037    
038                    ClusterEventType clusterEventType = clusterEvent.getClusterEventType();
039    
040                    if (clusterEventType.equals(ClusterEventType.DEPART)) {
041                            for (ClusterNode clusterNode : clusterNodes) {
042                                    _processDepartEvent(clusterNode);
043                            }
044                    }
045                    else if (clusterEventType.equals(ClusterEventType.JOIN)) {
046                            for (ClusterNode clusterNode : clusterNodes) {
047                                    _processJoinEvent(clusterNode);
048                            }
049                    }
050            }
051    
052            private void _processDepartEvent(ClusterNode clusterNode) {
053                    Message message = new Message();
054    
055                    message.put(ClusterLinkUtil.CLUSTER_FORWARD_MESSAGE, true);
056    
057                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
058    
059                    jsonObject.put("clusterNodeId", clusterNode.getClusterNodeId());
060                    jsonObject.put("command", "removeClusterNode");
061    
062                    message.setPayload(jsonObject.toString());
063    
064                    MessageBusUtil.sendMessage(DestinationNames.LIVE_USERS, message);
065            }
066    
067            private void _processJoinEvent(ClusterNode clusterNode) {
068                    Message message = new Message();
069    
070                    message.put(ClusterLinkUtil.CLUSTER_FORWARD_MESSAGE, true);
071    
072                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
073    
074                    jsonObject.put("clusterNodeId", clusterNode.getClusterNodeId());
075                    jsonObject.put("command", "addClusterNode");
076    
077                    message.setPayload(jsonObject.toString());
078    
079                    MessageBusUtil.sendMessage(DestinationNames.LIVE_USERS, message);
080            }
081    
082    }