INT-3826: Fix @EnableIntegrationManagement
JIRA: https://jira.spring.io/browse/INT-3826 Application Context would not initialize with annotation without attributes. Defaults were wrong; metrics factory bean name. Make `IntegrationManagementConfigurer` more robust for the external options
This commit is contained in:
committed by
Artem Bilan
parent
9505420ae8
commit
13b40376c0
@@ -55,7 +55,7 @@ public @interface EnableIntegrationManagement {
|
||||
* components. Overrides {@link #defaultCountsEnabled()} for matching bean names.
|
||||
* @return the patterns.
|
||||
*/
|
||||
String[] countsEnabled() default "";
|
||||
String[] countsEnabled() default "*";
|
||||
|
||||
/**
|
||||
* A list of simple patterns for component names for which message statistics will be
|
||||
@@ -74,7 +74,7 @@ public @interface EnableIntegrationManagement {
|
||||
* components.
|
||||
* @return the patterns.
|
||||
*/
|
||||
String[] statsEnabled() default "";
|
||||
String[] statsEnabled() default "*";
|
||||
|
||||
/**
|
||||
* The default setting for enabling counts when a bean name is not matched by
|
||||
|
||||
@@ -93,14 +93,14 @@ public class IntegrationManagementConfiguration implements ImportAware, Environm
|
||||
configurer.setEnabledCountsPatterns(patterns.toArray(new String[patterns.size()]));
|
||||
}
|
||||
|
||||
private void setupStatsEnabledNamePatterns(IntegrationManagementConfigurer exporter) {
|
||||
private void setupStatsEnabledNamePatterns(IntegrationManagementConfigurer configurer) {
|
||||
List<String> patterns = new ArrayList<String>();
|
||||
String[] statsEnabled = this.attributes.getStringArray("statsEnabled");
|
||||
for (String managedComponent : statsEnabled) {
|
||||
String pattern = this.environment.resolvePlaceholders(managedComponent);
|
||||
patterns.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(pattern)));
|
||||
}
|
||||
exporter.setEnabledStatsPatterns(patterns.toArray(new String[patterns.size()]));
|
||||
configurer.setEnabledStatsPatterns(patterns.toArray(new String[patterns.size()]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.support.management;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -26,6 +27,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.PatternMatchUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
@@ -51,7 +53,7 @@ public class IntegrationManagementConfigurer implements SmartInitializingSinglet
|
||||
|
||||
private Boolean defaultStatsEnabled = false;
|
||||
|
||||
private MetricsFactory metricsFactory = new DefaultMetricsFactory();
|
||||
private MetricsFactory metricsFactory;
|
||||
|
||||
private String metricsFactoryBeanName;
|
||||
|
||||
@@ -72,6 +74,8 @@ public class IntegrationManagementConfigurer implements SmartInitializingSinglet
|
||||
|
||||
/**
|
||||
* Set a metrics factory.
|
||||
* Has a precedence over {@link #metricsFactoryBeanName}.
|
||||
* Defaults to {@link DefaultMetricsFactory}.
|
||||
* @param metricsFactory the factory.
|
||||
* @since 4.2
|
||||
*/
|
||||
@@ -79,6 +83,12 @@ public class IntegrationManagementConfigurer implements SmartInitializingSinglet
|
||||
this.metricsFactory = metricsFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a metrics factory bean name.
|
||||
* Is used if {@link #metricsFactory} isn't specified.
|
||||
* @param metricsFactory the factory.
|
||||
* @since 4.2
|
||||
*/
|
||||
public void setMetricsFactoryBeanName(String metricsFactory) {
|
||||
this.metricsFactoryBeanName = metricsFactory;
|
||||
}
|
||||
@@ -117,7 +127,7 @@ public class IntegrationManagementConfigurer implements SmartInitializingSinglet
|
||||
* @param enabledStatsPatterns the patterns.
|
||||
*/
|
||||
public void setEnabledStatsPatterns(String[] enabledStatsPatterns) {
|
||||
Assert.notEmpty(enabledStatsPatterns, "componentNamePatterns must not be empty");
|
||||
Assert.notEmpty(enabledStatsPatterns, "enabledStatsPatterns must not be empty");
|
||||
this.enabledStatsPatterns = Arrays.copyOf(enabledStatsPatterns, enabledStatsPatterns.length);
|
||||
}
|
||||
|
||||
@@ -175,9 +185,12 @@ public class IntegrationManagementConfigurer implements SmartInitializingSinglet
|
||||
Assert.state(this.applicationContext != null, "'applicationContext' must not be null");
|
||||
Assert.state(MANAGEMENT_CONFIGURER_NAME.equals(this.beanName), getClass().getSimpleName()
|
||||
+ " bean name must be " + MANAGEMENT_CONFIGURER_NAME);
|
||||
if (this.metricsFactoryBeanName != null) {
|
||||
if (this.metricsFactory == null && StringUtils.hasText(this.metricsFactoryBeanName)) {
|
||||
this.metricsFactory = this.applicationContext.getBean(this.metricsFactoryBeanName, MetricsFactory.class);
|
||||
}
|
||||
if (this.metricsFactory == null) {
|
||||
this.metricsFactory = new DefaultMetricsFactory();
|
||||
}
|
||||
Map<String, IntegrationManagement> managed = this.applicationContext.getBeansOfType(IntegrationManagement.class);
|
||||
for (Entry<String, IntegrationManagement> entry : managed.entrySet()) {
|
||||
IntegrationManagement bean = entry.getValue();
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package org.springframework.integration.support.management;
|
||||
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -26,10 +28,18 @@ import java.util.Map;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.channel.AbstractMessageChannel;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.config.EnableIntegration;
|
||||
import org.springframework.integration.config.EnableIntegrationManagement;
|
||||
import org.springframework.integration.endpoint.AbstractMessageSource;
|
||||
import org.springframework.integration.handler.AbstractMessageHandler;
|
||||
import org.springframework.integration.router.RecipientListRouter;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
@@ -73,4 +83,28 @@ public class IntegrationManagementConfigurerTests {
|
||||
assertFalse(source.isLoggingEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyAnnotation() {
|
||||
AnnotationConfigApplicationContext ctx =
|
||||
new AnnotationConfigApplicationContext(ConfigEmptyAnnotation.class);
|
||||
AbstractMessageChannel channel = ctx.getBean("channel", AbstractMessageChannel.class);
|
||||
assertTrue(channel.isCountsEnabled());
|
||||
assertTrue(channel.isStatsEnabled());
|
||||
assertThat(TestUtils.getPropertyValue(channel, "channelMetrics"),
|
||||
instanceOf(DefaultMessageChannelMetrics.class));
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableIntegration
|
||||
@EnableIntegrationManagement
|
||||
public static class ConfigEmptyAnnotation {
|
||||
|
||||
@Bean
|
||||
public MessageChannel channel() {
|
||||
return new DirectChannel();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user