Skip to content

Commit

Permalink
INT-4181: Refactor IMBE to use IntMngmtConfigurer
Browse files Browse the repository at this point in the history
JIRA: https://jira.spring.io/browse/INT-4181

* To avoid duplicate code move actual `Metrics` resolution to the `IntegrationManagementConfigurer` and delegate from the `IntegrationMBeanExporter` for backward compatibility
* Minor refactoring and improvements in the `IntegrationMBeanExporter`
* Fix typos in the `jms/AsyncGatewayTests`

**Cherry-pick to 4.3.x**
  • Loading branch information
artembilan authored and garyrussell committed Dec 20, 2016
1 parent 16c7535 commit 37b9612
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 109 deletions.
Expand Up @@ -17,9 +17,13 @@
package org.springframework.integration.support.management;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.SmartInitializingSingleton;
Expand All @@ -35,14 +39,23 @@
* Configures counts, stats, logging for all (or selected) components.
*
* @author Gary Russell
* @author Artem Bilan
* @since 4.2
*
*/
public class IntegrationManagementConfigurer implements SmartInitializingSingleton, ApplicationContextAware,
BeanNameAware {

private static final Log logger = LogFactory.getLog(IntegrationManagementConfigurer.class);

public static final String MANAGEMENT_CONFIGURER_NAME = "integrationManagementConfigurer";

private final Map<String, MessageChannelMetrics> channelsByName = new HashMap<String, MessageChannelMetrics>();

private final Map<String, MessageHandlerMetrics> handlersByName = new HashMap<String, MessageHandlerMetrics>();

private final Map<String, MessageSourceMetrics> sourcesByName = new HashMap<String, MessageSourceMetrics>();

private ApplicationContext applicationContext;

private String beanName;
Expand All @@ -61,7 +74,6 @@ public class IntegrationManagementConfigurer implements SmartInitializingSinglet

private String[] enabledStatsPatterns = { };


@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
Expand Down Expand Up @@ -230,6 +242,7 @@ private void configureChannelMetrics(String name, MessageChannelMetrics bean) {
if (bean instanceof ConfigurableMetricsAware) {
((ConfigurableMetricsAware<AbstractMessageChannelMetrics>) bean).configureMetrics(metrics);
}
this.channelsByName.put(name, bean);
}

@SuppressWarnings("unchecked")
Expand All @@ -255,6 +268,8 @@ private void configureHandlerMetrics(String name, MessageHandlerMetrics bean) {
if (bean instanceof ConfigurableMetricsAware) {
((ConfigurableMetricsAware<AbstractMessageHandlerMetrics>) bean).configureMetrics(metrics);
}

this.handlersByName.put(bean.getManagedName() != null ? bean.getManagedName() : name, bean);
}

private void configureSourceMetrics(String name, MessageSourceMetrics bean) {
Expand All @@ -265,6 +280,7 @@ private void configureSourceMetrics(String name, MessageSourceMetrics bean) {
else {
bean.setCountsEnabled(this.defaultCountsEnabled);
}
this.sourcesByName.put(bean.getManagedName() != null ? bean.getManagedName() : name, bean);
}

/**
Expand Down Expand Up @@ -294,23 +310,52 @@ else if (pattern.startsWith("\\")) {
return null; //NOSONAR - intentional null return
}

public String[] getChannelNames() {
return this.channelsByName.keySet().toArray(new String[this.channelsByName.size()]);
}

public String[] getHandlerNames() {
return this.handlersByName.keySet().toArray(new String[this.handlersByName.size()]);
}

public String[] getSourceNames() {
return this.sourcesByName.keySet().toArray(new String[this.sourcesByName.size()]);
}

public MessageChannelMetrics getChannelMetrics(String name) {
if (this.applicationContext.containsBean(name)) {
return this.applicationContext.getBean(name, MessageChannelMetrics.class);
if (this.channelsByName.containsKey(name)) {
return this.channelsByName.get(name);
}
if (logger.isDebugEnabled()) {
logger.debug("No channel found for (" + name + ")");
}
return null;
}

public MessageHandlerMetrics getHandlerMetrics(String name) {
if (this.applicationContext.containsBean(name)) {
return this.applicationContext.getBean(name, MessageHandlerMetrics.class);
if (this.handlersByName.containsKey(name)) {
return this.handlersByName.get(name);
}
if (this.handlersByName.containsKey(name + ".handler")) {
return this.handlersByName.get(name + ".handler");
}

if (logger.isDebugEnabled()) {
logger.debug("No handler found for (" + name + ")");
}
return null;
}

public MessageSourceMetrics getSourceMetrics(String name) {
if (this.applicationContext.containsBean(name)) {
return this.applicationContext.getBean(name, MessageSourceMetrics.class);
if (this.sourcesByName.containsKey(name)) {
return this.sourcesByName.get(name);
}
if (this.sourcesByName.containsKey(name + ".source")) {
return this.sourcesByName.get(name + ".source");
}

if (logger.isDebugEnabled()) {
logger.debug("No source found for (" + name + ")");
}
return null;
}
Expand Down
Expand Up @@ -105,7 +105,7 @@ public void testWithTimeout() throws Exception {
assertThat(error.getPayload(), instanceOf(MessagingException.class));
assertThat(((MessagingException) error.getPayload()).getCause(), instanceOf(JmsTimeoutException.class));
assertEquals("foo", ((MessagingException) error.getPayload()).getFailedMessage().getPayload());
this.gateway1.stop();
this.gateway2.stop();
}

@Test
Expand All @@ -121,7 +121,7 @@ public void testWithTimeoutNoReplyRequired() throws Exception {
assertNotNull(received);
org.springframework.messaging.Message<?> error = errors.receive(1000);
assertNull(error);
this.gateway1.stop();
this.gateway2.stop();
}

@Configuration
Expand All @@ -131,7 +131,7 @@ public static class Config {
@Bean
public CachingConnectionFactory ccf() {
return new CachingConnectionFactory(
new ActiveMQConnectionFactory("vm://localhosti?broker.persistent=false"));
new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"));
}

@Bean
Expand Down

0 comments on commit 37b9612

Please sign in to comment.