private void registerHandlers() {
   for (SimpleMessageHandlerMetrics source : handlers) {
     MessageHandlerMetrics monitor = enhanceHandlerMonitor(source);
     String name = monitor.getName();
     this.allHandlersByName.put(name, monitor);
     if (!PatternMatchUtils.simpleMatch(this.componentNamePatterns, name)) {
       continue;
     }
     // Only register once...
     if (!handlersByName.containsKey(name)) {
       String beanKey = getHandlerBeanKey(monitor);
       if (name != null) {
         handlersByName.put(name, monitor);
       }
       registerBeanNameOrInstance(monitor, beanKey);
       // Expose the raw bean if it is managed
       MessageHandler bean = source.getMessageHandler();
       if (assembler.includeBean(bean.getClass(), source.getName())) {
         registerBeanInstance(bean, this.getMonitoredIntegrationObjectBeanKey(bean, name));
       }
     }
   }
 }
  private MessageHandlerMetrics enhanceHandlerMonitor(SimpleMessageHandlerMetrics monitor) {

    MessageHandlerMetrics result = monitor;

    if (monitor.getName() != null && monitor.getSource() != null) {
      return monitor;
    }

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

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

    for (String beanName : names) {
      endpoint = beanFactory.getBean(beanName);
      Object field = null;
      try {
        field = extractTarget(getField(endpoint, "handler"));
      } catch (Exception e) {
        logger.trace("Could not get handler from bean = " + beanName);
      }
      if (field == monitor.getMessageHandler()) {
        name = beanName;
        endpointName = beanName;
        break;
      }
    }
    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 (!anonymousHandlerCounters.containsKey(field)) {
          anonymousHandlerCounters.put(field, new AtomicLong());
        }
        AtomicLong count = 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
      result = new LifecycleMessageHandlerMetrics((Lifecycle) endpoint, monitor);
    }

    if (name == null) {
      name = monitor.getMessageHandler().toString();
      source = "handler";
    }

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

    monitor.setSource(source);
    monitor.setName(name);

    return result;
  }