001
014
015 package com.liferay.portal.liveusers.messaging;
016
017 import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
018 import com.liferay.portal.kernel.cluster.ClusterNodeResponse;
019 import com.liferay.portal.kernel.cluster.ClusterNodeResponses;
020 import com.liferay.portal.kernel.cluster.ClusterRequest;
021 import com.liferay.portal.kernel.cluster.FutureClusterResponses;
022 import com.liferay.portal.kernel.json.JSONFactoryUtil;
023 import com.liferay.portal.kernel.json.JSONObject;
024 import com.liferay.portal.kernel.messaging.BaseMessageListener;
025 import com.liferay.portal.kernel.messaging.Message;
026 import com.liferay.portal.kernel.util.MethodHandler;
027 import com.liferay.portal.kernel.util.MethodKey;
028 import com.liferay.portal.liveusers.LiveUsers;
029
030 import java.util.Map;
031 import java.util.Set;
032 import java.util.concurrent.TimeUnit;
033
034
038 public class LiveUsersMessageListener extends BaseMessageListener {
039
040 protected void doCommandAddClusterNode(JSONObject jsonObject)
041 throws Exception {
042
043 String clusterNodeId = jsonObject.getString("clusterNodeId");
044
045 ClusterRequest clusterRequest = ClusterRequest.createUnicastRequest(
046 _getLocalClusterUsersMethodHandler, clusterNodeId);
047
048 FutureClusterResponses futureClusterResponses =
049 ClusterExecutorUtil.execute(clusterRequest);
050
051 ClusterNodeResponses clusterNodeResponses = futureClusterResponses.get(
052 20000, TimeUnit.MILLISECONDS);
053
054 ClusterNodeResponse clusterNodeResponse =
055 clusterNodeResponses.getClusterResponse(clusterNodeId);
056
057 Object result = clusterNodeResponse.getResult();
058
059 if (result == null) {
060 return;
061 }
062
063 Map<Long, Map<Long, Set<String>>> clusterUsers =
064 (Map<Long, Map<Long, Set<String>>>)result;
065
066 LiveUsers.addClusterNode(clusterNodeId, clusterUsers);
067 }
068
069 protected void doCommandRemoveClusterNode(JSONObject jsonObject)
070 throws Exception {
071
072 String clusterNodeId = jsonObject.getString("clusterNodeId");
073
074 LiveUsers.removeClusterNode(clusterNodeId);
075 }
076
077 protected void doCommandSignIn(JSONObject jsonObject) throws Exception {
078 String clusterNodeId = jsonObject.getString("clusterNodeId");
079 long companyId = jsonObject.getLong("companyId");
080 long userId = jsonObject.getLong("userId");
081 String sessionId = jsonObject.getString("sessionId");
082 String remoteAddr = jsonObject.getString("remoteAddr");
083 String remoteHost = jsonObject.getString("remoteHost");
084 String userAgent = jsonObject.getString("userAgent");
085
086 LiveUsers.signIn(
087 clusterNodeId, companyId, userId, sessionId, remoteAddr, remoteHost,
088 userAgent);
089 }
090
091 protected void doCommandSignOut(JSONObject jsonObject) throws Exception {
092 String clusterNodeId = jsonObject.getString("clusterNodeId");
093 long companyId = jsonObject.getLong("companyId");
094 long userId = jsonObject.getLong("userId");
095 String sessionId = jsonObject.getString("sessionId");
096
097 LiveUsers.signOut(clusterNodeId, companyId, userId, sessionId);
098 }
099
100 @Override
101 protected void doReceive(Message message) throws Exception {
102 String payload = (String)message.getPayload();
103
104 JSONObject jsonObject = JSONFactoryUtil.createJSONObject(payload);
105
106 String command = jsonObject.getString("command");
107
108 if (command.equals("addClusterNode")) {
109 doCommandAddClusterNode(jsonObject);
110 }
111 else if (command.equals("removeClusterNode")) {
112 doCommandRemoveClusterNode(jsonObject);
113 }
114 else if (command.equals("signIn")) {
115 doCommandSignIn(jsonObject);
116 }
117 else if (command.equals("signOut")) {
118 doCommandSignOut(jsonObject);
119 }
120 }
121
122 private static MethodHandler _getLocalClusterUsersMethodHandler =
123 new MethodHandler(
124 new MethodKey(LiveUsers.class.getName(), "getLocalClusterUsers"));
125
126 }