本文整理汇总了Java中javax.management.modelmbean.RequiredModelMBean.addAttributeChangeNotificationListener方法的典型用法代码示例。如果您正苦于以下问题:Java RequiredModelMBean.addAttributeChangeNotificationListener方法的具体用法?Java RequiredModelMBean.addAttributeChangeNotificationListener怎么用?Java RequiredModelMBean.addAttributeChangeNotificationListener使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.management.modelmbean.RequiredModelMBean
的用法示例。
在下文中一共展示了RequiredModelMBean.addAttributeChangeNotificationListener方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNegative
import javax.management.modelmbean.RequiredModelMBean; //导入方法依赖的package包/类
/**
* Verify that value of attribute return value which return getter method if
* currencyTimeLimit < 0.
* <ul>
* Step by step:
* <li>1.Create java class, which is not MBean. This class has getter and
* setter methods.
* <li>2.Create ModelMBeanAttibuteInfo object for class in 1st step.
* <li>3.Extract descriptor from ModelMBeanAttibuteInfo class and set
* additional fields currencyTimeLimit < 0 and setMethod=nameOfSetterMethod.
* <li>4.Create ModelMBeanInfoSupport object.
* <li>5.Create RequiredModelMBean object.
* <li>6.Create notification listener and register it in RequiredModelMBean
* object for attribute using addAttributeChangeNotificationListener.
* <li>7.Instance of java class in 1st step sets managed resource for
* RequiredModelMBean using setManagedResource method.
* <li>8.Create objectName.
* <li>9.Register RequiredModelMBean object in MBeanServer with objectName
* specified in previous step.
* <li>10.Set attribute on MBeanServer using setAttribute method.
* <li>11.Verify that notification listener was notified about change
* attribute.
* <li>12.Verify that setter method was invoked.
* <li>13.Verify that getAttribute of MBeanServer returns correct value of
* attribute and getter method was invoked.
* <li>14. Invoke again getAttribute of MBeanServer method and verify
* returned value. Also verify that getter method was invoked.
* </ul>
*/
public Result testNegative() throws Exception {
Method getter = class1.getMethod("getG", null);
Method setter = class1.getMethod("setG", new Class[] { String.class });
ModelMBeanAttributeInfo attributeInfoForG = new ModelMBeanAttributeInfo(
"name", "description", getter, setter);
Descriptor descriptor = attributeInfoForG.getDescriptor();
descriptor.setField("currencyTimeLimit", "-1");
descriptor.setField("setMethod", setter.getName());
descriptor.setField("getMethod", getter.getName());
attributeInfoForG.setDescriptor(descriptor);
ModelMBeanAttributeInfo[] attributeInfos = new ModelMBeanAttributeInfo[] { attributeInfoForG };
ModelMBeanOperationInfo[] operationInfos = new ModelMBeanOperationInfo[] {
new ModelMBeanOperationInfo("description", setter),
new ModelMBeanOperationInfo("description", getter) };
ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport(
class1.getName(), "description", attributeInfos, null,
operationInfos, null);
RequiredModelMBean requiredModelMBean = new RequiredModelMBean(
beanInfoSupport);
ManageResourceAndNotification notificationListener = new ManageResourceAndNotification();
requiredModelMBean.addAttributeChangeNotificationListener(
notificationListener, attributeInfoForG.getName(),
ManageResourceAndNotification.handback);
ManageResourceAndNotification managedResource = new ManageResourceAndNotification();
requiredModelMBean.setManagedResource(managedResource,
"ObjectReference");
ObjectName objectName = new ObjectName("domain", "name", "simple name");
MBeanServer server = MBeanServerFactory.createMBeanServer();
server.registerMBean(requiredModelMBean, objectName);
String newValue = "new value";
server.setAttribute(objectName, new Attribute(attributeInfoForG
.getName(), newValue));
assertTrue(notificationListener.isWasHandleNotificationInvoked());
assertEquals(managedResource.getG(), newValue);
managedResource.isGetGWasInvoked();
assertEquals(server.getAttribute(objectName, attributeInfoForG
.getName()), newValue);
assertTrue(managedResource.isGetGWasInvoked());
assertTrue(server.getMBeanInfo(objectName).getAttributes()[0] == attributeInfoForG);
assertNull(attributeInfoForG.getDescriptor().getFieldValue("value"));
return result();
}
示例2: testZero
import javax.management.modelmbean.RequiredModelMBean; //导入方法依赖的package包/类
/**
* Verify that value of attribute retrieves a value from cache if
* currencyTimeLimit = 0.
* <ul>
* Step by step:
* <li>1.Create java class, which is not MBean. This class has getter and
* setter methods.
* <li>2.Create ModelMBeanAttibuteInfo object for class in 1st step.
* <li>3.Extract descriptor from ModelMBeanAttibuteInfo class and set
* additional fields currencyTimeLimit = 0 and setMethod=nameOfSetterMethod.
* <li>4.Create ModelMBeanInfoSupport object.
* <li>5.Create RequiredModelMBean object.
* <li>6.Create notification listener and register it in RequiredModelMBean
* object for attribute using addAttributeChangeNotificationListener.
* <li>7.Instance of java class in 1st step sets managed resource for
* RequiredModelMBean using setManagedResource method.
* <li>8.Create objectName.
* <li>9.Register RequiredModelMBean object in MBeanServer with objectName
* specified in previous step.
* <li>10.Set attribute on MBeanServer using setAttribute method.
* <li>11.Verify that notification listener was notified about change
* attribute.
* <li>12.Verify that setter method was invoked.
* <li>13.Verify that getAttribute returns correct value of attribute and
* getter method was not invoked.
* <li>14.Verify value of field value of descriptor of
* ModelMBeanAttibuteInfo.
*/
public Result testZero() throws Exception {
Method getter = class1.getMethod("getG", null);
Method setter = class1.getMethod("setG", new Class[] { String.class });
ModelMBeanAttributeInfo attributeInfoForG = new ModelMBeanAttributeInfo(
"name", "description", getter, setter);
Descriptor descriptor = attributeInfoForG.getDescriptor();
descriptor.setField("currencyTimeLimit", "0");
descriptor.setField("setMethod", setter.getName());
descriptor.setField("getMethod", getter.getName());
attributeInfoForG.setDescriptor(descriptor);
ModelMBeanAttributeInfo[] attributeInfos = new ModelMBeanAttributeInfo[] { attributeInfoForG };
ModelMBeanOperationInfo[] operationInfos = new ModelMBeanOperationInfo[] {
new ModelMBeanOperationInfo("description", setter),
new ModelMBeanOperationInfo("description", getter) };
ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport(
class1.getName(), "description", attributeInfos, null,
operationInfos, null);
RequiredModelMBean requiredModelMBean = new RequiredModelMBean(
beanInfoSupport);
ManageResourceAndNotification notificationListener = new ManageResourceAndNotification();
requiredModelMBean.addAttributeChangeNotificationListener(
notificationListener, attributeInfoForG.getName(),
ManageResourceAndNotification.handback);
ManageResourceAndNotification managedResource = new ManageResourceAndNotification();
requiredModelMBean.setManagedResource(managedResource,
"ObjectReference");
ObjectName objectName = new ObjectName("domain", "name", "simple name");
MBeanServer server = MBeanServerFactory.createMBeanServer();
server.registerMBean(requiredModelMBean, objectName);
String newValue = "new value";
server.setAttribute(objectName, new Attribute(attributeInfoForG
.getName(), newValue));
assertTrue(notificationListener.isWasHandleNotificationInvoked());
assertEquals(managedResource.getG(), newValue);
managedResource.isGetGWasInvoked();
assertEquals(server.getAttribute(objectName, attributeInfoForG
.getName()), newValue);
assertFalse(managedResource.isGetGWasInvoked());
assertTrue(server.getMBeanInfo(objectName).getAttributes()[0] == attributeInfoForG);
assertEquals(attributeInfoForG.getDescriptor().getFieldValue("value"),
newValue);
return result();
}
示例3: testNotPresent
import javax.management.modelmbean.RequiredModelMBean; //导入方法依赖的package包/类
/**
* Verify that value of attribute always retrieves from returned value of
* getter method if currencyTimeLimit is not defined in descriptor.
* <p>
* Instructions are the same as in testNegative.
*/
public Result testNotPresent() throws Exception {
Method getter = class1.getMethod("getG", null);
Method setter = class1.getMethod("setG", new Class[] { String.class });
ModelMBeanAttributeInfo attributeInfoForG = new ModelMBeanAttributeInfo(
"name", "description", getter, setter);
Descriptor descriptor = attributeInfoForG.getDescriptor();
descriptor.setField("setMethod", setter.getName());
descriptor.setField("getMethod", getter.getName());
attributeInfoForG.setDescriptor(descriptor);
ModelMBeanAttributeInfo[] attributeInfos = new ModelMBeanAttributeInfo[] { attributeInfoForG };
ModelMBeanOperationInfo[] operationInfos = new ModelMBeanOperationInfo[] {
new ModelMBeanOperationInfo("description", setter),
new ModelMBeanOperationInfo("description", getter) };
ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport(
class1.getName(), "description", attributeInfos, null,
operationInfos, null);
RequiredModelMBean requiredModelMBean = new RequiredModelMBean(
beanInfoSupport);
ManageResourceAndNotification notificationListener = new ManageResourceAndNotification();
requiredModelMBean.addAttributeChangeNotificationListener(
notificationListener, attributeInfoForG.getName(),
ManageResourceAndNotification.handback);
ManageResourceAndNotification managedResource = new ManageResourceAndNotification();
requiredModelMBean.setManagedResource(managedResource,
"ObjectReference");
ObjectName objectName = new ObjectName("domain", "name", "simple name");
MBeanServer server = MBeanServerFactory.createMBeanServer();
server.registerMBean(requiredModelMBean, objectName);
String newValue = "new value";
server.setAttribute(objectName, new Attribute(attributeInfoForG
.getName(), newValue));
assertTrue(notificationListener.isWasHandleNotificationInvoked());
assertEquals(managedResource.getG(), newValue);
managedResource.isGetGWasInvoked();
assertEquals(server.getAttribute(objectName, attributeInfoForG
.getName()), newValue);
assertTrue(managedResource.isGetGWasInvoked());
assertTrue(server.getMBeanInfo(objectName).getAttributes()[0] == attributeInfoForG);
assertNull(attributeInfoForG.getDescriptor().getFieldValue("value"));
return result();
}