public void TestMBeanServer() { IMBeanServer mbeanServer = mbeanServerExtension.MBeanServer; var name = new ObjectName("MBeanServer:component=ValidMBean"); MBeanInfo info = mbeanServer.GetMBeanInfo(name); Assert.AreEqual("MBean", info.Description); Assert.AreEqual("CrimsonLogic.Common.Library.Tests.ValidInstanceMBean, CrimsonLogic.Common.Library.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5fb9aab394206ec1", info.ClassName); foreach (MBeanAttributeInfo attributeInfo in info.Attributes) { string attribute = string.Format("Attribute {0} ({1}) [{2}{3}]: {4}", attributeInfo.Name, attributeInfo.Description, attributeInfo.Readable ? "r" : "", attributeInfo.Writable ? "w" : "", attributeInfo.Type); Assert.AreEqual("Attribute Counter (Counter value) [rw]: System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", attribute); } int index = 0; foreach (MBeanOperationInfo operationInfo in info.Operations) { string operation = string.Format("Operation {0} ({1}) [{2}]", operationInfo.Name, operationInfo.Description, operationInfo.Impact); Assert.AreEqual( index == 0 ? "Operation AddAmount (Adds specified value to value of the counter) [Unknown]" : "Operation ResetCounter (Sets counter value to 0) [Unknown]", operation); index++; } object counter = mbeanServer.GetAttribute(name, "Counter"); Assert.AreEqual(0, counter); mbeanServer.SetAttribute(name, "Counter", 5); counter = mbeanServer.GetAttribute(name, "Counter"); Assert.AreEqual(5, counter); mbeanServer.Invoke(name, "AddAmount", new object[] { 5 }); counter = mbeanServer.GetAttribute(name, "Counter"); Assert.AreEqual(10, counter); mbeanServer.Invoke(name, "ResetCounter", new object[] { }); counter = mbeanServer.GetAttribute(name, "Counter"); Assert.AreEqual(0, counter); Logger.Debug("Completed testing the MBeanServer!"); }
static void Main(string[] args) { IMBeanServer server = MBeanServerFactory.CreateMBeanServer(); Counter o = new Counter(); ObjectName name = new ObjectName("QuickStart:type=counter"); server.RegisterMBean(o, name); Console.WriteLine("******"); MBeanInfo info = server.GetMBeanInfo(name); Console.WriteLine("MBean description: {0}", info.Description); Console.WriteLine("MBean class name: {0}", info.ClassName); foreach (MBeanAttributeInfo attributeInfo in info.Attributes) { Console.WriteLine("Attribute {0} ({1}) [{2}{3}]: {4}", attributeInfo.Name, attributeInfo.Description, attributeInfo.Readable ? "r" : "", attributeInfo.Writable ? "w" : "", attributeInfo.Type); } foreach (MBeanOperationInfo operationInfo in info.Operations) { Console.WriteLine("Operation {0} ({1}) [{2}]", operationInfo.Name, operationInfo.Description, operationInfo.Impact); } Console.WriteLine("******"); server.AddNotificationListener(name, CounterChanged, null, null); object counter = server.GetAttribute(name, "Value"); Console.WriteLine("Counter value is {0}", counter); server.SetAttribute(name, "Value", 5); counter = server.GetAttribute(name, "Value"); Console.WriteLine("Now, counter value is {0}", counter); counter = server.Invoke(name, "Add", new object[] { 5 }); counter = server.GetAttribute(name, "Value"); Console.WriteLine("Now, counter value is {0}", counter); counter = server.Invoke(name, "Reset", new object[] { }); counter = server.GetAttribute(name, "Value"); Console.WriteLine("Now, counter value is {0}", counter); server.RemoveNotificationListener(name, CounterChanged, null, null); Console.ReadKey(); }
protected IComparable GetObservedValue(ObjectName objectName, Type desiredType, out string errorType, out string errorDescription) { object result = null; errorType = null; errorDescription = null; try { result = _server.GetAttribute(objectName, _observedAttribute); if (result == null || ((desiredType == null && Array.IndexOf(SupportedTypes, result.GetType()) == -1) || (desiredType != null && desiredType != result.GetType()))) { errorType = MonitorNotification.ObservedAttributeTypeError; result = null; } } catch (InstanceNotFoundException) { errorType = MonitorNotification.ObservedObjectError; } catch (AttributeNotFoundException) { errorType = MonitorNotification.ObservedAttributeError; } catch (Exception ex) { errorType = MonitorNotification.RuntimeError; errorDescription = ex.Message; } return((IComparable)result); }