コード例 #1
0
        public static Dictionary <string, TopicStats> GetTopicStat(string cacheName, string initialNodeName, Runtime.CacheManagement.CacheContext context, int port)
        {
            CacheService cacheService = GetCacheService(context);

            if (port != 0)
            {
                cacheService.Port = port;
            }
            string            startingNode      = initialNodeName;
            CacheServerConfig cacheServerConfig = null;
            ICacheServer      cacheServer       = null;



            try
            {
                if (initialNodeName.Equals(string.Empty))
                {
                    cacheServerConfig = GetCacheConfigThroughClientConfig(cacheName, port, context);
                    if (cacheServerConfig == null)
                    {
                        throw new ManagementException("cache with name " + cacheName + " not found in " + config);
                    }
                }
                else
                {
                    cacheService.ServerName = initialNodeName;
                    cacheServer             = cacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                    if (cacheServer == null)
                    {
                        throw new ManagementException("provided initial node not available");
                    }

                    cacheServerConfig = cacheServer.GetCacheConfiguration(cacheName);
                    if (cacheServerConfig == null)
                    {
                        throw new ManagementException("cache with name " + cacheName + " not registered on specified node");
                    }
                }

                //For Local Cache
                if (cacheServerConfig.CacheType.Equals(LOCALCACHE, StringComparison.OrdinalIgnoreCase))
                {
                    if (cacheServerConfig.InProc)
                    {
                        throw new ArgumentException("API is not supported for Local Inproc Cache");
                    }

                    cacheService.ServerName = Environment.MachineName;
                    cacheServer             = cacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30));

                    if (cacheServer != null && cacheServer.IsRunning(cacheName))
                    {
                        return(cacheServer.GetTopicStats(cacheServerConfig.Name));
                    }
                }
                //For Clustered Cache
                else
                {
                    Dictionary <string, TopicStats> topicWiseStat = new Dictionary <string, TopicStats>();
                    ArrayList initialHost = InitialHostList(cacheServerConfig.Cluster.Channel.InitialHosts);
                    foreach (object host in initialHost)
                    {
                        try
                        {
                            cacheService.ServerName = (string)host;
                            cacheServer             = cacheService.GetCacheServer(new TimeSpan(0, 0, 0, 30));
                            if (cacheServer.IsRunning(cacheName))
                            {
                                Dictionary <string, TopicStats> NodeWisetopicStat = cacheServer.GetTopicStats(cacheServerConfig.Name);
                                if (NodeWisetopicStat != null)
                                {
                                    foreach (var item in NodeWisetopicStat)
                                    {
                                        if (!topicWiseStat.ContainsKey(item.Key))
                                        {
                                            item.Value.TopicName = item.Key;
                                            topicWiseStat.Add(item.Key, ((TopicStats)item.Value.Clone()));
                                        }
                                        else
                                        {
                                            TopicStats topicStat = topicWiseStat[item.Key];
                                            topicStat.CurrentMessageCount += item.Value.CurrentMessageCount;
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                        }
                    }
                    return(topicWiseStat);
                }
            }
            catch (Exception ex)
            {
                throw new ManagementException(ex.Message);
            }
            finally
            {
                if (cacheServer != null)
                {
                    cacheServer.Dispose();
                }
                cacheService.Dispose();
            }
            return(null);
        }