@Test public void testJmxNotificationMultipleSubscriptionUsingListener() throws Exception { final String one = "notification.one"; final String two = "notification.two"; final StandardEmitterMBean mbean = jmxService.registerMBean(ImmutableList.of(one, two), objectName); final AtomicInteger sequence = new AtomicInteger(0); feed = JmxFeed.builder() .entity(entity) .subscribeToNotification(new JmxNotificationSubscriptionConfig<Integer>(intAttribute) .objectName(objectName) .notificationFilter(JmxNotificationFilters.matchesTypes(one, two))) .build(); // Notification updates the sensor // Note that subscription is done async, so can't just send notification immediately during test. Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT_MS), new Runnable() { @Override public void run() { sendNotification(mbean, one, sequence.getAndIncrement(), 123); assertEquals(entity.getAttribute(intAttribute), (Integer)123); }}); // And wildcard means other notifications also received sendNotification(mbean, two, sequence.getAndIncrement(), 456); assertSensorEventually(intAttribute, 456, TIMEOUT_MS); }
@Test public void testJmxAttributePollerReturnsMBeanAttribute() throws Exception { GeneralisedDynamicMBean mbean = jmxService.registerMBean(ImmutableMap.of(attributeName, 42), objectName); feed = JmxFeed.builder() .entity(entity) .pollAttribute(new JmxAttributePollConfig<Integer>(intAttribute) .objectName(objectName) .period(50) .attributeName(attributeName)) .build(); // Starts with value defined when registering... assertSensorEventually(intAttribute, 42, TIMEOUT_MS); // Change the value and check it updates mbean.updateAttributeValue(attributeName, 64); assertSensorEventually(intAttribute, 64, TIMEOUT_MS); }
@Test public void testJmxNotificationSubscriptionForSensor() throws Exception { final String one = "notification.one", two = "notification.two"; final StandardEmitterMBean mbean = jmxService.registerMBean(ImmutableList.of(one, two), objectName); final AtomicInteger sequence = new AtomicInteger(0); feed = JmxFeed.builder() .entity(entity) .subscribeToNotification(new JmxNotificationSubscriptionConfig<Integer>(intAttribute) .objectName(objectName) .notificationFilter(JmxNotificationFilters.matchesType(one))) .build(); // Notification updates the sensor // Note that subscription is done async, so can't just send notification immediately during test. Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT_MS), new Runnable() { @Override public void run() { sendNotification(mbean, one, sequence.getAndIncrement(), 123); assertEquals(entity.getAttribute(intAttribute), (Integer)123); }}); // But other notification types are ignored sendNotification(mbean, two, sequence.getAndIncrement(), -1); Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT_MS), new Runnable() { @Override public void run() { assertEquals(entity.getAttribute(intAttribute), (Integer)123); }}); }
@Test public void testJmxOperationWithArgPolledForSensor() throws Exception { // This is awful syntax... MBeanParameterInfo paramInfo = new MBeanParameterInfo("param1", String.class.getName(), "my param1"); MBeanParameterInfo[] paramInfos = new MBeanParameterInfo[] {paramInfo}; MBeanOperationInfo opInfo = new MBeanOperationInfo(opName, "my descr", paramInfos, String.class.getName(), MBeanOperationInfo.ACTION); GeneralisedDynamicMBean mbean = jmxService.registerMBean( Collections.emptyMap(), ImmutableMap.of(opInfo, new Function<Object[], String>() { @Override public String apply(Object[] args) { return args[0]+"suffix"; }}), objectName); feed = JmxFeed.builder() .entity(entity) .pollOperation(new JmxOperationPollConfig<String>(stringAttribute) .objectName(objectName) .operationName(opName) .operationParams(ImmutableList.of("myprefix"))) .build(); assertSensorEventually(stringAttribute, "myprefix"+"suffix", TIMEOUT_MS); }
@Test public void testJmxAttributeSensor() throws Exception { GeneralisedDynamicMBean mbean = jmxService.registerMBean(ImmutableMap.of(attributeName, 42), objectName); String sensorName = "testJmxAttributeSensorName"; ConfigBag params = new ConfigBag(); params.put(AddSensor.SENSOR_NAME, sensorName); params.put(AddSensor.SENSOR_TYPE, "java.lang.Integer"); params.put(AddSensor.SENSOR_PERIOD, Duration.millis(SHORT_WAIT_MS)); params.put(JmxAttributeSensor.ATTRIBUTE, attributeName); params.put(JmxAttributeSensor.OBJECT_NAME, objectName); JmxAttributeSensor<Integer> mySensor = new JmxAttributeSensor<>(params); mySensor.apply(entity); final AttributeSensor<Integer> sensor = (AttributeSensor<Integer>) entity.getEntityType().getSensor(sensorName); // Starts with value defined when registering... assertSensorEventually(sensor, 42, TIMEOUT_MS); // Change the value and check it updates mbean.updateAttributeValue(attributeName, 64); assertSensorEventually(sensor, 64, TIMEOUT_MS); }
assertSensorEventually( mapAttribute, ImmutableMap.of("myint", 1234, "mystring", "on", "mybool", Boolean.TRUE),