private String getHandlerBeanKey(MessageHandlerMetrics handler) {
   // This ordering of keys seems to work with default settings of JConsole
   return String.format(
       this.domain + ":type=MessageHandler,name=%s,bean=%s" + getStaticNames(),
       handler.getManagedName(),
       handler.getManagedType());
 }
 @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Active Handler Count")
 public long getActiveHandlerCountLong() {
   int count = 0;
   for (MessageHandlerMetrics monitor : this.handlers) {
     count += monitor.getActiveCountLong();
   }
   return count;
 }
 private void registerHandlers() {
   for (MessageHandlerMetrics handler : this.handlers) {
     MessageHandlerMetrics monitor = enhanceHandlerMonitor(handler);
     String name = monitor.getManagedName();
     this.allHandlersByName.put(name, monitor);
     if (!matches(this.componentNamePatterns, name)) {
       continue;
     }
     // Only register once...
     if (!this.handlersByName.containsKey(name)) {
       String beanKey = getHandlerBeanKey(monitor);
       if (name != null) {
         this.handlersByName.put(name, monitor);
       }
       registerBeanNameOrInstance(monitor, beanKey);
     }
   }
 }
  private MessageHandlerMetrics enhanceHandlerMonitor(MessageHandlerMetrics monitor) {

    MessageHandlerMetrics result = monitor;

    if (monitor.getManagedName() != null && monitor.getManagedType() != null) {
      return monitor;
    }

    // Assignment algorithm and bean id, with bean id pulled reflectively out of enclosing endpoint
    // if possible
    String[] names = this.applicationContext.getBeanNamesForType(AbstractEndpoint.class);

    String name = null;
    String endpointName = null;
    String source = "endpoint";
    Object endpoint = null;

    for (String beanName : names) {
      endpoint = this.applicationContext.getBean(beanName);
      try {
        Object field = extractTarget(getField(endpoint, "handler"));
        if (field == monitor
            || this.extractTarget(this.handlerInAnonymousWrapper(field)) == monitor) {
          name = beanName;
          endpointName = beanName;
          break;
        }
      } catch (Exception e) {
        logger.trace("Could not get handler from bean = " + beanName);
      }
    }
    if (name != null && endpoint != null && name.startsWith("_org.springframework.integration")) {
      name = getInternalComponentName(name);
      source = "internal";
    }
    if (name != null && endpoint != null && name.startsWith("org.springframework.integration")) {
      Object target = endpoint;
      if (endpoint instanceof Advised) {
        TargetSource targetSource = ((Advised) endpoint).getTargetSource();
        if (targetSource != null) {
          try {
            target = targetSource.getTarget();
          } catch (Exception e) {
            logger.debug("Could not get handler from bean = " + name);
          }
        }
      }
      Object field = getField(target, "inputChannel");
      if (field != null) {
        if (!this.anonymousHandlerCounters.containsKey(field)) {
          this.anonymousHandlerCounters.put(field, new AtomicLong());
        }
        AtomicLong count = this.anonymousHandlerCounters.get(field);
        long total = count.incrementAndGet();
        String suffix = "";
        /*
         * Short hack to makes sure object names are unique if more than one endpoint has the same input channel
         */
        if (total > 1) {
          suffix = "#" + total;
        }
        name = field + suffix;
        source = "anonymous";
      }
    }

    if (endpoint instanceof Lifecycle) {
      // Wrap the monitor in a lifecycle so it exposes the start/stop operations
      if (monitor instanceof MappingMessageRouterManagement) {
        if (monitor instanceof TrackableComponent) {
          result =
              new TrackableRouterMetrics(
                  (Lifecycle) endpoint, (MappingMessageRouterManagement) monitor);
        } else {
          result =
              new RouterMetrics((Lifecycle) endpoint, (MappingMessageRouterManagement) monitor);
        }
      } else {
        if (monitor instanceof TrackableComponent) {
          result = new LifecycleTrackableMessageHandlerMetrics((Lifecycle) endpoint, monitor);
        } else {
          result = new LifecycleMessageHandlerMetrics((Lifecycle) endpoint, monitor);
        }
      }
    }

    if (name == null) {
      if (monitor instanceof NamedComponent) {
        name = ((NamedComponent) monitor).getComponentName();
      }
      if (name == null) {
        name = monitor.toString();
      }
      source = "handler";
    }

    if (endpointName != null) {
      this.beansByEndpointName.put(name, endpointName);
    }

    monitor.setManagedType(source);
    monitor.setManagedName(name);

    return result;
  }