All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.MetricsAgents.JMX.JMXAgent Maven / Gradle / Ivy

The newest version!
package Alachisoft.NCache.MetricsAgents.JMX;

import Alachisoft.NCache.Common.Logger.ILogger;
import Alachisoft.NCache.DataModel.CounterData;
import Alachisoft.NCache.MetricsAgents.JMX.Metrics.CounterStore;
import Alachisoft.NCache.MetricsAgents.JMX.Metrics.CountersMBean;
import Alachisoft.NCache.MetricsAgents.MetricsAgent;
import Util.ExecutionServiceConfiguration;
import com.alachisoft.ncache.common.monitoring.CounterMetadata;
import com.alachisoft.ncache.common.monitoring.Publisher;

import javax.management.*;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.*;

public class JMXAgent implements MetricsAgent {

    private ILogger _logger;
    private final int JMXListeningPort = ExecutionServiceConfiguration.getJmxPort();
    private final String domainName = "com.alachisoft.ncache";
    private Map categoryCollection;
    private boolean enableJmxPublishing;
    MBeanServer platformMBeanServer;

    @Override
    public void initialize(ILogger logger) {
        _logger = logger;
        categoryCollection = new HashMap<>();
        platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
    }

    public JMXAgent(boolean enableJmxPublishing) {
        this.enableJmxPublishing = enableJmxPublishing;
    }

    @Override
    public void start() {
        try {

            startJMXAgent();
            _logger.Info("Metric Server", "JMX Started at port " + JMXListeningPort);

        } catch (RemoteException e) {
            _logger.Error("Metric Server", e.getMessage());
        } catch (IOException e) {
            _logger.Error("Metric Server", e.getMessage());
        }
    }

    @Override
    public void initializeCounterInstance(String instanceName, String counterName, CounterMetadata counterMetaData) {

//        try {
//            instanceName = extractInstanceId(instanceName);
//            String counterCategory = counterMetaData.getCategory().toString();
//
//            if (cacheCollection.containsKey(instanceName)){
//
//                if (cacheCollection.get(instanceName).ContainsInstance(counterCategory)){
//
//                    CountersMBean countersMBean = cacheCollection.get(instanceName).GetJMXMBeanInstance(counterCategory);
//
//                    CounterData counterData = new CounterData();
//                    counterData.setCounterMetadata((Counter) counterMetaData);
//                    Attribute attribute = new Attribute(counterMetaData.getName(), counterData);
//                    countersMBean.setAttribute(attribute);
//                }
//                else {
//
//                    CountersMBean countersMBean = new CountersMBean();
//
//                    CounterData counterData = new CounterData();
//                    counterData.setCounterMetadata((Counter) counterMetaData);
//                    Attribute attribute = new Attribute(counterMetaData.getName(), counterData);
//                    countersMBean.setAttribute(attribute);
//
//                    cacheCollection.get(instanceName).AddJMXMBeanInstance(counterCategory, countersMBean);
//                    ObjectName objectName = new ObjectName("NCache:name="+instanceName+"."+counterCategory);
//                    platformMBeanServer.registerMBean(countersMBean, objectName);
//                }
//            }
//            else{
//                cacheCollection.put(instanceName, new CounterStore());
//
//                CountersMBean countersMBean = new CountersMBean();
//
//                CounterData counterData = new CounterData();
//                counterData.setCounterMetadata((Counter) counterMetaData);
//                Attribute attribute = new Attribute(counterMetaData.getName(), counterData);
//                countersMBean.setAttribute(attribute);
//
//                cacheCollection.get(instanceName).AddJMXMBeanInstance(counterCategory, countersMBean);
//                ObjectName objectName = new ObjectName("NCache:name="+instanceName+"."+counterCategory);
//                platformMBeanServer.registerMBean(countersMBean, objectName);
//
//            }
//
//        } catch (Exception e) {
//            _logger.Error("Metric Server", e.getMessage());
//        }
    }

    @Override
    public void publishCounterData(String instanceName, CounterData counterData, List counterParams) {

        if (!enableJmxPublishing)
            return;

        try {

            String counterName = counterData.getCounterMetadata().getName();
            String cacheName = "";
            String counterCategory = "";

            Publisher counterCategoryEnum = counterData.getCounterMetadata().getCategory();

            // Default cache and category names
            cacheName = checkIfReplica(instanceName) ? counterParams.get(0) + "_Replica" : counterParams.get(0);
            counterCategory = counterCategoryEnum.toString();

            if (counterCategoryEnum == Publisher.BridgedCache || counterCategoryEnum == Publisher.Bridge) {

                cacheName = counterCategoryEnum == Publisher.BridgedCache ? counterParams.get(2) : cacheName;
                counterCategory = "NCache" + Publisher.Bridge;
            }

            if (categoryCollection.containsKey(counterCategory)) {

                if (categoryCollection.get(counterCategory).ContainsInstance(cacheName)) {

                    CountersMBean countersMBean = categoryCollection.get(counterCategory).GetJMXMBeanInstance(cacheName);
                    countersMBean.setAttributeValue(counterName, counterData);
                } else {

                    CountersMBean countersMBean = new CountersMBean();
                    countersMBean.setAttributeValue(counterName, counterData);

                    categoryCollection.get(counterCategory).AddJMXMBeanInstance(cacheName, countersMBean);
                    Hashtable hashtable = new Hashtable<>();
                    hashtable.put("type", counterCategory);
                    hashtable.put("instanceName", cacheName);

                    ObjectName objectName = new ObjectName(domainName, hashtable);
                    platformMBeanServer.registerMBean(countersMBean, objectName);
                }
            } else {
                categoryCollection.put(counterCategory, new CounterStore());

                CountersMBean countersMBean = new CountersMBean();
                countersMBean.setAttributeValue(counterName, counterData);

                categoryCollection.get(counterCategory).AddJMXMBeanInstance(cacheName, countersMBean);
                Hashtable hashtable = new Hashtable<>();
                hashtable.put("type", counterCategory);
                hashtable.put("instanceName", cacheName);

                ObjectName objectName = new ObjectName(domainName, hashtable);
                platformMBeanServer.registerMBean(countersMBean, objectName);
            }

        } catch (Exception e) {
            _logger.Error("Metric Server", e.getMessage());
        }

    }

    @Override
    public void disposeCounterInstance(String instanceName, List counterParams) {
        try {

            String counterCategory = extractCategoryName(instanceName);
            String cacheName = "";

            if (counterCategory == Publisher.BridgedCache.toString()) {

                cacheName = counterParams.get(2);
                counterCategory = "NCache" + Publisher.Bridge.toString();
            } else {

                cacheName = checkIfReplica(instanceName) ? counterParams.get(0) + "_Replica" : counterParams.get(0);
            }

            if (categoryCollection != null && categoryCollection.containsKey(counterCategory)) {

                if (categoryCollection.get(counterCategory).ContainsInstance(cacheName)) {

                    ObjectName objectName = null;

                    Hashtable hashtable = new Hashtable<>();
                    hashtable.put("type", counterCategory);
                    hashtable.put("instanceName", cacheName);
                    objectName = new ObjectName(domainName, hashtable);
                    platformMBeanServer.unregisterMBean(objectName);
                    categoryCollection.get(counterCategory).RemoveJMXBeanInstance(cacheName);

                    if (categoryCollection.get(counterCategory).IsEmpty())
                        categoryCollection.remove(counterCategory);
                }
            }
        } catch (Exception e) {
            _logger.Error("Metric Server", e.getMessage());
        }
    }

    @Override
    public void dispose() {

    }

    private void startJMXAgent() throws IOException {

        // Create an RMI registry
        LocateRegistry.createRegistry(JMXListeningPort);

        // Create a JMX agent
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi://localhost:"+JMXListeningPort+"/jndi/rmi://localhost:"+JMXListeningPort+"/jmxrmi");
        JMXConnectorServer jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, mBeanServer);

        // Start the JMX agent
        jmxConnectorServer.start();
    }

    private boolean checkIfReplica(String instanceName) {
        String result = "";
        try {
            result = instanceName.split(":")[0];
            String[] splitReplica = result.split("_");
            result = splitReplica.length > 1 ? splitReplica[1] : "";
        } catch (Exception ignored) {
        }
        return result != "";
    }

    private String extractCategoryName(String instanceName) {
        try {
            if (instanceName.contains("ncache_bridgedcache"))
                return Publisher.BridgedCache.toString();
            else if (instanceName.contains("ncache_bridge"))
                return "NCache" + Publisher.Bridge.toString();
            else if (instanceName.contains("ncache_client"))
                return Publisher.NCacheClient.toString();
            else if (instanceName.contains("lucene"))
                return Publisher.NCacheLucene.toString();
            else if (instanceName.contains("persistence"))
                return Publisher.NCachePersistence.toString();
            else
                return Publisher.NCache.toString();
        } catch (Exception ignored) {
        }
        return Publisher.NCache.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy