/** * {@inheritDoc} */ @Override public void afterPropertiesSet() throws Exception { for (JmxSensorTypeConfig config : configurationStorage.getJmxSensorTypes()) { if (config.getClassName().equals(this.getClass().getName())) { this.init(config); break; } } }
/** * {@inheritDoc} */ public void init(JmxSensorTypeConfig sensorTypeConfig) { this.sensorTypeConfig = sensorTypeConfig; // check for forcing server creation Map<String, Object> parameters = sensorTypeConfig.getParameters(); if (MapUtils.isNotEmpty(parameters)) { if (Boolean.TRUE.equals(parameters.get("forceMBeanServer"))) { // create only, get it via hook MBeanServer server = ManagementFactory.getPlatformMBeanServer(); mbeanServerAdded(server); } } }
/** * {@inheritDoc} */ @Override public void update(ICoreService coreService) { long sensorTypeIdent = sensorTypeConfig.getId(); long currentTime = System.currentTimeMillis(); // Check if the collectData method should be invoked if (MapUtils.isNotEmpty(activeServerMap) && ((currentTime - lastDataCollectionTimestamp) > DATA_COLLECT_INTERVAL)) { // store the invocation timestamp lastDataCollectionTimestamp = System.currentTimeMillis(); for (MBeanServerHolder holder : activeServerMap.values()) { collectData(holder, coreService, sensorTypeIdent); } } }
@Test public void sensorSet() { JmxSensorTypeConfig sensorTypeConfig1 = mock(JmxSensorTypeConfig.class); jmxSensor.init(sensorTypeConfig1); assertThat(jmxSensor.getSensorTypeConfig(), is(sensorTypeConfig1)); }
@Test public void listenerUnregistered() throws Exception { ArgumentCaptor<MBeanServerHolder> captor = ArgumentCaptor.forClass(MBeanServerHolder.class); jmxSensor.mbeanServerAdded(mBeanServer); jmxSensor.mbeanServerRemoved(mBeanServer); verify(mBeanServer).removeNotificationListener(Matchers.<ObjectName> any(), captor.capture(), Matchers.<NotificationFilter> any(), eq(null)); assertThat(captor.getValue().mBeanServer, is(mBeanServer)); } }
when(connection.analyzeJmxAttributes(eq(platformIdent), Matchers.<Collection<JmxAttributeDescriptor>> any())).thenReturn(Collections.<JmxAttributeDescriptor> emptyList()); when(mBeanServer.getAttribute(objectName, testAttributeName)).thenReturn(value); jmxSensor.mbeanServerAdded(mBeanServer); jmxSensor.update(coreService);
@Test public void nothingToCollect() { jmxSensor.update(coreService); verifyZeroInteractions(coreService); }
/** * {@inheritDoc} */ @Override public void mbeanServerAdded(MBeanServer server) { // if null or already registered do nothing if ((null == server) || activeServerMap.containsKey(server)) { return; } try { // create holder MBeanServerHolder holder = new MBeanServerHolder(server); // register listener try { server.addNotificationListener(new ObjectName(MBEAN_SERVER_DELEGATE_NAME), holder, NOTIFICATION_FILTER, null); } catch (Exception e) { log.warn("Failed to add notification listener to the MBean server " + server.toString() + ". New added beans/attributes will not be monitored.", e); } // register already existing beans registerMBeans(holder, null); // add to map if it all works activeServerMap.put(server, holder); } catch (Throwable t) { // NOPMD // catching throwable if anything goes wrong log.warn("Unable to add the MBean server " + server.toString() + ".", t); } }
@Test public void unknownServer() throws Exception { jmxSensor.mbeanServerRemoved(mBeanServer); verifyZeroInteractions(mBeanServer); }
value = "null"; } else if (collectedValue.getClass().isArray()) { value = getArrayValue(collectedValue); } else { value = collectedValue.toString();
jmxSensor.mbeanServerAdded(mBeanServer); jmxSensor.update(coreService); jmxSensor.lastDataCollectionTimestamp = 0; jmxSensor.update(coreService);
MBeanServerNotification notification = new MBeanServerNotification(MBeanServerNotification.UNREGISTRATION_NOTIFICATION, this, 1, objectName); jmxSensor.mbeanServerAdded(mBeanServer); ArgumentCaptor<MBeanServerHolder> notificationListener = ArgumentCaptor.forClass(MBeanServerHolder.class); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), notificationListener.capture(), Matchers.<NotificationFilter> any(), eq(null)); notificationListener.getValue().handleNotification(notification, null); jmxSensor.update(coreService);
@Test public void nullServer() { jmxSensor.mbeanServerAdded(null); verifyZeroInteractions(connection); }
@BeforeMethod public void init() { jmxSensor.init(sensorTypeConfig); }
jmxSensor.mbeanServerAdded(mBeanServer); jmxSensor.update(coreService);
@Test public void listenerRegistered() throws Exception { ArgumentCaptor<MBeanServerHolder> captor = ArgumentCaptor.forClass(MBeanServerHolder.class); jmxSensor.mbeanServerAdded(mBeanServer); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), captor.capture(), Matchers.<NotificationFilter> any(), eq(null)); assertThat(captor.getValue().mBeanServer, is(mBeanServer)); }
@BeforeMethod public void init() { jmxSensor.init(sensorTypeConfig); }
jmxSensor.mbeanServerAdded(mBeanServer); jmxSensor.update(coreService);
@Test public void connectionOff() throws Exception { when(connection.isConnected()).thenReturn(false); jmxSensor.mbeanServerAdded(mBeanServer); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), Matchers.<NotificationListener> any(), Matchers.<NotificationFilter> any(), eq(null)); verifyNoMoreInteractions(mBeanServer); }
@BeforeMethod public void init() { jmxSensor.init(sensorTypeConfig); }