Search in sources :

Example 1 with LifecycleMessageHandlerMetrics

use of org.springframework.integration.support.management.LifecycleMessageHandlerMetrics in project spring-integration by spring-projects.

the class IntegrationMBeanExporter method enhanceHandlerMonitor.

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.error("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;
}
Also used : TargetSource(org.springframework.aop.TargetSource) TrackableRouterMetrics(org.springframework.integration.support.management.TrackableRouterMetrics) Lifecycle(org.springframework.context.Lifecycle) MappingMessageRouterManagement(org.springframework.integration.support.management.MappingMessageRouterManagement) TrackableComponent(org.springframework.integration.support.management.TrackableComponent) NamedComponent(org.springframework.integration.support.context.NamedComponent) UnableToRegisterMBeanException(org.springframework.jmx.export.UnableToRegisterMBeanException) JMException(javax.management.JMException) BeansException(org.springframework.beans.BeansException) LifecycleTrackableMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics) LifecycleMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleMessageHandlerMetrics) MessageHandlerMetrics(org.springframework.integration.support.management.MessageHandlerMetrics) LifecycleTrackableMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics) AtomicLong(java.util.concurrent.atomic.AtomicLong) LifecycleMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleMessageHandlerMetrics) Advised(org.springframework.aop.framework.Advised) TrackableRouterMetrics(org.springframework.integration.support.management.TrackableRouterMetrics) RouterMetrics(org.springframework.integration.support.management.RouterMetrics)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)1 JMException (javax.management.JMException)1 TargetSource (org.springframework.aop.TargetSource)1 Advised (org.springframework.aop.framework.Advised)1 BeansException (org.springframework.beans.BeansException)1 Lifecycle (org.springframework.context.Lifecycle)1 NamedComponent (org.springframework.integration.support.context.NamedComponent)1 LifecycleMessageHandlerMetrics (org.springframework.integration.support.management.LifecycleMessageHandlerMetrics)1 LifecycleTrackableMessageHandlerMetrics (org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics)1 MappingMessageRouterManagement (org.springframework.integration.support.management.MappingMessageRouterManagement)1 MessageHandlerMetrics (org.springframework.integration.support.management.MessageHandlerMetrics)1 RouterMetrics (org.springframework.integration.support.management.RouterMetrics)1 TrackableComponent (org.springframework.integration.support.management.TrackableComponent)1 TrackableRouterMetrics (org.springframework.integration.support.management.TrackableRouterMetrics)1 UnableToRegisterMBeanException (org.springframework.jmx.export.UnableToRegisterMBeanException)1