Search in sources :

Example 1 with MessageHandlerMetrics

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

the class IntegrationMBeanExporter method registerHandlers.

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;
        }
        String beanKey = getHandlerBeanKey(monitor);
        if (logger.isInfoEnabled()) {
            logger.info("Registering MessageHandler " + name);
        }
        registerBeanNameOrInstance(monitor, beanKey);
    }
}
Also used : LifecycleTrackableMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics) LifecycleMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleMessageHandlerMetrics) MessageHandlerMetrics(org.springframework.integration.support.management.MessageHandlerMetrics)

Example 2 with MessageHandlerMetrics

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

the class MBeanExporterParserTests method testMBeanExporterExists.

@Test
public void testMBeanExporterExists() {
    IntegrationMBeanExporter exporter = this.context.getBean(IntegrationMBeanExporter.class);
    MBeanServer server = this.context.getBean("mbs", MBeanServer.class);
    Properties properties = TestUtils.getPropertyValue(exporter, "objectNameStaticProperties", Properties.class);
    assertNotNull(properties);
    assertEquals(2, properties.size());
    assertTrue(properties.containsKey("foo"));
    assertTrue(properties.containsKey("bar"));
    assertEquals(server, exporter.getServer());
    assertSame(context.getBean("keyNamer"), TestUtils.getPropertyValue(exporter, "namingStrategy"));
    MessageChannelMetrics metrics = context.getBean("foo", MessageChannelMetrics.class);
    assertTrue(metrics.isCountsEnabled());
    assertFalse(metrics.isStatsEnabled());
    checkCustomized(metrics);
    MessageHandlerMetrics handlerMetrics = context.getBean("transformer.handler", MessageHandlerMetrics.class);
    checkCustomized(handlerMetrics);
    metrics = context.getBean("bar", MessageChannelMetrics.class);
    assertTrue(metrics.isCountsEnabled());
    assertFalse(metrics.isStatsEnabled());
    metrics = context.getBean("baz", MessageChannelMetrics.class);
    assertFalse(metrics.isCountsEnabled());
    assertFalse(metrics.isStatsEnabled());
    metrics = context.getBean("qux", MessageChannelMetrics.class);
    assertFalse(metrics.isCountsEnabled());
    assertFalse(metrics.isStatsEnabled());
    metrics = context.getBean("fiz", MessageChannelMetrics.class);
    assertTrue(metrics.isCountsEnabled());
    assertTrue(metrics.isStatsEnabled());
    metrics = context.getBean("buz", MessageChannelMetrics.class);
    assertTrue(metrics.isCountsEnabled());
    assertTrue(metrics.isStatsEnabled());
    metrics = context.getBean("!excluded", MessageChannelMetrics.class);
    assertFalse(metrics.isCountsEnabled());
    assertFalse(metrics.isStatsEnabled());
    checkCustomized(metrics);
    MetricsFactory factory = context.getBean(MetricsFactory.class);
    IntegrationManagementConfigurer configurer = context.getBean(IntegrationManagementConfigurer.class);
    assertSame(factory, TestUtils.getPropertyValue(configurer, "metricsFactory"));
    exporter.destroy();
}
Also used : MessageHandlerMetrics(org.springframework.integration.support.management.MessageHandlerMetrics) AbstractMessageHandlerMetrics(org.springframework.integration.support.management.AbstractMessageHandlerMetrics) DefaultMessageHandlerMetrics(org.springframework.integration.support.management.DefaultMessageHandlerMetrics) IntegrationManagementConfigurer(org.springframework.integration.config.IntegrationManagementConfigurer) IntegrationMBeanExporter(org.springframework.integration.monitor.IntegrationMBeanExporter) DefaultMessageChannelMetrics(org.springframework.integration.support.management.DefaultMessageChannelMetrics) AbstractMessageChannelMetrics(org.springframework.integration.support.management.AbstractMessageChannelMetrics) MessageChannelMetrics(org.springframework.integration.support.management.MessageChannelMetrics) MetricsFactory(org.springframework.integration.support.management.MetricsFactory) Properties(java.util.Properties) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 3 with MessageHandlerMetrics

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

the class ChannelIntegrationTests method testMessageChannelStatistics.

@Test
public void testMessageChannelStatistics() throws Exception {
    requests.send(new GenericMessage<String>("foo"));
    String intermediateChannelName = "" + intermediate;
    assertEquals(1, messageChannelsMonitor.getChannelSendCount(intermediateChannelName));
    double rate = messageChannelsMonitor.getChannelSendRate("" + requests).getMean();
    assertTrue("No statistics for requests channel", rate >= 0);
    rate = messageChannelsMonitor.getChannelSendRate(intermediateChannelName).getMean();
    assertTrue("No statistics for intermediate channel", rate >= 0);
    assertNotNull(intermediate.receive(100L));
    assertEquals(1, messageChannelsMonitor.getChannelReceiveCount(intermediateChannelName));
    requests.send(new GenericMessage<String>("foo"));
    try {
        requests.send(new GenericMessage<String>("foo"));
    } catch (MessageDeliveryException e) {
    }
    assertEquals(3, messageChannelsMonitor.getChannelSendCount(intermediateChannelName));
    assertEquals(1, messageChannelsMonitor.getChannelSendErrorCount(intermediateChannelName));
    assertSame(intermediate, messageChannelsMonitor.getChannelMetrics(intermediateChannelName));
    MessageHandlerMetrics handlerMetrics = messageChannelsMonitor.getHandlerMetrics("bridge");
    assertEquals(3, handlerMetrics.getHandleCount());
    assertEquals(1, handlerMetrics.getErrorCount());
    assertNotNull(this.sourceChannel.receive(10000));
    assertTrue(messageChannelsMonitor.getSourceMessageCount("source") > 0);
    assertTrue(messageChannelsMonitor.getSourceMetrics("source").getMessageCount() > 0);
}
Also used : MessageHandlerMetrics(org.springframework.integration.support.management.MessageHandlerMetrics) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 4 with MessageHandlerMetrics

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

Example 5 with MessageHandlerMetrics

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

the class IntegrationMBeanExporter method afterSingletonsInstantiated.

@Override
public void afterSingletonsInstantiated() {
    Map<String, MessageHandlerMetrics> messageHandlers = this.applicationContext.getBeansOfType(MessageHandlerMetrics.class);
    for (Entry<String, MessageHandlerMetrics> entry : messageHandlers.entrySet()) {
        String beanName = entry.getKey();
        MessageHandlerMetrics bean = entry.getValue();
        if (this.handlerInAnonymousWrapper(bean) != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Skipping " + beanName + " because it wraps another handler");
            }
            continue;
        }
        // If the handler is proxied, we have to extract the target to expose as an MBean.
        // The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
        MessageHandlerMetrics monitor = (MessageHandlerMetrics) extractTarget(bean);
        this.handlers.add(monitor);
    }
    Map<String, MessageSourceMetrics> messageSources = this.applicationContext.getBeansOfType(MessageSourceMetrics.class);
    for (Entry<String, MessageSourceMetrics> entry : messageSources.entrySet()) {
        // If the source is proxied, we have to extract the target to expose as an MBean.
        // The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
        MessageSourceMetrics monitor = (MessageSourceMetrics) extractTarget(entry.getValue());
        this.sources.add(monitor);
    }
    Map<String, MessageChannelMetrics> messageChannels = this.applicationContext.getBeansOfType(MessageChannelMetrics.class);
    for (Entry<String, MessageChannelMetrics> entry : messageChannels.entrySet()) {
        // If the channel is proxied, we have to extract the target to expose as an MBean.
        // The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
        MessageChannelMetrics monitor = (MessageChannelMetrics) extractTarget(entry.getValue());
        this.channels.add(monitor);
    }
    Map<String, MessageProducer> messageProducers = this.applicationContext.getBeansOfType(MessageProducer.class);
    for (Entry<String, MessageProducer> entry : messageProducers.entrySet()) {
        MessageProducer messageProducer = entry.getValue();
        if (messageProducer instanceof Lifecycle) {
            Lifecycle target = (Lifecycle) extractTarget(messageProducer);
            if (!(target instanceof AbstractMessageProducingHandler)) {
                this.inboundLifecycleMessageProducers.add(target);
            }
        }
    }
    super.afterSingletonsInstantiated();
    try {
        registerChannels();
        registerHandlers();
        registerSources();
        registerEndpoints();
        if (this.applicationContext.containsBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME)) {
            Object messageHistoryConfigurer = this.applicationContext.getBean(IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
            if (messageHistoryConfigurer instanceof MessageHistoryConfigurer) {
                registerBeanInstance(messageHistoryConfigurer, IntegrationContextUtils.INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME);
            }
        }
        if (!this.applicationContext.containsBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME)) {
            this.managementConfigurer = new IntegrationManagementConfigurer();
            this.managementConfigurer.setDefaultCountsEnabled(true);
            this.managementConfigurer.setDefaultStatsEnabled(true);
            this.managementConfigurer.setApplicationContext(this.applicationContext);
            this.managementConfigurer.setBeanName(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
            this.managementConfigurer.afterSingletonsInstantiated();
        } else {
            this.managementConfigurer = this.applicationContext.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME, IntegrationManagementConfigurer.class);
        }
    } catch (RuntimeException e) {
        unregisterBeans();
        throw e;
    }
}
Also used : IntegrationManagementConfigurer(org.springframework.integration.config.IntegrationManagementConfigurer) MessageChannelMetrics(org.springframework.integration.support.management.MessageChannelMetrics) MessageSourceMetrics(org.springframework.integration.support.management.MessageSourceMetrics) LifecycleTrackableMessageSourceMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageSourceMetrics) LifecycleMessageSourceMetrics(org.springframework.integration.support.management.LifecycleMessageSourceMetrics) MessageHistoryConfigurer(org.springframework.integration.history.MessageHistoryConfigurer) Lifecycle(org.springframework.context.Lifecycle) LifecycleTrackableMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics) LifecycleMessageHandlerMetrics(org.springframework.integration.support.management.LifecycleMessageHandlerMetrics) MessageHandlerMetrics(org.springframework.integration.support.management.MessageHandlerMetrics) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) MessageProducer(org.springframework.integration.core.MessageProducer)

Aggregations

MessageHandlerMetrics (org.springframework.integration.support.management.MessageHandlerMetrics)5 LifecycleMessageHandlerMetrics (org.springframework.integration.support.management.LifecycleMessageHandlerMetrics)3 LifecycleTrackableMessageHandlerMetrics (org.springframework.integration.support.management.LifecycleTrackableMessageHandlerMetrics)3 Test (org.junit.Test)2 Lifecycle (org.springframework.context.Lifecycle)2 IntegrationManagementConfigurer (org.springframework.integration.config.IntegrationManagementConfigurer)2 MessageChannelMetrics (org.springframework.integration.support.management.MessageChannelMetrics)2 Properties (java.util.Properties)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 JMException (javax.management.JMException)1 MBeanServer (javax.management.MBeanServer)1 TargetSource (org.springframework.aop.TargetSource)1 Advised (org.springframework.aop.framework.Advised)1 BeansException (org.springframework.beans.BeansException)1 MessageProducer (org.springframework.integration.core.MessageProducer)1 AbstractMessageProducingHandler (org.springframework.integration.handler.AbstractMessageProducingHandler)1 MessageHistoryConfigurer (org.springframework.integration.history.MessageHistoryConfigurer)1 IntegrationMBeanExporter (org.springframework.integration.monitor.IntegrationMBeanExporter)1 NamedComponent (org.springframework.integration.support.context.NamedComponent)1 AbstractMessageChannelMetrics (org.springframework.integration.support.management.AbstractMessageChannelMetrics)1