forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add start/stop goals to maven plugin
SpringApplicationLifecycle provides lifecycle operations on the current Spring Boot application. It can be registered as an MBean of the platform MBean server if a specific property is set. Besides, the JMX name can also be customized via a property in case more than one Spring Boot application is started in the same process. The Maven plugin uses that MBean to check that the application is ready before ending the "start" phase. It uses it to trigger a proper shutdown of the application during the "stop" phase. If the process has to be forked, the platform MBean server is exposed on a configurable port so that the maven plugin can connect to it. Such change permits the maven plugin to integrate a classical integration test scenario where the "start" goal is invoked during the pre-integration phase and the "stop" goal during the post-integration phase. Closes spring-projectsgh-2525
- Loading branch information
Showing
25 changed files
with
1,918 additions
and
382 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...ngframework/boot/autoconfigure/lifecycle/SpringApplicationLifecycleAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2012-2015 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.lifecycle; | ||
|
||
import javax.management.MalformedObjectNameException; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.jmx.export.MBeanExporter; | ||
|
||
/** | ||
* Register a JMX component that allows to manage the lifecycle of the current | ||
* application. Intended for internal use only. | ||
* | ||
* @author Stephane Nicoll | ||
* @since 1.3.0 | ||
* @see SpringApplicationLifecycleMXBean | ||
*/ | ||
@Configuration | ||
@AutoConfigureAfter(JmxAutoConfiguration.class) | ||
@ConditionalOnProperty(value = "spring.application.lifecycle.enabled", havingValue = "true", matchIfMissing = false) | ||
class SpringApplicationLifecycleAutoConfiguration { | ||
|
||
/** | ||
* The property to use to customize the {@code ObjectName} of the application lifecycle mbean. | ||
*/ | ||
static final String JMX_NAME_PROPERTY = "spring.application.lifecycle.jmx-name"; | ||
|
||
/** | ||
* The default {@code ObjectName} of the application lifecycle mbean. | ||
*/ | ||
static final String DEFAULT_JMX_NAME = "org.springframework.boot:type=Lifecycle,name=springApplicationLifecycle"; | ||
|
||
@Autowired(required = false) | ||
private MBeanExporter mbeanExporter; | ||
|
||
@Autowired | ||
private Environment environment; | ||
|
||
@Bean | ||
public SpringApplicationLifecycleRegistrar springApplicationLifecycleRegistrar() | ||
throws MalformedObjectNameException { | ||
|
||
String jmxName = this.environment.getProperty(JMX_NAME_PROPERTY, DEFAULT_JMX_NAME); | ||
if (mbeanExporter != null) { // Make sure to not register that MBean twice | ||
mbeanExporter.addExcludedBean(jmxName); | ||
} | ||
return new SpringApplicationLifecycleRegistrar(jmxName); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...va/org/springframework/boot/autoconfigure/lifecycle/SpringApplicationLifecycleMXBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2012-2015 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.lifecycle; | ||
|
||
/** | ||
* A simple MBean contract to control the lifecycle of a {@code SpringApplication} via | ||
* JMX. Intended for internal use only. | ||
* | ||
* @author Stephane Nicoll | ||
* @since 1.3.0 | ||
*/ | ||
public interface SpringApplicationLifecycleMXBean { | ||
|
||
/** | ||
* Specify if the application has fully started and is now ready. | ||
* @return {@code true} if the application is ready | ||
* @see org.springframework.boot.context.event.ApplicationReadyEvent | ||
*/ | ||
boolean isReady(); | ||
|
||
/** | ||
* Shutdown the application. | ||
* @see org.springframework.context.ConfigurableApplicationContext#close() | ||
*/ | ||
void shutdown(); | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
...org/springframework/boot/autoconfigure/lifecycle/SpringApplicationLifecycleRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2012-2015 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.lifecycle; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import javax.management.MBeanServer; | ||
import javax.management.MalformedObjectNameException; | ||
import javax.management.ObjectName; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.DisposableBean; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Register a {@link SpringApplicationLifecycleMXBean} implementation to the platform | ||
* {@link MBeanServer}. | ||
* | ||
* @author Stephane Nicoll | ||
* @since 1.3.0 | ||
*/ | ||
public class SpringApplicationLifecycleRegistrar implements ApplicationContextAware, InitializingBean, DisposableBean, | ||
ApplicationListener<ApplicationReadyEvent> { | ||
|
||
private static final Log logger = LogFactory.getLog(SpringApplicationLifecycle.class); | ||
|
||
private ConfigurableApplicationContext applicationContext; | ||
|
||
private final ObjectName objectName; | ||
|
||
private boolean ready = false; | ||
|
||
public SpringApplicationLifecycleRegistrar(String name) throws MalformedObjectNameException { | ||
this.objectName = new ObjectName(name); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
Assert.isTrue(applicationContext instanceof ConfigurableApplicationContext, | ||
"ApplicationContext does not implement ConfigurableApplicationContext"); | ||
this.applicationContext = (ConfigurableApplicationContext) applicationContext; | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(ApplicationReadyEvent event) { | ||
ready = true; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | ||
server.registerMBean(new SpringApplicationLifecycle(), objectName); | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("Application lifecycle MBean registered with name '" + objectName + "'"); | ||
} | ||
} | ||
|
||
@Override | ||
public void destroy() throws Exception { | ||
ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName); | ||
} | ||
|
||
|
||
private class SpringApplicationLifecycle implements SpringApplicationLifecycleMXBean { | ||
|
||
@Override | ||
public boolean isReady() { | ||
return ready; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
logger.info("Application shutdown requested."); | ||
applicationContext.close(); | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.