diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java b/spring-integration-core/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java index a19abfaefa..624cfa16fd 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/SourcePollingChannelAdapterFactoryBean.java @@ -145,7 +145,7 @@ public class SourcePollingChannelAdapterFactoryBean implements FactoryBean) bean.getClass(), () -> bean) .getRawBeanDefinition(); - ((BeanDefinitionRegistry) this.beanFactory).registerBeanDefinition(beanName, beanDefinition); + this.beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition); if (parentName != null) { this.beanFactory.registerDependentBean(parentName, beanName); diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcJmxManagedBeanTests-context.xml b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcJmxManagedBeanTests-context.xml index 80bbdff037..82fd154bf3 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcJmxManagedBeanTests-context.xml +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcJmxManagedBeanTests-context.xml @@ -9,8 +9,9 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - - + + + diff --git a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java index 10b4e076d3..90036a2616 100644 --- a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java +++ b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java @@ -40,6 +40,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.aop.TargetSource; import org.springframework.aop.framework.Advised; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.EmbeddedValueResolverAware; @@ -49,7 +50,10 @@ import org.springframework.integration.config.IntegrationManagementConfigurer; import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.context.OrderlyShutdownCapable; import org.springframework.integration.core.MessageProducer; +import org.springframework.integration.core.MessageSource; import org.springframework.integration.endpoint.AbstractEndpoint; +import org.springframework.integration.endpoint.IntegrationConsumer; +import org.springframework.integration.endpoint.SourcePollingChannelAdapter; import org.springframework.integration.gateway.MessagingGatewaySupport; import org.springframework.integration.handler.AbstractMessageProducingHandler; import org.springframework.integration.history.MessageHistoryConfigurer; @@ -114,7 +118,7 @@ import org.springframework.util.StringValueResolver; */ @org.springframework.jmx.export.annotation.ManagedResource public class IntegrationMBeanExporter extends MBeanExporter implements ApplicationContextAware, - EmbeddedValueResolverAware { + EmbeddedValueResolverAware, DestructionAwareBeanPostProcessor { private static final Log logger = LogFactory.getLog(IntegrationMBeanExporter.class); @@ -138,27 +142,32 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati private final Map allChannelsByName = new HashMap<>(); - private final Map allHandlersByName = new HashMap<>(); - private final Map allSourcesByName = new HashMap<>(); - private final Map beansByEndpointName = new HashMap<>(); + private final Map endpointsByMonitor = new HashMap<>(); - private String domain = DEFAULT_DOMAIN; + private final Map objectNames = new HashMap<>(); + + private final Set endpointNames = new HashSet<>(); + + private final AtomicBoolean shuttingDown = new AtomicBoolean(); private final Properties objectNameStaticProperties = new Properties(); + private final Set runtimeBeans = new HashSet<>(); + private final MetadataNamingStrategy defaultNamingStrategy = new IntegrationMetadataNamingStrategy(this.attributeSource); + private String domain = DEFAULT_DOMAIN; + private String[] componentNamePatterns = { "*" }; private IntegrationManagementConfigurer managementConfigurer; private volatile long shutdownDeadline; - private final AtomicBoolean shuttingDown = new AtomicBoolean(); - + private volatile boolean singletonsInstantiated; public IntegrationMBeanExporter() { super(); @@ -257,10 +266,7 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati for (Entry entry : messageProducers.entrySet()) { MessageProducer messageProducer = entry.getValue(); if (messageProducer instanceof Lifecycle) { - Lifecycle target = (Lifecycle) extractTarget(messageProducer); - if (!(target instanceof AbstractMessageProducingHandler)) { - this.inboundLifecycleMessageProducers.add(target); - } + registerProducer(messageProducer); } } super.afterSingletonsInstantiated(); @@ -292,12 +298,114 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati this.applicationContext.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME, IntegrationManagementConfigurer.class); } + + this.singletonsInstantiated = true; } catch (RuntimeException e) { unregisterBeans(); throw e; } + } + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (this.singletonsInstantiated) { + try { + if (bean instanceof MessageChannelMetrics) { + MessageChannelMetrics monitor = (MessageChannelMetrics) extractTarget(bean); + this.channels.add(monitor); + registerChannel(monitor); + this.runtimeBeans.add(bean); + } + else if (bean instanceof MessageProducer && bean instanceof Lifecycle) { + registerProducer((MessageProducer) bean); + this.runtimeBeans.add(bean); + } + else if (bean instanceof AbstractEndpoint) { + if (bean instanceof IntegrationConsumer) { + IntegrationConsumer integrationConsumer = (IntegrationConsumer) bean; + MessageHandler handler = integrationConsumer.getHandler(); + if (handler instanceof MessageHandlerMetrics) { + MessageHandlerMetrics messageHandlerMetrics = + (MessageHandlerMetrics) extractTarget(handler); + registerHandler(messageHandlerMetrics); + this.handlers.add(messageHandlerMetrics); + this.runtimeBeans.add(messageHandlerMetrics); + return bean; + } + } + else if (bean instanceof SourcePollingChannelAdapter) { + SourcePollingChannelAdapter pollingChannelAdapter = (SourcePollingChannelAdapter) bean; + MessageSource messageSource = pollingChannelAdapter.getMessageSource(); + if (messageSource instanceof MessageSourceMetrics) { + MessageSourceMetrics messageSourceMetrics = + (MessageSourceMetrics) extractTarget(messageSource); + registerSource(messageSourceMetrics); + this.sources.add(messageSourceMetrics); + this.runtimeBeans.add(messageSourceMetrics); + return bean; + } + } + + registerEndpoint((AbstractEndpoint) bean); + this.runtimeBeans.add(bean); + } + } + catch (Exception e) { + logger.error("Could not register an MBean for: " + beanName, e); + } + } + return bean; + } + + private void registerProducer(MessageProducer messageProducer) { + Lifecycle target = (Lifecycle) extractTarget(messageProducer); + if (!(target instanceof AbstractMessageProducingHandler)) { + this.inboundLifecycleMessageProducers.add(target); + } + } + + @Override + public boolean requiresDestruction(Object bean) { + return bean instanceof MessageChannelMetrics || + bean instanceof MessageHandlerMetrics || + bean instanceof MessageSourceMetrics || + (bean instanceof MessageProducer && bean instanceof Lifecycle) || + bean instanceof AbstractEndpoint; + } + + @Override + public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException { + if (this.runtimeBeans.remove(bean)) { + ObjectName objectName = this.objectNames.remove(bean); + if (objectName != null) { + doUnregister(objectName); + if (bean instanceof AbstractEndpoint) { + this.endpointNames.remove(((AbstractEndpoint) bean).getComponentName()); + } + else { + + this.endpointsByMonitor.remove(bean); + if (bean instanceof MessageChannelMetrics) { + this.channels.remove(bean); + this.allChannelsByName.remove(((NamedComponent) bean).getComponentName()); + } + else if (bean instanceof MessageHandlerMetrics) { + this.handlers.remove(bean); + this.endpointNames.remove(((NamedComponent) bean).getComponentName()); + } + else if (bean instanceof MessageSourceMetrics) { + this.sources.remove(bean); + this.endpointNames.remove(((NamedComponent) bean).getComponentName()); + String managedName = ((MessageSourceMetrics) bean).getManagedName(); + this.allSourcesByName.remove(managedName); + } + } + } + else if (bean instanceof MessageProducer && bean instanceof Lifecycle) { + this.inboundLifecycleMessageProducers.remove(bean); + } + } } private MessageHandler handlerInAnonymousWrapper(final Object bean) { @@ -629,89 +737,94 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati } private void registerChannels() { - for (MessageChannelMetrics monitor : this.channels) { - String name = ((NamedComponent) monitor).getComponentName(); - this.allChannelsByName.put(name, monitor); - if (!matches(this.componentNamePatterns, name)) { - continue; - } + this.channels.forEach(this::registerChannel); + } + private void registerChannel(MessageChannelMetrics monitor) { + String name = ((NamedComponent) monitor).getComponentName(); + this.allChannelsByName.put(name, monitor); + if (matches(this.componentNamePatterns, name)) { String beanKey = getChannelBeanKey(name); if (logger.isInfoEnabled()) { logger.info("Registering MessageChannel " + name); } - registerBeanNameOrInstance(monitor, beanKey); + ObjectName objectName = registerBeanNameOrInstance(monitor, beanKey); + this.objectNames.put(monitor, objectName); } } 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; - } + this.handlers.forEach(this::registerHandler); + } + + private void registerHandler(MessageHandlerMetrics handler) { + MessageHandlerMetrics monitor = enhanceHandlerMonitor(handler); + String name = monitor.getManagedName(); + if (matches(this.componentNamePatterns, name)) { String beanKey = getHandlerBeanKey(monitor); if (logger.isInfoEnabled()) { logger.info("Registering MessageHandler " + name); } - registerBeanNameOrInstance(monitor, beanKey); + ObjectName objectName = registerBeanNameOrInstance(monitor, beanKey); + this.objectNames.put(handler, objectName); } } private void registerSources() { - for (MessageSourceMetrics source : this.sources) { - MessageSourceMetrics monitor = enhanceSourceMonitor(source); - String name = monitor.getManagedName(); - this.allSourcesByName.put(name, monitor); - if (!matches(this.componentNamePatterns, name)) { - continue; - } + this.sources.forEach(this::registerSource); + } + private void registerSource(MessageSourceMetrics source) { + MessageSourceMetrics monitor = enhanceSourceMonitor(source); + String name = monitor.getManagedName(); + this.allSourcesByName.put(name, monitor); + if (matches(this.componentNamePatterns, name)) { String beanKey = getSourceBeanKey(monitor); if (logger.isInfoEnabled()) { logger.info("Registering MessageSource " + name); } - registerBeanNameOrInstance(monitor, beanKey); + ObjectName objectName = registerBeanNameOrInstance(monitor, beanKey); + this.objectNames.put(source, objectName); } } private void registerEndpoints() { String[] names = this.applicationContext.getBeanNamesForType(AbstractEndpoint.class); - Set endpointNames = new HashSet<>(); for (String name : names) { - if (!this.beansByEndpointName.values().contains(name)) { + if (!this.endpointsByMonitor.values().contains(name)) { AbstractEndpoint endpoint = this.applicationContext.getBean(name, AbstractEndpoint.class); - String beanKey; - name = endpoint.getComponentName(); - String source; - if (name.startsWith("_org.springframework.integration")) { - name = getInternalComponentName(name); - source = "internal"; - } - else { - name = endpoint.getComponentName(); - source = "endpoint"; - } - if (!matches(this.componentNamePatterns, name)) { - continue; - } - if (endpointNames.contains(name)) { - int count = 0; - String unique = name + "#" + count; - while (endpointNames.contains(unique)) { - unique = name + "#" + (++count); - } - name = unique; - } - endpointNames.add(name); - beanKey = getEndpointBeanKey(endpoint, name, source); - ObjectName objectName = registerBeanInstance(new ManagedEndpoint(endpoint), beanKey); - if (logger.isInfoEnabled()) { - logger.info("Registered endpoint without MessageSource: " + objectName); + registerEndpoint(endpoint); + } + } + } + + private void registerEndpoint(AbstractEndpoint endpoint) { + String beanKey; + String name = endpoint.getComponentName(); + String source; + if (name.startsWith("_org.springframework.integration")) { + name = getInternalComponentName(name); + source = "internal"; + } + else { + source = "endpoint"; + } + if (matches(this.componentNamePatterns, name)) { + if (this.endpointNames.contains(name)) { + int count = 0; + String unique = name + "#" + count; + while (this.endpointNames.contains(unique)) { + unique = name + "#" + (++count); } + name = unique; + } + this.endpointNames.add(name); + beanKey = getEndpointBeanKey(endpoint, name, source); + ObjectName objectName = registerBeanInstance(new ManagedEndpoint(endpoint), beanKey); + this.objectNames.put(endpoint, objectName); + if (logger.isInfoEnabled()) { + logger.info("Registered endpoint without MessageSource: " + objectName); } } } @@ -806,19 +919,19 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati } // Assignment algorithm and bean id, with bean id pulled reflectively out of enclosing endpoint if possible - String[] names = this.applicationContext.getBeanNamesForType(AbstractEndpoint.class); + String[] names = this.applicationContext.getBeanNamesForType(IntegrationConsumer.class); String name = null; String endpointName = null; String source = "endpoint"; - Object endpoint = null; + IntegrationConsumer endpoint = null; for (String beanName : names) { - endpoint = this.applicationContext.getBean(beanName); + endpoint = this.applicationContext.getBean(beanName, IntegrationConsumer.class); try { - Object field = extractTarget(getField(endpoint, "handler")); - if (field == monitor || - this.extractTarget(this.handlerInAnonymousWrapper(field)) == monitor) { + MessageHandler handler = endpoint.getHandler(); + if (handler == monitor || + extractTarget(handlerInAnonymousWrapper(handler)) == monitor) { name = beanName; endpointName = beanName; break; @@ -826,6 +939,7 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati } catch (Exception e) { logger.trace("Could not get handler from bean = " + beanName); + endpoint = null; } } if (name != null && endpoint != null && name.startsWith("_org.springframework.integration")) { @@ -833,33 +947,22 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati 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); - } + MessageChannel inputChannel = endpoint.getInputChannel(); + if (inputChannel != null) { + if (!this.anonymousHandlerCounters.containsKey(inputChannel)) { + this.anonymousHandlerCounters.put(inputChannel, new AtomicLong()); } - } - 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); + AtomicLong count = this.anonymousHandlerCounters.get(inputChannel); 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 + * 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; + name = inputChannel + suffix; source = "anonymous"; } } @@ -868,7 +971,8 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati // 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); + result = new TrackableRouterMetrics((Lifecycle) endpoint, + (MappingMessageRouterManagement) monitor); } else { result = new RouterMetrics((Lifecycle) endpoint, (MappingMessageRouterManagement) monitor); @@ -895,11 +999,11 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati } if (endpointName != null) { - this.beansByEndpointName.put(name, endpointName); + this.endpointsByMonitor.put(monitor, endpointName); } - monitor.setManagedType(source); - monitor.setManagedName(name); + result.setManagedType(source); + result.setManagedName(name); return result; @@ -910,7 +1014,6 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati } private MessageSourceMetrics enhanceSourceMonitor(MessageSourceMetrics monitor) { - MessageSourceMetrics result = monitor; if (monitor.getManagedName() != null) { @@ -937,6 +1040,7 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati } catch (Exception e) { logger.trace("Could not get source from bean = " + beanName); + endpoint = null; } } @@ -972,7 +1076,7 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati outputChannel = ((MessagingGatewaySupport) target).getRequestChannel(); } else { - outputChannel = getField(target, "outputChannel"); + outputChannel = ((SourcePollingChannelAdapter) target).getOutputChannel(); } if (outputChannel != null) { @@ -983,7 +1087,8 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati 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 + * Short hack to makes sure object names are unique if more than one endpoint has the same input + * channel */ if (total > 1) { suffix = "#" + total; @@ -1021,7 +1126,7 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati } if (endpointName != null) { - this.beansByEndpointName.put(name, endpointName); + this.endpointsByMonitor.put(monitor, endpointName); } monitor.setManagedType(source); diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/AttributePollingMessageSourceTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/AttributePollingMessageSourceTests.java index ef16955fc1..05635d7836 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/AttributePollingMessageSourceTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/AttributePollingMessageSourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,52 +23,47 @@ import java.util.concurrent.atomic.AtomicInteger; import javax.management.MBeanServer; -import org.junit.Before; import org.junit.Test; -import org.springframework.messaging.Message; import org.springframework.jmx.support.MBeanServerFactoryBean; import org.springframework.jmx.support.ObjectNameManager; +import org.springframework.messaging.Message; /** * @author Mark Fisher + * @author Artem Bilan + * * @since 2.0 */ public class AttributePollingMessageSourceTests { - private final TestCounter counter = new TestCounter(); - - private volatile MBeanServer server; - - - @Before - public void setup() throws Exception { - MBeanServerFactoryBean factoryBean = new MBeanServerFactoryBean(); - factoryBean.setLocateExistingServerIfPossible(true); - factoryBean.afterPropertiesSet(); - this.server = factoryBean.getObject(); - this.server.registerMBean(this.counter, ObjectNameManager.getInstance("test:name=counter")); - } - - @Test - public void basicPolling() { + public void basicPolling() throws Exception { + MBeanServerFactoryBean factoryBean = new MBeanServerFactoryBean(); + factoryBean.afterPropertiesSet(); + MBeanServer server = factoryBean.getObject(); + TestCounter counter = new TestCounter(); + server.registerMBean(counter, ObjectNameManager.getInstance("test:name=counter")); AttributePollingMessageSource source = new AttributePollingMessageSource(); source.setAttributeName("Count"); source.setObjectName("test:name=counter"); - source.setServer(this.server); + source.setServer(server); Message message1 = source.receive(); assertNotNull(message1); assertEquals(0, message1.getPayload()); - this.counter.increment(); + counter.increment(); Message message2 = source.receive(); assertNotNull(message2); assertEquals(1, message2.getPayload()); + + factoryBean.destroy(); } public interface TestCounterMBean { + int getCount(); + } @@ -83,6 +78,7 @@ public class AttributePollingMessageSourceTests { public void increment() { this.counter.incrementAndGet(); } + } } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/MBeanAttributeFilterTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/MBeanAttributeFilterTests-context.xml index ace1583f23..892f88d349 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/MBeanAttributeFilterTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/MBeanAttributeFilterTests-context.xml @@ -1,17 +1,17 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/MBeanTreePollingMessageSourceTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/MBeanTreePollingMessageSourceTests.java index 2a8030eae3..f0bf45cd49 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/MBeanTreePollingMessageSourceTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/MBeanTreePollingMessageSourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2016 the original author or authors. + * Copyright 2013-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,30 +25,38 @@ import java.util.Map; import javax.management.MBeanServer; -import org.junit.Before; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.springframework.jmx.support.MBeanServerFactoryBean; /** * @author Stuart Williams + * @author Artem Bilan * */ public class MBeanTreePollingMessageSourceTests { - private MBeanServer server; + private static MBeanServerFactoryBean factoryBean; - @Before - public void setup() { - MBeanServerFactoryBean factoryBean = new MBeanServerFactoryBean(); + private static MBeanServer server; + + @BeforeClass + public static void setup() { + factoryBean = new MBeanServerFactoryBean(); factoryBean.setLocateExistingServerIfPossible(true); factoryBean.afterPropertiesSet(); - this.server = factoryBean.getObject(); + server = factoryBean.getObject(); + } + + @AfterClass + public static void tearDown() { + factoryBean.destroy(); } @Test public void testDefaultPoll() { - MBeanObjectConverter converter = new DefaultMBeanObjectConverter(); MBeanTreePollingMessageSource source = new MBeanTreePollingMessageSource(converter); source.setServer(server); diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/NotificationListeningMessageProducerTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/NotificationListeningMessageProducerTests.java index 63b923574e..ec0eea889d 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/NotificationListeningMessageProducerTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/NotificationListeningMessageProducerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,9 @@ import javax.management.Notification; import javax.management.ObjectName; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; @@ -47,25 +49,31 @@ import org.springframework.messaging.Message; /** * @author Mark Fisher * @author Artem Bilan + * * @since 2.0 */ public class NotificationListeningMessageProducerTests { - private volatile MBeanServer server; - - private volatile ObjectName objectName; + private static MBeanServerFactoryBean serverFactoryBean; private final NumberHolder numberHolder = new NumberHolder(); + private MBeanServer server; + + private ObjectName objectName; + + + @BeforeClass + public static void setupClass() { + serverFactoryBean = new MBeanServerFactoryBean(); + serverFactoryBean.afterPropertiesSet(); + } @Before public void setup() throws Exception { - MBeanServerFactoryBean serverFactoryBean = new MBeanServerFactoryBean(); - serverFactoryBean.setLocateExistingServerIfPossible(true); - serverFactoryBean.afterPropertiesSet(); this.server = serverFactoryBean.getObject(); MBeanExporter exporter = new MBeanExporter(); - exporter.setAutodetect(false); + exporter.setServer(this.server); exporter.afterPropertiesSet(); this.objectName = ObjectNameManager.getInstance("si:name=numberHolder"); exporter.registerManagedResource(this.numberHolder, this.objectName); @@ -76,8 +84,13 @@ public class NotificationListeningMessageProducerTests { this.server.unregisterMBean(this.objectName); } + @AfterClass + public static void tearDown() { + serverFactoryBean.destroy(); + } + @Test - public void simpleNotification() throws Exception { + public void simpleNotification() { QueueChannel outputChannel = new QueueChannel(); NotificationListeningMessageProducer adapter = new NotificationListeningMessageProducer(); adapter.setServer(this.server); @@ -98,7 +111,7 @@ public class NotificationListeningMessageProducerTests { } @Test - public void notificationWithHandback() throws Exception { + public void notificationWithHandback() { QueueChannel outputChannel = new QueueChannel(); NotificationListeningMessageProducer adapter = new NotificationListeningMessageProducer(); adapter.setServer(this.server); @@ -122,7 +135,7 @@ public class NotificationListeningMessageProducerTests { @Test @SuppressWarnings("serial") - public void notificationWithFilter() throws Exception { + public void notificationWithFilter() { QueueChannel outputChannel = new QueueChannel(); NotificationListeningMessageProducer adapter = new NotificationListeningMessageProducer(); adapter.setServer(this.server); @@ -170,6 +183,7 @@ public class NotificationListeningMessageProducerTests { Notification notification = new Notification("testType", this, sequence.getAndIncrement(), message); this.notificationPublisher.sendNotification(notification); } + } } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/NotificationPublishingMessageHandlerTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/NotificationPublishingMessageHandlerTests.java index 2b79d9eea4..7c00277897 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/NotificationPublishingMessageHandlerTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/NotificationPublishingMessageHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,16 +29,21 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.support.StaticApplicationContext; -import org.springframework.messaging.MessageHandler; -import org.springframework.messaging.support.GenericMessage; import org.springframework.integration.monitor.IntegrationMBeanExporter; import org.springframework.jmx.export.MBeanExporter; +import org.springframework.jmx.support.MBeanServerFactoryBean; import org.springframework.jmx.support.ObjectNameManager; +import org.springframework.messaging.MessageHandler; +import org.springframework.messaging.support.GenericMessage; /** * @author Mark Fisher + * @author Artem Bilan + * * @since 2.0 */ public class NotificationPublishingMessageHandlerTests { @@ -49,14 +54,14 @@ public class NotificationPublishingMessageHandlerTests { private volatile ObjectName publisherObjectName; - @Before public void setup() throws Exception { this.publisherObjectName = ObjectNameManager.getInstance("test:type=publisher"); - // deliberately registering two exporters (one SI specific and one generic) - // should not fail INT-1816 - context.registerSingleton("exporter", IntegrationMBeanExporter.class); - context.registerSingleton("anotherExporter", MBeanExporter.class); + this.context.registerBean("mbeanServer", MBeanServerFactoryBean.class, MBeanServerFactoryBean::new); + this.context.registerSingleton("exporter", IntegrationMBeanExporter.class, + new MutablePropertyValues() + .add("server", new RuntimeBeanReference("mbeanServer"))); + this.context.registerSingleton("anotherExporter", MBeanExporter.class); RootBeanDefinition publisherDefinition = new RootBeanDefinition(NotificationPublishingMessageHandler.class); publisherDefinition.getConstructorArgumentValues().addGenericArgumentValue(this.publisherObjectName); @@ -88,7 +93,7 @@ public class NotificationPublishingMessageHandlerTests { public static class TestNotificationListener implements NotificationListener { - private final List notifications = new ArrayList(); + private final List notifications = new ArrayList<>(); public void handleNotification(Notification notification, Object handback) { this.notifications.add(notification); @@ -97,6 +102,7 @@ public class NotificationPublishingMessageHandlerTests { void clearNotifications() { this.notifications.clear(); } + } } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/OperationInvokingMessageHandlerTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/OperationInvokingMessageHandlerTests.java index 1fb65eb242..7060d7c3b8 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/OperationInvokingMessageHandlerTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/OperationInvokingMessageHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,9 @@ import java.util.Map; import javax.management.MBeanServer; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; @@ -42,45 +44,58 @@ import org.springframework.messaging.MessagingException; /** * See DynamicRouterTests for additional tests where the MBean is registered by the Spring exporter. - * @see DynamicRouterTests + * * @author Mark Fisher * @author Oleg Zhurakousky * @author Gary Russell + * @author Artem Bilan + * * @since 2.0 + * + * @see DynamicRouterTests */ public class OperationInvokingMessageHandlerTests { + private static MBeanServerFactoryBean factoryBean; + + private static MBeanServer server; + private final String objectName = "si:name=test"; - private volatile MBeanServer server; + @BeforeClass + public static void setupClass() { + factoryBean = new MBeanServerFactoryBean(); + factoryBean.afterPropertiesSet(); + server = factoryBean.getObject(); + } + @AfterClass + public static void tearDown() { + factoryBean.destroy(); + } @Before public void setup() throws Exception { - MBeanServerFactoryBean factoryBean = new MBeanServerFactoryBean(); - factoryBean.setLocateExistingServerIfPossible(true); - factoryBean.afterPropertiesSet(); - this.server = factoryBean.getObject(); - this.server.registerMBean(new TestOps(), ObjectNameManager.getInstance(this.objectName)); + server.registerMBean(new TestOps(), ObjectNameManager.getInstance(this.objectName)); } @After public void cleanup() throws Exception { - this.server.unregisterMBean(ObjectNameManager.getInstance(this.objectName)); + server.unregisterMBean(ObjectNameManager.getInstance(this.objectName)); } @Test - public void invocationWithMapPayload() throws Exception { + public void invocationWithMapPayload() { QueueChannel outputChannel = new QueueChannel(); OperationInvokingMessageHandler handler = new OperationInvokingMessageHandler(); - handler.setServer(this.server); + handler.setServer(server); handler.setObjectName(this.objectName); handler.setOutputChannel(outputChannel); handler.setOperationName("x"); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); - Map params = new HashMap(); + Map params = new HashMap<>(); params.put("p1", "foo"); params.put("p2", "bar"); Message message = MessageBuilder.withPayload(params).build(); @@ -91,10 +106,10 @@ public class OperationInvokingMessageHandlerTests { } @Test - public void invocationWithPayloadNoReturnValue() throws Exception { + public void invocationWithPayloadNoReturnValue() { QueueChannel outputChannel = new QueueChannel(); OperationInvokingMessageHandler handler = new OperationInvokingMessageHandler(); - handler.setServer(this.server); + handler.setServer(server); handler.setObjectName(this.objectName); handler.setOutputChannel(outputChannel); handler.setOperationName("y"); @@ -105,16 +120,16 @@ public class OperationInvokingMessageHandlerTests { } @Test(expected = MessagingException.class) - public void invocationWithMapPayloadNotEnoughParameters() throws Exception { + public void invocationWithMapPayloadNotEnoughParameters() { QueueChannel outputChannel = new QueueChannel(); OperationInvokingMessageHandler handler = new OperationInvokingMessageHandler(); - handler.setServer(this.server); + handler.setServer(server); handler.setObjectName(this.objectName); handler.setOutputChannel(outputChannel); handler.setOperationName("x"); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); - Map params = new HashMap(); + Map params = new HashMap<>(); params.put("p1", "foo"); Message message = MessageBuilder.withPayload(params).build(); handler.handleMessage(message); @@ -124,16 +139,16 @@ public class OperationInvokingMessageHandlerTests { } @Test - public void invocationWithListPayload() throws Exception { + public void invocationWithListPayload() { QueueChannel outputChannel = new QueueChannel(); OperationInvokingMessageHandler handler = new OperationInvokingMessageHandler(); - handler.setServer(this.server); + handler.setServer(server); handler.setObjectName(this.objectName); handler.setOutputChannel(outputChannel); handler.setOperationName("x"); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); - List params = Arrays.asList(new Object[] { "foo", new Integer(123) }); + List params = Arrays.asList(new Object[] { "foo", 123 }); Message message = MessageBuilder.withPayload(params).build(); handler.handleMessage(message); Message reply = outputChannel.receive(0); @@ -148,8 +163,8 @@ public class OperationInvokingMessageHandlerTests { String x(String s, Integer i); void y(String s); - } + } public static class TestOps implements TestOpsMBean { @@ -164,7 +179,9 @@ public class OperationInvokingMessageHandlerTests { } @Override - public void y(String s) { } + public void y(String s) { + } + } } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/ServiceActivatorDefaultFrameworkMethodTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/ServiceActivatorDefaultFrameworkMethodTests-context.xml index 0e5a5f7c13..4b3f7fa229 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/ServiceActivatorDefaultFrameworkMethodTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/ServiceActivatorDefaultFrameworkMethodTests-context.xml @@ -1,15 +1,14 @@ + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> + + - diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/ServiceActivatorDefaultFrameworkMethodTests-fail-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/ServiceActivatorDefaultFrameworkMethodTests-fail-context.xml index 942235110a..0eca02aafd 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/ServiceActivatorDefaultFrameworkMethodTests-fail-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/ServiceActivatorDefaultFrameworkMethodTests-fail-context.xml @@ -1,15 +1,14 @@ + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> + + - + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/AttributePollingChannelAdapterParserTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/AttributePollingChannelAdapterParserTests-context.xml index ba38340cc8..25b36b71fd 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/AttributePollingChannelAdapterParserTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/AttributePollingChannelAdapterParserTests-context.xml @@ -13,8 +13,9 @@ http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd"> - - + + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/ControlBusParserTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/ControlBusParserTests-context.xml index e758e0fa7b..85be10e52f 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/ControlBusParserTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/ControlBusParserTests-context.xml @@ -1,19 +1,16 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/ControlBusParserTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/ControlBusParserTests.java index e245e69469..e0d623505a 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/ControlBusParserTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/ControlBusParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.integration.core.MessagingTemplate; -import org.springframework.jmx.export.MBeanExporter; import org.springframework.messaging.MessageChannel; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; @@ -44,14 +43,12 @@ public class ControlBusParserTests { private ApplicationContext context; @Test - public void testControlMessageToChannelMetrics() throws InterruptedException { + public void testControlMessageToChannelMetrics() { MessageChannel control = this.context.getBean("controlChannel", MessageChannel.class); MessagingTemplate messagingTemplate = new MessagingTemplate(); Object value = messagingTemplate.convertSendAndReceive(control, "@integrationMbeanExporter.getChannelSendRate('testChannel').count", Object.class); assertEquals(0, value); - MBeanExporter exporter = this.context.getBean(MBeanExporter.class); - exporter.destroy(); } } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/CustomObjectNameTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/CustomObjectNameTests-context.xml index ee04714f3b..1a332dfcee 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/CustomObjectNameTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/CustomObjectNameTests-context.xml @@ -11,7 +11,7 @@ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests-context.xml index b8ca0fbf40..5771651e82 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/DynamicRouterTests-context.xml @@ -10,15 +10,16 @@ xmlns:int-jmx="http://www.springframework.org/schema/integration/jmx"> - - + + + - @@ -11,9 +10,9 @@ - + - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanAutoDetectSecondTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanAutoDetectSecondTests-context.xml index e3b5d4edf7..73b6678848 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanAutoDetectSecondTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanAutoDetectSecondTests-context.xml @@ -1,13 +1,12 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanAutoDetectTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanAutoDetectTests.java index aa76c9b6d0..7703b91b0e 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanAutoDetectTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanAutoDetectTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,13 +24,14 @@ import javax.management.MBeanServer; import javax.management.ObjectName; import org.junit.After; -import org.junit.Ignore; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Dave Syer + * @author Artem Bilan + * * @since 2.0 */ public class MBeanAutoDetectTests { @@ -57,7 +58,6 @@ public class MBeanAutoDetectTests { } @Test - @Ignore // Fails because the MBeanExporter is created before the router public void testRouterMBeanExistsWhenDefinedSecond() throws Exception { context = new ClassPathXmlApplicationContext("MBeanAutoDetectSecondTests-context.xml", getClass()); server = context.getBean(MBeanServer.class); diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanExporterNameTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanExporterNameTests-context.xml index 418a97c365..9525c0787d 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanExporterNameTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanExporterNameTests-context.xml @@ -14,16 +14,16 @@ - - + + - + - + - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanExporterParserTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanExporterParserTests-context.xml index 86395e2b68..120232ff95 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanExporterParserTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanExporterParserTests-context.xml @@ -1,22 +1,20 @@ + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> - + - - + foo bar diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanRegistrationCustomNamingTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanRegistrationCustomNamingTests-context.xml index 2e41b34f61..385172c5a6 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanRegistrationCustomNamingTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanRegistrationCustomNamingTests-context.xml @@ -1,12 +1,10 @@ - - + + - + - + - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanRegistrationTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanRegistrationTests-context.xml index 51d9fb4308..6efe3b6ce3 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanRegistrationTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanRegistrationTests-context.xml @@ -14,7 +14,7 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanTreePollingChannelAdapterParserTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanTreePollingChannelAdapterParserTests-context.xml index 4594f50722..aabb143e9f 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanTreePollingChannelAdapterParserTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MBeanTreePollingChannelAdapterParserTests-context.xml @@ -13,8 +13,11 @@ http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd"> - - + + + + + - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MessageStoreTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MessageStoreTests-context.xml index 8f8fa7a32a..03abb0afd4 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MessageStoreTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MessageStoreTests-context.xml @@ -10,7 +10,7 @@ http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd"> - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MethodInvokerTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MethodInvokerTests-context.xml index e300c2f4e4..dcc7eb70b0 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MethodInvokerTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/MethodInvokerTests-context.xml @@ -1,10 +1,9 @@ + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> @@ -15,7 +14,8 @@ - - + - + + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests-context.xml index f0746eae95..13d84f2f07 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationListeningChannelAdapterParserTests-context.xml @@ -11,8 +11,10 @@ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> - - + + + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests-context.xml index fd148ede37..a973614cea 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/NotificationPublishingChannelAdapterParserTests-context.xml @@ -13,7 +13,7 @@ http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd"> - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/OperationInvokingChannelAdapterParserTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/OperationInvokingChannelAdapterParserTests-context.xml index 4c6ec94578..f52b2162b9 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/OperationInvokingChannelAdapterParserTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/OperationInvokingChannelAdapterParserTests-context.xml @@ -13,8 +13,9 @@ http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd"> - - + + + - - + + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/PollingAdapterMBeanTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/PollingAdapterMBeanTests-context.xml index 0ab4f1ab17..3987901f72 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/PollingAdapterMBeanTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/PollingAdapterMBeanTests-context.xml @@ -12,7 +12,9 @@ http://www.springframework.org/schema/integration/jmx http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd"> - + + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/PriorityChannelTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/PriorityChannelTests-context.xml index 5e758822b0..52ccd7a77e 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/PriorityChannelTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/PriorityChannelTests-context.xml @@ -1,28 +1,26 @@ - - + + - + - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanGatewayTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanGatewayTests-context.xml index 96f478f0f3..304b9513e3 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanGatewayTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanGatewayTests-context.xml @@ -20,7 +20,7 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanNoneTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanNoneTests-context.xml index 6840c5b65e..302e838adf 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanNoneTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanNoneTests-context.xml @@ -1,11 +1,9 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanSwitchTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanSwitchTests-context.xml index d182ef0082..3b297aceda 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanSwitchTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanSwitchTests-context.xml @@ -1,11 +1,9 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanTests-context.xml index 094428a656..1f5b73f9ec 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanTests-context.xml @@ -17,7 +17,7 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanVanillaTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanVanillaTests-context.xml index f22c5d6ebd..2a0d0af5e3 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanVanillaTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/config/RouterMBeanVanillaTests-context.xml @@ -1,15 +1,13 @@ + http://www.springframework.org/schema/integration/spring-integration.xsd"> @@ -17,7 +15,7 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/configuration/DslMBeanTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/configuration/DslMBeanTests.java new file mode 100644 index 0000000000..c270ef707c --- /dev/null +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/configuration/DslMBeanTests.java @@ -0,0 +1,114 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.jmx.configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.dsl.IntegrationFlow; +import org.springframework.integration.dsl.IntegrationFlows; +import org.springframework.integration.dsl.context.IntegrationFlowContext; +import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport; +import org.springframework.jmx.support.MBeanServerFactoryBean; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +/** + * @author Gary Russell + * @author Artem Bilan + * + * @since 5.1 + * + */ +@SpringJUnitConfig +@DirtiesContext +public class DslMBeanTests { + + @Autowired + private MBeanServer server; + + @Test + void testRuntimeBeanRegistration(@Autowired IntegrationFlowContext context) throws Exception { + Set query = this.server.queryNames(new ObjectName("dsl:type=MessageChannel,*"), null); + assertThat(query).hasSize(3); + + query = this.server.queryNames(new ObjectName("dsl:type=MessageHandler,*"), null); + assertThat(query).hasSize(2); + + query = this.server.queryNames(new ObjectName("dsl:type=MessageSource,*"), null); + assertThat(query).hasSize(0); + + IntegrationFlow dynamicFlow = + IntegrationFlows.from(() -> "foo", e -> e.poller(p -> p.fixedDelay(1000))) + .channel("channelTwo") + .nullChannel(); + + IntegrationFlowContext.IntegrationFlowRegistration registration = + context.registration(dynamicFlow) + .id("dynamic") + .register(); + + query = this.server.queryNames(new ObjectName("dsl:type=MessageChannel,*"), null); + assertThat(query).hasSize(4); + + query = this.server.queryNames(new ObjectName("dsl:type=MessageHandler,*"), null); + assertThat(query).hasSize(3); + + query = this.server.queryNames(new ObjectName("dsl:type=MessageSource,*"), null); + assertThat(query).hasSize(1); + + registration.destroy(); + + query = this.server.queryNames(new ObjectName("dsl:type=MessageChannel,*"), null); + assertThat(query).hasSize(3); + + query = this.server.queryNames(new ObjectName("dsl:type=MessageHandler,*"), null); + assertThat(query).hasSize(2); + + query = this.server.queryNames(new ObjectName("dsl:type=MessageSource,*"), null); + assertThat(query).hasSize(0); + } + + @Configuration + @EnableIntegrationMBeanExport(defaultDomain = "dsl", server = "mbeanServer") + @EnableIntegration + public static class Config { + + @Bean + public static MBeanServerFactoryBean mbeanServer() { + return new MBeanServerFactoryBean(); + } + + @Bean + public IntegrationFlow staticFlow() { + return IntegrationFlows.from("channelOne") + .nullChannel(); + } + + } + +} diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/configuration/EnableMBeanExportTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/configuration/EnableMBeanExportTests.java index eaa5d8cbc5..0d07370c07 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/configuration/EnableMBeanExportTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/jmx/configuration/EnableMBeanExportTests.java @@ -133,9 +133,7 @@ public class EnableMBeanExportTests { @Bean public MBeanServerFactoryBean mbeanServer() { - MBeanServerFactoryBean mBeanServerFactoryBean = new MBeanServerFactoryBean(); - mBeanServerFactoryBean.setLocateExistingServerIfPossible(true); - return mBeanServerFactoryBean; + return new MBeanServerFactoryBean(); } @Bean diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/AggregatingMetricsTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/AggregatingMetricsTests-context.xml index 1727a8f3f9..fb49944e76 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/AggregatingMetricsTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/AggregatingMetricsTests-context.xml @@ -1,15 +1,13 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests-context.xml index 4064c6879a..b7086ee7ca 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChainWithMessageProducingHandlersTests-context.xml @@ -10,8 +10,7 @@ class="org.springframework.integration.monitor.IntegrationMBeanExporter" p:server-ref="mbeanServer" p:defaultDomain="forum" /> - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml index 1ed5b18c5d..652e95484e 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml @@ -18,9 +18,7 @@ - - - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/IdempotentReceiverIntegrationTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/IdempotentReceiverIntegrationTests.java index 9385416901..8cc1cb8938 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/IdempotentReceiverIntegrationTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/IdempotentReceiverIntegrationTests.java @@ -242,9 +242,7 @@ public class IdempotentReceiverIntegrationTests { @Bean public static MBeanServerFactoryBean mBeanServer() { - MBeanServerFactoryBean mBeanServerFactoryBean = new MBeanServerFactoryBean(); - mBeanServerFactoryBean.setLocateExistingServerIfPossible(true); - return mBeanServerFactoryBean; + return new MBeanServerFactoryBean(); } @Bean diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MBeanExporterIntegrationTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MBeanExporterIntegrationTests.java index 6912f084c6..1a02ad0299 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MBeanExporterIntegrationTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MBeanExporterIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2016 the original author or authors. + * Copyright 2009-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,7 @@ import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.Lifecycle; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; import org.springframework.context.support.GenericXmlApplicationContext; import org.springframework.integration.channel.AbstractMessageChannel; import org.springframework.integration.channel.QueueChannel; @@ -51,11 +52,18 @@ import org.springframework.integration.endpoint.SourcePollingChannelAdapter; import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport; import org.springframework.integration.test.util.TestUtils; import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MBeanServerFactoryBean; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.support.GenericMessage; import org.springframework.util.Assert; +/** + * @author Dave Syer + * @author Gary Russell + * @author Artem Bilan + * + */ public class MBeanExporterIntegrationTests { private IntegrationMBeanExporter messageChannelsMonitor; @@ -70,21 +78,21 @@ public class MBeanExporterIntegrationTests { } @Test - public void testCircularReferenceNoChannel() throws Exception { + public void testCircularReferenceNoChannel() { context = new GenericXmlApplicationContext(getClass(), "oref-nonchannel.xml"); messageChannelsMonitor = context.getBean(IntegrationMBeanExporter.class); assertNotNull(messageChannelsMonitor); } @Test - public void testCircularReferenceNoChannelInFactoryBean() throws Exception { + public void testCircularReferenceNoChannelInFactoryBean() { context = new GenericXmlApplicationContext(getClass(), "oref-factory-nonchannel.xml"); messageChannelsMonitor = context.getBean(IntegrationMBeanExporter.class); assertNotNull(messageChannelsMonitor); } @Test - public void testCircularReferenceWithChannel() throws Exception { + public void testCircularReferenceWithChannel() { context = new GenericXmlApplicationContext(getClass(), "oref-channel.xml"); messageChannelsMonitor = context.getBean(IntegrationMBeanExporter.class); assertNotNull(messageChannelsMonitor); @@ -98,11 +106,13 @@ public class MBeanExporterIntegrationTests { assertTrue(Arrays.asList(messageChannelsMonitor.getChannelNames()).contains("anonymous")); MBeanServer server = context.getBean(MBeanServer.class); assertEquals(1, server.queryNames(ObjectName.getInstance("com.foo:*"), null).size()); - assertEquals(1, server.queryNames(ObjectName.getInstance("org.springframework.integration:name=anonymous,*"), null).size()); + assertEquals(1, + server.queryNames(ObjectName.getInstance("org.springframework.integration:name=anonymous,*"), null) + .size()); } @Test - public void testCircularReferenceWithChannelInFactoryBeanAutodetected() throws Exception { + public void testCircularReferenceWithChannelInFactoryBeanAutodetected() { context = new GenericXmlApplicationContext(getClass(), "oref-factory-channel-autodetect.xml"); messageChannelsMonitor = context.getBean(IntegrationMBeanExporter.class); assertNotNull(messageChannelsMonitor); @@ -114,7 +124,9 @@ public class MBeanExporterIntegrationTests { messageChannelsMonitor = context.getBean(IntegrationMBeanExporter.class); assertNotNull(messageChannelsMonitor); MBeanServer server = context.getBean(MBeanServer.class); - Set names = server.queryNames(ObjectName.getInstance("org.springframework.integration:type=ManagedEndpoint,*"), null); + Set names = + server.queryNames( + ObjectName.getInstance("org.springframework.integration:type=ManagedEndpoint,*"), null); assertEquals(2, names.size()); names = server.queryNames(ObjectName.getInstance("org.springframework.integration:name=explicit,*"), null); assertEquals(1, names.size()); @@ -129,7 +141,7 @@ public class MBeanExporterIntegrationTests { // Lifecycle method name assertEquals("start", startName); assertTrue((Boolean) server.invoke(names.iterator().next(), "isRunning", null, null)); - messageChannelsMonitor.stopActiveComponents(3000); + messageChannelsMonitor.stopActiveComponents(100); assertFalse((Boolean) server.invoke(names.iterator().next(), "isRunning", null, null)); ActiveChannel activeChannel = context.getBean("activeChannel", ActiveChannel.class); assertTrue(activeChannel.isStopCalled()); @@ -144,7 +156,7 @@ public class MBeanExporterIntegrationTests { QueueChannel input2 = (QueueChannel) extractTarget(context.getBean("input2")); input.purge(null); input2.purge(null); - input.send(new GenericMessage("foo")); + input.send(new GenericMessage<>("foo")); assertNotNull(input2.receive(10000)); } @@ -185,7 +197,9 @@ public class MBeanExporterIntegrationTests { messageChannelsMonitor = context.getBean(IntegrationMBeanExporter.class); assertNotNull(messageChannelsMonitor); MBeanServer server = context.getBean(MBeanServer.class); - Set names = server.queryNames(ObjectName.getInstance("org.springframework.integration:type=ManagedEndpoint,*"), null); + Set names = + server.queryNames( + ObjectName.getInstance("org.springframework.integration:type=ManagedEndpoint,*"), null); assertEquals(1, names.size()); names = server.queryNames(ObjectName.getInstance("org.springframework.integration:name=gateway,*"), null); assertEquals(1, names.size()); @@ -204,7 +218,8 @@ public class MBeanExporterIntegrationTests { context = new GenericXmlApplicationContext(getClass(), "lifecycle-no-source.xml"); server = context.getBean(MBeanServer.class); - names = server.queryNames(ObjectName.getInstance("org.springframework.integration:type=ManagedEndpoint,*"), null); + names = server.queryNames( + ObjectName.getInstance("org.springframework.integration:type=ManagedEndpoint,*"), null); assertEquals(1, names.size()); names = server.queryNames(ObjectName.getInstance("org.springframework.integration:name=gateway,*"), null); assertEquals(1, names.size()); @@ -216,10 +231,13 @@ public class MBeanExporterIntegrationTests { messageChannelsMonitor = context.getBean(IntegrationMBeanExporter.class); assertNotNull(messageChannelsMonitor); MBeanServer server = context.getBean(MBeanServer.class); - Set names = server.queryNames(ObjectName.getInstance("org.springframework.integration:type=MessageChannel,*"), null); + Set names = + server.queryNames( + ObjectName.getInstance("org.springframework.integration:type=MessageChannel,*"), null); // Only one registered (out of >2 available) assertEquals(1, names.size()); - names = server.queryNames(ObjectName.getInstance("org.springframework.integration:type=MessageHandler,*"), null); + names = server.queryNames( + ObjectName.getInstance("org.springframework.integration:type=MessageHandler,*"), null); assertEquals(0, names.size()); } @@ -229,7 +247,9 @@ public class MBeanExporterIntegrationTests { messageChannelsMonitor = context.getBean(IntegrationMBeanExporter.class); assertNotNull(messageChannelsMonitor); MBeanServer server = context.getBean(MBeanServer.class); - Set names = server.queryNames(ObjectName.getInstance("org.springframework.integration:type=ManagedEndpoint,*"), null); + Set names = + server.queryNames( + ObjectName.getInstance("org.springframework.integration:type=ManagedEndpoint,*"), null); assertEquals(2, names.size()); } @@ -247,12 +267,26 @@ public class MBeanExporterIntegrationTests { @EnableIntegrationMBeanExport(defaultDomain = "config1") public static class Config1 { + @Bean + public MBeanServerFactoryBean fb() { + MBeanServerFactoryBean fb = new MBeanServerFactoryBean(); + fb.setLocateExistingServerIfPossible(true); + return fb; + } + } @EnableIntegration @EnableIntegrationMBeanExport(defaultDomain = "config2") public static class Config2 { + @Bean + public MBeanServerFactoryBean fb() { + MBeanServerFactoryBean fb = new MBeanServerFactoryBean(); + fb.setLocateExistingServerIfPossible(true); + return fb; + } + } public static class BogusEndpoint extends AbstractEndpoint { @@ -313,7 +347,7 @@ public class MBeanExporterIntegrationTests { } @Override - public void afterPropertiesSet() throws Exception { + public void afterPropertiesSet() { Assert.state(date != null, "A date must be provided"); } @@ -322,6 +356,7 @@ public class MBeanExporterIntegrationTests { public static class MetricFactoryBean implements FactoryBean { private MessageChannel channel; + private final Metric date = new Metric(); public void setChannel(MessageChannel channel) { @@ -329,7 +364,7 @@ public class MBeanExporterIntegrationTests { } @Override - public Metric getObject() throws Exception { + public Metric getObject() { Assert.state(channel != null, "A channel must be provided"); return date; } @@ -367,11 +402,15 @@ public class MBeanExporterIntegrationTests { } public interface Service { + String execute() throws Exception; + int getCounter(); + } public static class SimpleService implements Service { + private int counter; @Override @@ -385,10 +424,13 @@ public class MBeanExporterIntegrationTests { public int getCounter() { return counter; } + } public interface ActiveChannel { + boolean isStopCalled(); + } public static class ActiveChannelImpl extends AbstractMessageChannel implements Lifecycle, ActiveChannel { @@ -418,6 +460,7 @@ public class MBeanExporterIntegrationTests { public boolean isStopCalled() { return this.stopCalled; } + } public static class OtherActiveComponent extends MessageProducerSupport @@ -446,9 +489,11 @@ public class MBeanExporterIntegrationTests { this.afterCalled = true; return 0; } + } public static class AMessageProducer extends MessageProducerSupport { + } } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessageMetricsAdviceTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessageMetricsAdviceTests.java index 6b91cc63bb..a18aa757b2 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessageMetricsAdviceTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessageMetricsAdviceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2017 the original author or authors. + * Copyright 2011-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,22 +18,27 @@ package org.springframework.integration.monitor; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.NameMatchMethodPointcutAdvisor; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.context.support.GenericApplicationContext; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.integration.channel.NullChannel; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport; import org.springframework.integration.support.MessageBuilder; -import org.springframework.integration.test.util.TestUtils; +import org.springframework.jmx.support.MBeanServerFactoryBean; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessagingException; -import org.springframework.util.ClassUtils; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * @author Tareq Abedrabbo @@ -43,75 +48,62 @@ import org.springframework.util.ClassUtils; * * @since 2.0.4 */ +@SpringJUnitConfig +@DirtiesContext public class MessageMetricsAdviceTests { - private GenericApplicationContext applicationContext; + private BeanFactory beanFactory; - private ConfigurableListableBeanFactory beanFactory; + private BeanDefinitionRegistry beanDefinitionRegistry; - private IntegrationMBeanExporter mBeanExporter; - - private MessageHandler handler; - - private MessageChannel channel; - - @Before - public void setUp() throws Exception { - this.applicationContext = TestUtils.createTestApplicationContext(); - this.beanFactory = this.applicationContext.getBeanFactory(); - this.channel = new NullChannel(); - this.mBeanExporter = new IntegrationMBeanExporter(); - this.mBeanExporter.setApplicationContext(this.applicationContext); - this.mBeanExporter.setBeanFactory(this.beanFactory); - this.mBeanExporter.setBeanClassLoader(ClassUtils.getDefaultClassLoader()); - this.mBeanExporter.afterPropertiesSet(); - this.handler = new DummyHandler(); - applicationContext.refresh(); - } - - @After - public void tearDown() throws Exception { - if (this.applicationContext != null) { - this.applicationContext.close(); - } + @Autowired + MessageMetricsAdviceTests(BeanFactory beanFactory) { + this.beanFactory = beanFactory; + this.beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory; } @Test - public void exportAdvisedHandler() throws Exception { - + void exportAdvisedHandler() { DummyInterceptor interceptor = new DummyInterceptor(); NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(interceptor); advisor.addMethodName("handleMessage"); - ProxyFactory factory = new ProxyFactory(this.handler); + MessageHandler handler = new DummyHandler(); + + ProxyFactory factory = new ProxyFactory(handler); factory.addAdvisor(advisor); MessageHandler advised = (MessageHandler) factory.getProxy(); - this.beanFactory.registerSingleton("test", advised); - this.beanFactory.initializeBean(advised, "test"); + this.beanDefinitionRegistry.registerBeanDefinition("test", + BeanDefinitionBuilder.genericBeanDefinition(MessageHandler.class, () -> advised) + .getRawBeanDefinition()); - mBeanExporter.afterSingletonsInstantiated(); MessageHandler exported = this.beanFactory.getBean("test", MessageHandler.class); exported.handleMessage(MessageBuilder.withPayload("test").build()); + + this.beanDefinitionRegistry.removeBeanDefinition("test"); } @Test - public void exportAdvisedChannel() throws Exception { - + void exportAdvisedChannel() { DummyInterceptor interceptor = new DummyInterceptor(); NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(interceptor); advisor.addMethodName("send"); + MessageChannel channel = new NullChannel(); + ProxyFactory factory = new ProxyFactory(channel); factory.addAdvisor(advisor); MessageChannel advised = (MessageChannel) factory.getProxy(); - this.beanFactory.registerSingleton("test", advised); - this.beanFactory.initializeBean(advised, "test"); + this.beanDefinitionRegistry.registerBeanDefinition("test", + BeanDefinitionBuilder.genericBeanDefinition(MessageChannel.class, () -> advised) + .getRawBeanDefinition()); - mBeanExporter.afterSingletonsInstantiated(); MessageChannel exported = this.beanFactory.getBean("test", MessageChannel.class); exported.send(MessageBuilder.withPayload("test").build()); + + this.beanDefinitionRegistry.removeBeanDefinition("test"); } private static class DummyHandler implements MessageHandler { @@ -151,4 +143,16 @@ public class MessageMetricsAdviceTests { } + @Configuration + @EnableIntegrationMBeanExport + @EnableIntegration + public static class Config { + + @Bean + public MBeanServerFactoryBean fb() { + return new MBeanServerFactoryBean(); + } + + } + } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessageSourceTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessageSourceTests.java index 5271468c35..9e43f294f9 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessageSourceTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessageSourceTests.java @@ -73,15 +73,13 @@ public class MessageSourceTests { } @Configuration - @EnableIntegrationMBeanExport(defaultDomain = "foo") + @EnableIntegrationMBeanExport(server = "mbeanServer", defaultDomain = "foo") @EnableIntegration public static class Config { @Bean - public MBeanServerFactoryBean fb() { - MBeanServerFactoryBean fb = new MBeanServerFactoryBean(); - fb.setLocateExistingServerIfPossible(true); - return fb; + public MBeanServerFactoryBean mbeanServer() { + return new MBeanServerFactoryBean(); } @Bean diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessagingGatewaySupportRegistrationTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessagingGatewaySupportRegistrationTests.java index 83deedfb68..09b3c1cc46 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessagingGatewaySupportRegistrationTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MessagingGatewaySupportRegistrationTests.java @@ -66,7 +66,7 @@ public class MessagingGatewaySupportRegistrationTests { @Configuration @EnableIntegration - @EnableIntegrationMBeanExport + @EnableIntegrationMBeanExport(server = "mbeanServer") public static class ContextConfiguration { @Bean @@ -100,10 +100,8 @@ public class MessagingGatewaySupportRegistrationTests { } @Bean - public static MBeanServerFactoryBean server() { - MBeanServerFactoryBean fb = new MBeanServerFactoryBean(); - fb.setLocateExistingServerIfPossible(true); - return fb; + public static MBeanServerFactoryBean mbeanServer() { + return new MBeanServerFactoryBean(); } } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MonitorTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MonitorTests-context.xml index 89acfd8b3a..e863fc4fae 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MonitorTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/MonitorTests-context.xml @@ -1,15 +1,14 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/RemoteMBeanServerTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/RemoteMBeanServerTests-context.xml index d79873f4de..09d05345b0 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/RemoteMBeanServerTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/RemoteMBeanServerTests-context.xml @@ -1,17 +1,16 @@ - + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ScatterGatherHandlerIntegrationTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ScatterGatherHandlerIntegrationTests.java index d87319d233..3cb22e8e52 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ScatterGatherHandlerIntegrationTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ScatterGatherHandlerIntegrationTests.java @@ -128,9 +128,7 @@ public class ScatterGatherHandlerIntegrationTests { @Bean public static MBeanServerFactoryBean mBeanServer() { - MBeanServerFactoryBean mBeanServerFactoryBean = new MBeanServerFactoryBean(); - mBeanServerFactoryBean.setLocateExistingServerIfPossible(true); - return mBeanServerFactoryBean; + return new MBeanServerFactoryBean(); } @Bean diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/transformerContextTests.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/TransformerContextTests-context.xml similarity index 79% rename from spring-integration-jmx/src/test/java/org/springframework/integration/monitor/transformerContextTests.xml rename to spring-integration-jmx/src/test/java/org/springframework/integration/monitor/TransformerContextTests-context.xml index 0498f75b3a..b74cd17423 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/transformerContextTests.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/TransformerContextTests-context.xml @@ -1,20 +1,18 @@ - + - + @@ -40,7 +38,7 @@ - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/TransformerContextTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/TransformerContextTests.java index f234fbc2a1..3c1e9e898c 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/TransformerContextTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/TransformerContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,9 +19,10 @@ package org.springframework.integration.monitor; import static org.junit.Assert.assertEquals; import org.junit.Test; +import org.junit.runner.RunWith; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice; import org.springframework.integration.support.MessageBuilder; @@ -29,45 +30,49 @@ import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Mark Fisher * @author Gary Russell + * @author Artem Bilan */ +@RunWith(SpringJUnit4ClassRunner.class) +@DirtiesContext public class TransformerContextTests { private static volatile int adviceCalled; private static volatile int bazCalled; + @Autowired + private ApplicationContext context; + @Test public void methodInvokingTransformer() { - ConfigurableApplicationContext context = new ClassPathXmlApplicationContext( - "transformerContextTests.xml", this.getClass()); - MessageChannel input = context.getBean("input", MessageChannel.class); - PollableChannel output = context.getBean("output", PollableChannel.class); - input.send(new GenericMessage("foo")); + MessageChannel input = this.context.getBean("input", MessageChannel.class); + PollableChannel output = this.context.getBean("output", PollableChannel.class); + input.send(new GenericMessage<>("foo")); Message reply = output.receive(0); assertEquals("FOO", reply.getPayload()); assertEquals(1, adviceCalled); - input = context.getBean("direct", MessageChannel.class); - input.send(new GenericMessage("foo")); + input = this.context.getBean("direct", MessageChannel.class); + input.send(new GenericMessage<>("foo")); reply = output.receive(0); assertEquals("FOO", reply.getPayload()); - input = context.getBean("directRef", MessageChannel.class); - input.send(new GenericMessage("foo")); + input = this.context.getBean("directRef", MessageChannel.class); + input.send(new GenericMessage<>("foo")); reply = output.receive(0); assertEquals("FOO", reply.getPayload()); assertEquals(2, adviceCalled); - input = context.getBean("service", MessageChannel.class); - input.send(new GenericMessage("foo")); + input = this.context.getBean("service", MessageChannel.class); + input.send(new GenericMessage<>("foo")); assertEquals(1, bazCalled); assertEquals(3, adviceCalled); - - context.close(); } public static class FooAdvice extends AbstractRequestHandlerAdvice { diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/common-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/common-context.xml index 24c9105ea3..b881ed26e9 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/common-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/common-context.xml @@ -1,16 +1,11 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - - - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/duplicate-components.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/duplicate-components.xml index 1d465c7f8a..1c47320876 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/duplicate-components.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/duplicate-components.xml @@ -9,9 +9,7 @@ - - - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/excluded-components.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/excluded-components.xml index 743f061a2a..ee6d558f8f 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/excluded-components.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/excluded-components.xml @@ -1,18 +1,15 @@ + xmlns:int="http://www.springframework.org/schema/integration" + xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - - - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/self-destruction-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/self-destruction-context.xml index bf30aacdcd..8fe8b34bdd 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/self-destruction-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/self-destruction-context.xml @@ -1,18 +1,16 @@ + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> + expression="@integrationMbeanExporter.stopActiveComponents(100)" /> @@ -21,9 +19,9 @@ - + - + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration_/mbeanexporterhelper/INT-2626-config.xml b/spring-integration-jmx/src/test/java/org/springframework/integration_/mbeanexporterhelper/INT-2626-config.xml index de7dff0fc6..025c4b4227 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration_/mbeanexporterhelper/INT-2626-config.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration_/mbeanexporterhelper/INT-2626-config.xml @@ -1,15 +1,14 @@ - + + servers = MBeanServerFactory.findMBeanServer(null); - assertEquals(1, servers.size()); - MBeanServer server = servers.get(0); + MBeanServer server = context.getBean(MBeanServer.class); Set mbeans = server.queryMBeans(null, null); int bits = 0; int count = 0; diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration_/mbeanexporterhelper/single-config-custom-exporter.xml b/spring-integration-jmx/src/test/java/org/springframework/integration_/mbeanexporterhelper/single-config-custom-exporter.xml index 4798d828e2..8cde875bea 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration_/mbeanexporterhelper/single-config-custom-exporter.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration_/mbeanexporterhelper/single-config-custom-exporter.xml @@ -1,16 +1,14 @@ + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> - + - +