Search in sources :

Example 1 with TrackableComponent

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

the class MessageHistoryConfigurer method start.

@ManagedOperation
@Override
public void start() {
    synchronized (this.lifecycleMonitor) {
        if (!this.running && this.beanFactory instanceof ListableBeanFactory) {
            for (TrackableComponent component : getTrackableComponents((ListableBeanFactory) this.beanFactory)) {
                String componentName = component.getComponentName();
                boolean shouldTrack = PatternMatchUtils.simpleMatch(this.componentNamePatterns, componentName);
                component.setShouldTrack(shouldTrack);
                if (shouldTrack) {
                    this.currentlyTrackedComponentNames.add(componentName);
                    if (this.logger.isInfoEnabled()) {
                        this.logger.info("Enabling MessageHistory tracking for component '" + componentName + "'");
                    }
                }
            }
            this.running = true;
        }
    }
}
Also used : TrackableComponent(org.springframework.integration.support.management.TrackableComponent) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) ManagedOperation(org.springframework.jmx.export.annotation.ManagedOperation)

Example 2 with TrackableComponent

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

the class MessageHistoryConfigurer method stop.

@ManagedOperation
@Override
public void stop() {
    synchronized (this.lifecycleMonitor) {
        if (this.running && this.beanFactory instanceof ListableBeanFactory) {
            for (TrackableComponent component : getTrackableComponents((ListableBeanFactory) this.beanFactory)) {
                String componentName = component.getComponentName();
                if (this.currentlyTrackedComponentNames.contains(componentName)) {
                    component.setShouldTrack(false);
                    if (this.logger.isInfoEnabled()) {
                        this.logger.info("Disabling MessageHistory tracking for component '" + componentName + "'");
                    }
                }
            }
            this.currentlyTrackedComponentNames.clear();
            this.running = false;
            // allow pattern changes
            this.componentNamePatternsExplicitlySet = false;
        }
    }
}
Also used : TrackableComponent(org.springframework.integration.support.management.TrackableComponent) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) ManagedOperation(org.springframework.jmx.export.annotation.ManagedOperation)

Example 3 with TrackableComponent

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

the class IntegrationMBeanExporter method enhanceSourceMonitor.

private MessageSourceMetrics enhanceSourceMonitor(MessageSourceMetrics monitor) {
    MessageSourceMetrics result = monitor;
    if (monitor.getManagedName() != 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);
        Object field = null;
        if (monitor instanceof MessagingGatewaySupport && endpoint == monitor) {
            field = monitor;
        } else {
            try {
                field = extractTarget(getField(endpoint, "source"));
            } catch (Exception e) {
                logger.trace("Could not get source from bean = " + beanName);
            }
        }
        if (field == monitor) {
            name = beanName;
            endpointName = beanName;
            break;
        }
    }
    if (endpointName == null) {
        endpoint = null;
    }
    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 outputChannel = null;
        if (target instanceof MessagingGatewaySupport) {
            outputChannel = ((MessagingGatewaySupport) target).getRequestChannel();
        } else {
            outputChannel = getField(target, "outputChannel");
        }
        if (outputChannel != null) {
            if (!this.anonymousSourceCounters.containsKey(outputChannel)) {
                this.anonymousSourceCounters.put(outputChannel, new AtomicLong());
            }
            AtomicLong count = this.anonymousSourceCounters.get(outputChannel);
            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 = outputChannel + suffix;
            source = "anonymous";
        }
    }
    if (endpoint instanceof Lifecycle) {
        // Wrap the monitor in a lifecycle so it exposes the start/stop operations
        if (endpoint instanceof TrackableComponent) {
            if (monitor instanceof MessageSourceManagement) {
                result = new LifecycleTrackableMessageSourceManagement((Lifecycle) endpoint, (MessageSourceManagement) monitor);
            } else {
                result = new LifecycleTrackableMessageSourceMetrics((Lifecycle) endpoint, monitor);
            }
        } else {
            if (monitor instanceof MessageSourceManagement) {
                result = new LifecycleMessageSourceManagement((Lifecycle) endpoint, (MessageSourceManagement) monitor);
            } else {
                result = new LifecycleMessageSourceMetrics((Lifecycle) endpoint, monitor);
            }
        }
    }
    if (name == null) {
        name = monitor.toString();
        source = "source";
    }
    if (endpointName != null) {
        this.beansByEndpointName.put(name, endpointName);
    }
    monitor.setManagedType(source);
    monitor.setManagedName(name);
    return result;
}
Also used : TargetSource(org.springframework.aop.TargetSource) MessageSourceMetrics(org.springframework.integration.support.management.MessageSourceMetrics) LifecycleTrackableMessageSourceMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageSourceMetrics) LifecycleMessageSourceMetrics(org.springframework.integration.support.management.LifecycleMessageSourceMetrics) Lifecycle(org.springframework.context.Lifecycle) MessagingGatewaySupport(org.springframework.integration.gateway.MessagingGatewaySupport) TrackableComponent(org.springframework.integration.support.management.TrackableComponent) LifecycleMessageSourceMetrics(org.springframework.integration.support.management.LifecycleMessageSourceMetrics) UnableToRegisterMBeanException(org.springframework.jmx.export.UnableToRegisterMBeanException) JMException(javax.management.JMException) BeansException(org.springframework.beans.BeansException) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageSourceManagement(org.springframework.integration.support.management.MessageSourceManagement) LifecycleMessageSourceManagement(org.springframework.integration.support.management.LifecycleMessageSourceManagement) LifecycleTrackableMessageSourceManagement(org.springframework.integration.support.management.LifecycleTrackableMessageSourceManagement) LifecycleTrackableMessageSourceManagement(org.springframework.integration.support.management.LifecycleTrackableMessageSourceManagement) Advised(org.springframework.aop.framework.Advised) LifecycleTrackableMessageSourceMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageSourceMetrics) LifecycleMessageSourceManagement(org.springframework.integration.support.management.LifecycleMessageSourceManagement)

Example 4 with TrackableComponent

use of org.springframework.integration.support.management.TrackableComponent 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

TrackableComponent (org.springframework.integration.support.management.TrackableComponent)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 JMException (javax.management.JMException)2 TargetSource (org.springframework.aop.TargetSource)2 Advised (org.springframework.aop.framework.Advised)2 BeansException (org.springframework.beans.BeansException)2 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)2 Lifecycle (org.springframework.context.Lifecycle)2 UnableToRegisterMBeanException (org.springframework.jmx.export.UnableToRegisterMBeanException)2 ManagedOperation (org.springframework.jmx.export.annotation.ManagedOperation)2 MessagingGatewaySupport (org.springframework.integration.gateway.MessagingGatewaySupport)1 NamedComponent (org.springframework.integration.support.context.NamedComponent)1 LifecycleMessageHandlerMetrics (org.springframework.integration.support.management.LifecycleMessageHandlerMetrics)1 LifecycleMessageSourceManagement (org.springframework.integration.support.management.LifecycleMessageSourceManagement)1 LifecycleMessageSourceMetrics (org.springframework.integration.support.management.LifecycleMessageSourceMetrics)1 LifecycleTrackableMessageHandlerMetrics (org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics)1 LifecycleTrackableMessageSourceManagement (org.springframework.integration.support.management.LifecycleTrackableMessageSourceManagement)1 LifecycleTrackableMessageSourceMetrics (org.springframework.integration.support.management.LifecycleTrackableMessageSourceMetrics)1 MappingMessageRouterManagement (org.springframework.integration.support.management.MappingMessageRouterManagement)1 MessageHandlerMetrics (org.springframework.integration.support.management.MessageHandlerMetrics)1