001    /**
002     * Copyright (c) 2000-present 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.ClusterInvokeThreadLocal;
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                    if (clusterNodes.isEmpty()) {
040                            return;
041                    }
042    
043                    ClusterEventType clusterEventType = clusterEvent.getClusterEventType();
044    
045                    String command = null;
046    
047                    if (clusterEventType == ClusterEventType.DEPART) {
048                            command = "removeClusterNode";
049                    }
050                    else if (clusterEventType == ClusterEventType.JOIN) {
051                            command = "addClusterNode";
052                    }
053                    else {
054                            throw new IllegalArgumentException(
055                                    "Unknown cluster event type " + clusterEventType);
056                    }
057    
058                    for (ClusterNode clusterNode : clusterNodes) {
059                            Message message = new Message();
060    
061                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
062    
063                            jsonObject.put("clusterNodeId", clusterNode.getClusterNodeId());
064                            jsonObject.put("command", command);
065    
066                            message.setPayload(jsonObject.toString());
067    
068                            ClusterInvokeThreadLocal.setEnabled(false);
069    
070                            try {
071                                    MessageBusUtil.sendMessage(
072                                            DestinationNames.LIVE_USERS, message);
073                            }
074                            finally {
075                                    ClusterInvokeThreadLocal.setEnabled(true);
076                            }
077                    }
078            }
079    
080    }