Browse Source
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-847pull/1066/merge
17 changed files with 537 additions and 4 deletions
@ -0,0 +1,86 @@
@@ -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; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,48 @@
@@ -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 @@
@@ -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 @@
@@ -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 @@
@@ -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 @@
@@ -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 @@
@@ -0,0 +1 @@
|
||||
service.greeting: Hello |
||||
@ -0,0 +1,20 @@
@@ -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 @@
@@ -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 @@
@@ -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 @@
@@ -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