View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans;
12  
13  import java.lang.management.ManagementFactory;
14  import java.util.Set;
15  
16  import javax.management.MBeanServer;
17  import javax.management.MalformedObjectNameException;
18  import javax.management.ObjectInstance;
19  import javax.management.ObjectName;
20  
21  import psiprobe.model.jmx.AsyncClusterSender;
22  import psiprobe.model.jmx.Cluster;
23  import psiprobe.model.jmx.ClusterSender;
24  import psiprobe.model.jmx.PooledClusterSender;
25  import psiprobe.model.jmx.SyncClusterSender;
26  import psiprobe.tools.JmxTools;
27  
28  /**
29   * The Class ClusterWrapperBean.
30   */
31  public class ClusterWrapperBean {
32  
33    /**
34     * Gets the cluster.
35     *
36     * @param serverName the server name
37     * @param hostName the host name
38     * @param loadMembers the load members
39     *
40     * @return the cluster
41     *
42     * @throws MalformedObjectNameException the malformed object name exception
43     */
44    public Cluster getCluster(String serverName, String hostName, boolean loadMembers)
45        throws MalformedObjectNameException {
46  
47      Cluster cluster = null;
48  
49      MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
50      ObjectName objectNameMembership =
51          new ObjectName(serverName + ":type=ClusterMembership,host=" + hostName);
52      ObjectName objectNameReceiver =
53          new ObjectName(serverName + ":type=ClusterReceiver,host=" + hostName);
54      ObjectName objectNameSender =
55          new ObjectName(serverName + ":type=ClusterSender,host=" + hostName);
56  
57      /*
58       * should be just one set, this is just to find out if this instance is cluster-enabled and the
59       * cluster supports JMX
60       */
61      Set<ObjectInstance> clusters =
62          mbeanServer.queryMBeans(new ObjectName("*:type=Cluster,host=" + hostName), null);
63      Set<ObjectInstance> membership = mbeanServer.queryMBeans(objectNameMembership, null);
64      if (clusters != null && !clusters.isEmpty() && membership != null && !membership.isEmpty()) {
65        ObjectName objectNameCluster = clusters.iterator().next().getObjectName();
66        cluster = new Cluster();
67  
68        cluster.setName(JmxTools.getStringAttr(mbeanServer, objectNameCluster, "clusterName"));
69        cluster.setInfo(JmxTools.getStringAttr(mbeanServer, objectNameCluster, "info"));
70        cluster.setManagerClassName(
71            JmxTools.getStringAttr(mbeanServer, objectNameCluster, "managerClassName"));
72  
73        cluster
74            .setMcastAddress(JmxTools.getStringAttr(mbeanServer, objectNameMembership, "mcastAddr"));
75        cluster.setMcastBindAddress(
76            JmxTools.getStringAttr(mbeanServer, objectNameMembership, "mcastBindAddress"));
77        cluster.setMcastClusterDomain(
78            JmxTools.getStringAttr(mbeanServer, objectNameMembership, "mcastClusterDomain"));
79        cluster.setMcastDropTime(
80            JmxTools.getLongAttr(mbeanServer, objectNameMembership, "mcastDropTime"));
81        cluster.setMcastFrequency(
82            JmxTools.getLongAttr(mbeanServer, objectNameMembership, "mcastFrequency"));
83        cluster.setMcastPort(JmxTools.getIntAttr(mbeanServer, objectNameMembership, "mcastPort"));
84        cluster.setMcastSoTimeout(
85            JmxTools.getIntAttr(mbeanServer, objectNameMembership, "mcastSoTimeout"));
86        cluster.setMcastTtl(JmxTools.getIntAttr(mbeanServer, objectNameMembership, "mcastTTL"));
87  
88        cluster.setTcpListenAddress(
89            JmxTools.getStringAttr(mbeanServer, objectNameReceiver, "tcpListenAddress"));
90        cluster
91            .setTcpListenPort(JmxTools.getIntAttr(mbeanServer, objectNameReceiver, "tcpListenPort"));
92        cluster.setNrOfMsgsReceived(
93            JmxTools.getLongAttr(mbeanServer, objectNameReceiver, "nrOfMsgsReceived"));
94        cluster.setTotalReceivedBytes(
95            JmxTools.getLongAttr(mbeanServer, objectNameReceiver, "totalReceivedBytes"));
96        // cluster.setTcpSelectorTimeout(
97        // JmxTools.getLongAttr(mbeanServer, objectNameReceiver, "tcpSelectorTimeout"));
98        // cluster.setTcpThreadCount(
99        // JmxTools.getIntAttr(mbeanServer, objectNameReceiver, "tcpThreadCount"));
100 
101       cluster
102           .setSenderAckTimeout(JmxTools.getLongAttr(mbeanServer, objectNameSender, "ackTimeout"));
103       cluster.setSenderAutoConnect(
104           JmxTools.getBooleanAttr(mbeanServer, objectNameSender, "autoConnect"));
105       cluster.setSenderFailureCounter(
106           JmxTools.getLongAttr(mbeanServer, objectNameSender, "failureCounter"));
107       cluster.setSenderNrOfRequests(
108           JmxTools.getLongAttr(mbeanServer, objectNameSender, "nrOfRequests"));
109       cluster.setSenderReplicationMode(
110           JmxTools.getStringAttr(mbeanServer, objectNameSender, "replicationMode"));
111       cluster
112           .setSenderTotalBytes(JmxTools.getLongAttr(mbeanServer, objectNameSender, "totalBytes"));
113 
114       if (loadMembers) {
115         ObjectName[] senders = (ObjectName[]) JmxTools.getAttribute(mbeanServer, objectNameSender,
116             "senderObjectNames");
117         for (ObjectName objectNameLocalSender : senders) {
118           ClusterSender sender;
119 
120           if ("pooled".equals(cluster.getSenderReplicationMode())) {
121             sender = new PooledClusterSender();
122           } else if ("synchronous".equals(cluster.getSenderReplicationMode())) {
123             sender = new SyncClusterSender();
124           } else if ("asynchronous".equals(cluster.getSenderReplicationMode())
125               || "fastasyncqueue".equals(cluster.getSenderReplicationMode())) {
126             sender = new AsyncClusterSender();
127           } else {
128             sender = new ClusterSender();
129           }
130 
131           sender.setAddress(JmxTools.getStringAttr(mbeanServer, objectNameLocalSender, "address"));
132           sender.setPort(JmxTools.getIntAttr(mbeanServer, objectNameLocalSender, "port"));
133 
134           sender.setAvgMessageSize(
135               JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "avgMessageSize", -1));
136           sender.setAvgProcessingTime(
137               JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "avgProcessingTime", -1));
138 
139           sender.setConnectCounter(
140               JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "connectCounter"));
141           sender.setDisconnectCounter(
142               JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "disconnectCounter"));
143           sender.setConnected(
144               JmxTools.getBooleanAttr(mbeanServer, objectNameLocalSender, "connected"));
145           sender.setKeepAliveTimeout(
146               JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "keepAliveTimeout"));
147           sender.setNrOfRequests(
148               JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "nrOfRequests"));
149           sender.setTotalBytes(
150               JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "totalBytes"));
151           sender.setResend(JmxTools.getBooleanAttr(mbeanServer, objectNameLocalSender, "resend"));
152           sender.setSuspect(JmxTools.getBooleanAttr(mbeanServer, objectNameLocalSender, "suspect"));
153 
154           if (sender instanceof PooledClusterSender) {
155             ((PooledClusterSender) sender).setMaxPoolSocketLimit(
156                 JmxTools.getIntAttr(mbeanServer, objectNameLocalSender, "maxPoolSocketLimit"));
157           }
158 
159           if (sender instanceof SyncClusterSender) {
160             SyncClusterSender syncSender = (SyncClusterSender) sender;
161             syncSender.setDataFailureCounter(
162                 JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "dataFailureCounter"));
163             syncSender.setDataResendCounter(
164                 JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "dataResendCounter"));
165             syncSender.setSocketOpenCounter(
166                 JmxTools.getIntAttr(mbeanServer, objectNameLocalSender, "socketOpenCounter"));
167             syncSender.setSocketCloseCounter(
168                 JmxTools.getIntAttr(mbeanServer, objectNameLocalSender, "socketCloseCounter"));
169             syncSender.setSocketOpenFailureCounter(JmxTools.getIntAttr(mbeanServer,
170                 objectNameLocalSender, "socketOpenFailureCounter"));
171           }
172 
173           if (sender instanceof AsyncClusterSender) {
174             AsyncClusterSender asyncSender = (AsyncClusterSender) sender;
175             asyncSender.setInQueueCounter(
176                 JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "inQueueCounter"));
177             asyncSender.setOutQueueCounter(
178                 JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "outQueueCounter"));
179             asyncSender
180                 .setQueueSize(JmxTools.getIntAttr(mbeanServer, objectNameLocalSender, "queueSize"));
181             asyncSender.setQueuedNrOfBytes(
182                 JmxTools.getLongAttr(mbeanServer, objectNameLocalSender, "queuedNrOfBytes"));
183           }
184           cluster.getMembers().add(sender);
185         }
186       }
187     }
188     return cluster;
189   }
190 
191 }