Revert "Add start/stop goals to maven plugin"

This reverts commit 54e12a07e6.
This commit is contained in:
Phillip Webb
2015-04-16 12:32:06 -07:00
parent 2085a5b0cf
commit a8d099f6b8
25 changed files with 382 additions and 1918 deletions

View File

@@ -1,70 +0,0 @@
/*
* 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);
}
}

View File

@@ -1,41 +0,0 @@
/*
* 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();
}

View File

@@ -1,101 +0,0 @@
/*
* 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();
}
}
}

View File

@@ -37,7 +37,6 @@ org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchDataAutoConfig
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.lifecycle.SpringApplicationLifecycleAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\

View File

@@ -1,179 +0,0 @@
/*
* 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.InstanceNotFoundException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Tests for {@link SpringApplicationLifecycleAutoConfiguration}.
*
* @author Stephane Nicoll
*/
public class SpringApplicationLifecycleAutoConfigurationTests {
public static final String ENABLE_LIFECYCLE_PROP = "spring.application.lifecycle.enabled=true";
@Rule
public final ExpectedException thrown = ExpectedException.none();
private AnnotationConfigApplicationContext context;
private MBeanServer mBeanServer;
@Before
public void setup() throws MalformedObjectNameException {
this.mBeanServer = ManagementFactory.getPlatformMBeanServer();
}
@After
public void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void notRegisteredByDefault() throws MalformedObjectNameException, InstanceNotFoundException {
load();
thrown.expect(InstanceNotFoundException.class);
this.mBeanServer.getObjectInstance(createDefaultObjectName());
}
@Test
public void registeredWithProperty() throws Exception {
load(ENABLE_LIFECYCLE_PROP);
ObjectName objectName = createDefaultObjectName();
ObjectInstance objectInstance = this.mBeanServer.getObjectInstance(objectName);
assertNotNull(objectInstance);
assertEquals("Simple context does not trigger proper event",
false, isCurrentApplicationReady(objectName));
this.context.publishEvent(new ApplicationReadyEvent(new SpringApplication(), null, this.context));
assertEquals("Application should be ready",
true, isCurrentApplicationReady(objectName));
assertTrue("context has been started", this.context.isActive());
mBeanServer.invoke(objectName, "shutdown", null, null);
assertFalse("Context should have been closed", this.context.isActive());
thrown.expect(InstanceNotFoundException.class); // JMX cleanup
this.mBeanServer.getObjectInstance(objectName);
}
@Test
public void registerWithCustomJmxName() throws InstanceNotFoundException {
String customJmxName = "org.acme:name=FooBar";
System.setProperty(SpringApplicationLifecycleAutoConfiguration.JMX_NAME_PROPERTY, customJmxName);
try {
load(ENABLE_LIFECYCLE_PROP);
try {
this.mBeanServer.getObjectInstance(createObjectName(customJmxName));
}
catch (InstanceNotFoundException e) {
fail("lifecycle MBean should have been exposed with custom name");
}
thrown.expect(InstanceNotFoundException.class); // Should not be exposed
this.mBeanServer.getObjectInstance(createDefaultObjectName());
}
finally {
System.clearProperty(SpringApplicationLifecycleAutoConfiguration.JMX_NAME_PROPERTY);
}
}
@Test
public void registerWithSpringApplication() throws Exception {
final ObjectName objectName = createDefaultObjectName();
SpringApplication application = new SpringApplication(ExampleConfig.class,
SpringApplicationLifecycleAutoConfiguration.class);
application.setWebEnvironment(false);
application.addListeners(new ApplicationListener<ContextRefreshedEvent>() {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
try {
assertFalse("Application should not be ready yet", isCurrentApplicationReady(objectName));
}
catch (Exception e) {
throw new IllegalStateException("Could not contact spring application lifecycle bean", e);
}
}
});
application.run("--" + ENABLE_LIFECYCLE_PROP);
assertTrue("application should be ready now", isCurrentApplicationReady(objectName));
}
private ObjectName createDefaultObjectName() {
return createObjectName(SpringApplicationLifecycleAutoConfiguration.DEFAULT_JMX_NAME);
}
private ObjectName createObjectName(String jmxName) {
try {
return new ObjectName(jmxName);
}
catch (MalformedObjectNameException e) {
throw new IllegalStateException("Invalid jmx name " + jmxName, e);
}
}
private Boolean isCurrentApplicationReady(ObjectName objectName) throws Exception {
return (Boolean) this.mBeanServer.getAttribute(objectName, "Ready");
}
private void load(String... environment) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.register(JmxAutoConfiguration.class, SpringApplicationLifecycleAutoConfiguration.class);
applicationContext.refresh();
this.context = applicationContext;
}
@Configuration
static class ExampleConfig {
}
}