Support for Jmx (and Integration) autoconfig in parent contexts
When there are parent contexts we already had a strategy for registering the actuator endpoints, but not the regular JMX or Integration MBeans. This chnage makes the autoconfigs for JMX aware of the parent context. Also adds a sample with a parent context. See gh-847
This commit is contained in:
parent
5adbf32c18
commit
36130b27e1
|
|
@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.jmx;
|
|||
|
||||
import javax.management.MBeanServer;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
|
|
@ -26,7 +28,13 @@ import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableMBeanExport;
|
||||
import org.springframework.context.annotation.MBeanExportConfiguration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.type.StandardAnnotationMetadata;
|
||||
import org.springframework.jmx.export.MBeanExporter;
|
||||
import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource;
|
||||
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
|
||||
import org.springframework.jmx.export.naming.ObjectNamingStrategy;
|
||||
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
||||
|
||||
/**
|
||||
|
|
@ -42,10 +50,33 @@ import org.springframework.jmx.support.MBeanServerFactoryBean;
|
|||
@ConditionalOnExpression("${spring.jmx.enabled:true}")
|
||||
public class JmxAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
@Autowired
|
||||
private ObjectNamingStrategy namingStrategy;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(value = MBeanExporter.class, search = SearchStrategy.CURRENT)
|
||||
@EnableMBeanExport(defaultDomain = "${spring.jmx.default_domain:}", server = "${spring.jmx.server:mbeanServer}")
|
||||
public static class MBeanExport {
|
||||
public AnnotationMBeanExporter mbeanExporter() {
|
||||
// Re-use the @EnableMBeanExport configuration
|
||||
MBeanExportConfiguration config = new MBeanExportConfiguration();
|
||||
config.setEnvironment(this.environment);
|
||||
config.setBeanFactory(this.beanFactory);
|
||||
config.setImportMetadata(new StandardAnnotationMetadata(Empty.class));
|
||||
// But add a custom naming strategy
|
||||
AnnotationMBeanExporter exporter = config.mbeanExporter();
|
||||
exporter.setNamingStrategy(this.namingStrategy);
|
||||
return exporter;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ObjectNamingStrategy.class)
|
||||
public ParentAwareNamingStrategy objectNamingStrategy() {
|
||||
return new ParentAwareNamingStrategy(new AnnotationJmxAttributeSource());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
@ -56,4 +87,9 @@ public class JmxAutoConfiguration {
|
|||
return factory;
|
||||
}
|
||||
|
||||
@EnableMBeanExport(defaultDomain = "${spring.jmx.default_domain:}", server = "${spring.jmx.server:mbeanServer}")
|
||||
private static class Empty {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Copyright 2012-2013 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.boot.autoconfigure.jmx;
|
||||
|
||||
import javax.management.MalformedObjectNameException;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.jmx.export.metadata.JmxAttributeSource;
|
||||
import org.springframework.jmx.export.naming.MetadataNamingStrategy;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @since 1.1.1
|
||||
*/
|
||||
public class ParentAwareNamingStrategy extends MetadataNamingStrategy implements
|
||||
ApplicationContextAware {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
private boolean ensureUniqueRuntimeObjectNames = false;
|
||||
|
||||
public ParentAwareNamingStrategy(JmxAttributeSource attributeSource) {
|
||||
super(attributeSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ensureUniqueRuntimeObjectNames the ensureUniqueRuntimeObjectNames to set
|
||||
*/
|
||||
public void setEnsureUniqueRuntimeObjectNames(boolean ensureUniqueRuntimeObjectNames) {
|
||||
this.ensureUniqueRuntimeObjectNames = ensureUniqueRuntimeObjectNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectName getObjectName(Object managedBean, String beanKey)
|
||||
throws MalformedObjectNameException {
|
||||
StringBuilder builder = new StringBuilder(beanKey);
|
||||
if (parentContextContainsSameBean(this.applicationContext, beanKey)) {
|
||||
builder.append(",context="
|
||||
+ ObjectUtils.getIdentityHexString(this.applicationContext));
|
||||
}
|
||||
if (this.ensureUniqueRuntimeObjectNames) {
|
||||
builder.append(",identity=" + ObjectUtils.getIdentityHexString(managedBean));
|
||||
}
|
||||
ObjectName name = super.getObjectName(managedBean, beanKey);
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
private boolean parentContextContainsSameBean(ApplicationContext applicationContext,
|
||||
String beanKey) {
|
||||
if (applicationContext.getParent() != null) {
|
||||
try {
|
||||
this.applicationContext.getParent().getBean(beanKey);
|
||||
return true;
|
||||
}
|
||||
catch (BeansException ex) {
|
||||
return parentContextContainsSameBean(applicationContext.getParent(),
|
||||
beanKey);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.integration;
|
|||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.integration.support.channel.HeaderChannelRegistry;
|
||||
|
||||
|
|
@ -29,10 +30,18 @@ import static org.junit.Assert.assertNotNull;
|
|||
*/
|
||||
public class IntegrationAutoConfigurationTests {
|
||||
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
@Test
|
||||
public void integrationIsAvailable() {
|
||||
this.context.register(IntegrationAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(HeaderChannelRegistry.class));
|
||||
this.context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addJmxAuto() {
|
||||
this.context.register(JmxAutoConfiguration.class,
|
||||
IntegrationAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
|
|
@ -40,4 +49,18 @@ public class IntegrationAutoConfigurationTests {
|
|||
this.context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parentContext() {
|
||||
this.context.register(IntegrationAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
AnnotationConfigApplicationContext parent = this.context;
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.setParent(parent);
|
||||
this.context.register(IntegrationAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(HeaderChannelRegistry.class));
|
||||
((ConfigurableApplicationContext) this.context.getParent()).close();
|
||||
this.context.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import org.junit.Rule;
|
|||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
|
@ -51,6 +52,9 @@ public class JmxAutoConfigurationTests {
|
|||
public void tearDown() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
if (this.context.getParent() != null) {
|
||||
((ConfigurableApplicationContext) this.context.getParent()).close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -104,6 +108,18 @@ public class JmxAutoConfigurationTests {
|
|||
ReflectionTestUtils.getField(naming, "defaultDomain"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParentContext() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(JmxAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
AnnotationConfigApplicationContext parent = this.context;
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.setParent(parent);
|
||||
this.context.register(JmxAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class TestConfiguration {
|
||||
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@
|
|||
<module>spring-boot-sample-integration</module>
|
||||
<module>spring-boot-sample-jetty</module>
|
||||
<module>spring-boot-sample-liquibase</module>
|
||||
<module>spring-boot-sample-parent-context</module>
|
||||
<module>spring-boot-sample-profile</module>
|
||||
<module>spring-boot-sample-secure</module>
|
||||
<module>spring-boot-sample-servlet</module>
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-integration</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-jmx</artifactId>
|
||||
|
|
|
|||
|
|
@ -17,12 +17,16 @@
|
|||
package sample.integration;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.jmx.export.annotation.ManagedAttribute;
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
|
||||
@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false)
|
||||
@ManagedResource
|
||||
public class ServiceProperties {
|
||||
|
||||
private String greeting = "Hello";
|
||||
|
||||
@ManagedAttribute
|
||||
public String getGreeting() {
|
||||
return this.greeting;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<!-- Your own application should inherit from spring-boot-starter-parent -->
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-samples</artifactId>
|
||||
<version>1.1.1.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>spring-boot-sample-parent-context</artifactId>
|
||||
<name>spring-boot-sample-parent-context</name>
|
||||
<description>Spring Boot Integration Sample</description>
|
||||
<url>http://projects.spring.io/spring-boot/</url>
|
||||
<organization>
|
||||
<name>Pivotal Software, Inc.</name>
|
||||
<url>http://www.spring.io</url>
|
||||
</organization>
|
||||
<properties>
|
||||
<main.basedir>${basedir}/../..</main.basedir>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-integration</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-jmx</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright 2012-2013 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 sample.parent;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class HelloWorldService {
|
||||
|
||||
@Autowired
|
||||
private ServiceProperties configuration;
|
||||
|
||||
public String getHelloMessage(String name) {
|
||||
return this.configuration.getGreeting() + " " + name;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright 2012-2013 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 sample.parent;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.annotation.MessageEndpoint;
|
||||
import org.springframework.integration.annotation.ServiceActivator;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
@MessageEndpoint
|
||||
public class SampleEndpoint {
|
||||
|
||||
@Autowired
|
||||
private HelloWorldService helloWorldService;
|
||||
|
||||
@ServiceActivator
|
||||
public String hello(File input) throws Exception {
|
||||
FileInputStream in = new FileInputStream(input);
|
||||
String name = new String(StreamUtils.copyToByteArray(in));
|
||||
in.close();
|
||||
return this.helloWorldService.getHelloMessage(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright 2012-2013 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 sample.parent;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@EnableConfigurationProperties(ServiceProperties.class)
|
||||
@ComponentScan
|
||||
public class SampleParentContextApplication {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new SpringApplicationBuilder(Parent.class).child(SampleParentContextApplication.class).run(args);
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@ImportResource("integration-context.xml")
|
||||
protected static class Parent {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright 2012-2014 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 sample.parent;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.jmx.export.annotation.ManagedAttribute;
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
|
||||
@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false)
|
||||
@ManagedResource
|
||||
public class ServiceProperties {
|
||||
|
||||
private String greeting = "Hello";
|
||||
|
||||
@ManagedAttribute
|
||||
public String getGreeting() {
|
||||
return this.greeting;
|
||||
}
|
||||
|
||||
public void setGreeting(String greeting) {
|
||||
this.greeting = greeting;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
service.greeting: Hello
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-file="http://www.springframework.org/schema/integration/file"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
|
||||
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<int-file:inbound-channel-adapter channel="input" directory="target/input" filename-pattern="*">
|
||||
<int:poller fixed-rate="500"/>
|
||||
</int-file:inbound-channel-adapter>
|
||||
|
||||
<int:service-activator input-channel="input" ref="sampleEndpoint" output-channel="output"/>
|
||||
|
||||
<int:channel id="output"/>
|
||||
|
||||
<int-file:outbound-channel-adapter channel="output" directory="target/output"/>
|
||||
|
||||
</beans>
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml"/>
|
||||
|
||||
<logger name="org.springframework.integration.file" level="DEBUG" />
|
||||
</configuration>
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Copyright 2012-2013 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 sample.parent.consumer;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.ResourcePatternUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import sample.parent.SampleParentContextApplication;
|
||||
import sample.parent.producer.ProducerApplication;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Basic integration tests for service demo application.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class SampleIntegrationApplicationTests {
|
||||
|
||||
private static ConfigurableApplicationContext context;
|
||||
|
||||
@BeforeClass
|
||||
public static void start() throws Exception {
|
||||
context = SpringApplication.run(SampleParentContextApplication.class);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stop() {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVanillaExchange() throws Exception {
|
||||
SpringApplication.run(ProducerApplication.class, "World");
|
||||
String output = getOutput();
|
||||
assertTrue("Wrong output: " + output, output.contains("Hello World"));
|
||||
}
|
||||
|
||||
private String getOutput() throws Exception {
|
||||
Future<String> future = Executors.newSingleThreadExecutor().submit(
|
||||
new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
Resource[] resources = new Resource[0];
|
||||
while (resources.length == 0) {
|
||||
Thread.sleep(200);
|
||||
resources = ResourcePatternUtils.getResourcePatternResolver(
|
||||
new DefaultResourceLoader()).getResources(
|
||||
"file:target/output/**");
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Resource resource : resources) {
|
||||
builder.append(new String(StreamUtils
|
||||
.copyToByteArray(resource.getInputStream())));
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
});
|
||||
return future.get(30, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright 2012-2013 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 sample.parent.producer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ProducerApplication implements CommandLineRunner {
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
new File("target/input").mkdirs();
|
||||
if (args.length > 0) {
|
||||
FileOutputStream stream = new FileOutputStream("target/input/data"
|
||||
+ System.currentTimeMillis() + ".txt");
|
||||
for (String arg : args) {
|
||||
stream.write(arg.getBytes());
|
||||
}
|
||||
stream.flush();
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(ProducerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue