@Override protected void disconnectSensors() { if (jmxFeed != null) jmxFeed.stop(); }
final SetMultimap<NotificationFilter, JmxNotificationSubscriptionConfig<?>> notificationSubscriptions = getConfig(NOTIFICATION_SUBSCRIPTIONS); final SetMultimap<List<?>, JmxOperationPollConfig<?>> operationPolls = getConfig(OPERATION_POLLS); final SetMultimap<String, JmxAttributePollConfig<?>> attributePolls = getConfig(ATTRIBUTE_POLLS); getPoller().submit(new Callable<Void>() { @Override public Void call() { getHelper().connect(getConfig(JMX_CONNECTION_TIMEOUT)); getPoller().submit(new Callable<Void>() { @Override public Void call() { registerAttributePoller(attributePolls.get(jmxAttributeName)); registerOperationPoller(operationPolls.get(operationIdentifier));
@Override protected void connectSensors() { String queue = String.format("org.apache.activemq:type=Broker,brokerName=%s,destinationType=Queue,destinationName=%s", getBrokerName(), getName()); jmxFeed = JmxFeed.builder() .entity(this) .helper(jmxHelper) .pollAttribute(new JmxAttributePollConfig<Integer>(QUEUE_DEPTH_MESSAGES) .objectName(queue) .attributeName("QueueSize")) .build(); }
/** * Registers to poll a jmx-attribute for an ObjectName, where all the given configs are for that same ObjectName + attribute. */ private void registerAttributePoller(Set<JmxAttributePollConfig<?>> configs) { Set<AttributePollHandler<? super Object>> handlers = Sets.newLinkedHashSet(); long minPeriod = Integer.MAX_VALUE; final ObjectName objectName = Iterables.get(configs, 0).getObjectName(); final String jmxAttributeName = Iterables.get(configs, 0).getAttributeName(); for (JmxAttributePollConfig<?> config : configs) { handlers.add(new AttributePollHandler<Object>(config, getEntity(), this)); if (config.getPeriod() > 0) minPeriod = Math.min(minPeriod, config.getPeriod()); } // TODO Not good calling this holding the synchronization lock getPoller().scheduleAtFixedRate( new Callable<Object>() { @Override public Object call() throws Exception { if (log.isTraceEnabled()) log.trace("jmx attribute polling for {} sensors at {} -> {}", new Object[] {getEntity(), getJmxUri(), jmxAttributeName}); return getHelper().getAttribute(objectName, jmxAttributeName); } }, new DelegatingPollHandler<Object>(handlers), minPeriod); }
AttributePollHandler<javax.management.Notification> handler = new AttributePollHandler<javax.management.Notification>(config, getEntity(), this) { @Override protected Object transformValueOnSuccess(javax.management.Notification val) { if (config.getOnNotification() != null) { getHelper().addNotificationListener(objectName, listener, filter);
private void unregisterNotificationListener(ObjectName objectName, NotificationListener listener) { try { getHelper().removeNotificationListener(objectName, listener); } catch (RuntimeException e) { log.warn("Failed to unregister listener: "+objectName+", "+listener+"; continuing...", e); } }
public String getJmxUri() { return getConfig(JMX_URI); }
@Override protected void connectSensors() { String queue = format("org.apache.qpid:type=VirtualHost.Queue,VirtualHost=\"%s\",name=\"%s\"", virtualHost, getName()); jmxFeed = JmxFeed.builder() .entity(this) .helper(jmxHelper) .pollAttribute(new JmxAttributePollConfig<Integer>(QUEUE_DEPTH_BYTES) .objectName(queue) .attributeName("QueueDepth")) .pollAttribute(new JmxAttributePollConfig<Integer>(QUEUE_DEPTH_MESSAGES) .objectName(queue) .attributeName("MessageCount")) .build(); }
@Override public Object call() throws Exception { if (log.isDebugEnabled()) log.debug("jmx operation polling for {} sensors at {} -> {}", new Object[] {getEntity(), getJmxUri(), operationName}); if (signature.size() == params.size()) { return getHelper().operation(objectName, operationName, signature, params); } else { return getHelper().operation(objectName, operationName, params.toArray()); } } },
@Override public String toString() { return "Connect JMX "+getHelper().getUrl(); } });
protected JmxHelper getHelper() { return getConfig(HELPER); }
@Override protected void disconnectSensors() { if (jmxFeed != null) jmxFeed.stop(); }
@Test public void testJmxOperationPolledForSensor() throws Exception { // This is awful syntax... final int opReturnVal = 123; final AtomicInteger invocationCount = new AtomicInteger(); MBeanOperationInfo opInfo = new MBeanOperationInfo(opName, "my descr", new MBeanParameterInfo[0], Integer.class.getName(), MBeanOperationInfo.ACTION); GeneralisedDynamicMBean mbean = jmxService.registerMBean( Collections.emptyMap(), ImmutableMap.of(opInfo, new Function<Object[], Integer>() { @Override public Integer apply(Object[] args) { invocationCount.incrementAndGet(); return opReturnVal; }}), objectName); feed = JmxFeed.builder() .entity(entity) .pollOperation(new JmxOperationPollConfig<Integer>(intAttribute) .objectName(objectName) .operationName(opName)) .build(); Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT_MS), new Runnable() { @Override public void run() { assertTrue(invocationCount.get() > 0, "invocationCount="+invocationCount); assertEquals(entity.getAttribute(intAttribute), (Integer)opReturnVal); }}); }
@Override protected boolean isConnected() { return super.isConnected() && getHelper().isConnected(); }