Commit e364adfe by Johannes Edmeier

Simplify port & ready-state detection

By using local.server.port, local.managment.port and ApplicationReadyEvent the port detection and ready state detection can be simplyfied in boot 1.3
parent 66385226
......@@ -22,10 +22,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
......@@ -74,53 +72,48 @@ public class AdminClientProperties {
@Autowired
private ServerProperties server;
private int serverPort = -1;
private Integer serverPort;
private int managementPort = -1;
private Integer managementPort;
private boolean serverInitialized = false;
private boolean ready = false;
@EventListener
public void onStartedEmbeddedContainer(EmbeddedServletContainerInitializedEvent event) {
if ("management".equals(event.getApplicationContext().getNamespace())) {
managementPort = event.getEmbeddedServletContainer().getPort();
} else {
serverPort = event.getEmbeddedServletContainer().getPort();
}
serverInitialized = true;
}
@EventListener
public void onStartedDeployedWar(ContextRefreshedEvent event) {
if (event.getApplicationContext() instanceof EmbeddedWebApplicationContext) {
EmbeddedWebApplicationContext context = (EmbeddedWebApplicationContext) event
.getApplicationContext();
if (context.getEmbeddedServletContainer() == null) {
if (!StringUtils.hasText(serviceUrl)) {
throw new RuntimeException(
"spring.boot.admin.client.serviceUrl must be set for deployed war files!");
}
serverInitialized = true;
}
}
public void onApplicationReady(ApplicationReadyEvent event) {
serverPort = event.getApplicationContext().getEnvironment().getProperty("local.server.port",
Integer.class);
managementPort = event.getApplicationContext().getEnvironment()
.getProperty("local.management.port", Integer.class, serverPort);
ready = true;
}
public String getManagementUrl() {
if (managementUrl != null) {
return managementUrl;
}
if (managementPort == -1) {
if ((managementPort == null || managementPort.equals(serverPort))
&& getServiceUrl() != null) {
return append(getServiceUrl(), management.getContextPath());
}
if (ready && managementPort == null) {
throw new IllegalStateException(
"serviceUrl must be set when deployed to servlet-container");
}
if (preferIp) {
Assert.notNull(management.getAddress(),
"management.address must be set when using preferIp");
return append(createLocalUri(management.getAddress().getHostAddress(), managementPort),
return append(
append(createLocalUri(management.getAddress().getHostAddress(), managementPort),
server.getContextPath()),
management.getContextPath());
}
return append(createLocalUri(getHostname(), managementPort), management.getContextPath());
return append(
append(createLocalUri(getHostname(), managementPort), server.getContextPath()),
management.getContextPath());
}
public void setManagementUrl(String managementUrl) {
......@@ -143,9 +136,9 @@ public class AdminClientProperties {
return serviceUrl;
}
if (serverPort == -1) {
if (ready && serverPort == null) {
throw new IllegalStateException(
"EmbeddedServletContainer has not been initialized yet!");
"serviceUrl must be set when deployed to servlet-container");
}
if (preferIp) {
......@@ -161,8 +154,8 @@ public class AdminClientProperties {
this.serviceUrl = serviceUrl;
}
public boolean isServerInitialized() {
return serverInitialized;
public boolean isReady() {
return ready;
}
public String getName() {
......
......@@ -78,7 +78,7 @@ public class SpringBootAdminClientAutoConfiguration {
Runnable registratorTask = new Runnable() {
@Override
public void run() {
if (client.isServerInitialized()) {
if (client.isReady()) {
registrator().register();
}
}
......
package de.codecentric.boot.admin.services;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
......@@ -16,7 +13,8 @@ public class RegistrationApplicationListener {
private boolean autoDeregister = false;
private final TaskExecutor executor;
public RegistrationApplicationListener(ApplicationRegistrator registrator, TaskExecutor executor) {
public RegistrationApplicationListener(ApplicationRegistrator registrator,
TaskExecutor executor) {
this.registrator = registrator;
this.executor = executor;
}
......@@ -27,7 +25,7 @@ public class RegistrationApplicationListener {
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void onStartedEmbeddedServer(EmbeddedServletContainerInitializedEvent event) {
public void onApplicationReady(ApplicationReadyEvent event) {
executor.execute(new Runnable() {
@Override
public void run() {
......@@ -38,24 +36,6 @@ public class RegistrationApplicationListener {
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void onStartedDeployedWar(ContextRefreshedEvent event) {
ApplicationContextEvent contextEvent = event;
if (contextEvent.getApplicationContext() instanceof EmbeddedWebApplicationContext) {
EmbeddedWebApplicationContext context = (EmbeddedWebApplicationContext) contextEvent
.getApplicationContext();
if (context.getEmbeddedServletContainer() == null) {
executor.execute(new Runnable() {
@Override
public void run() {
registrator.register();
}
});
}
}
}
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void onClosedContext(ContextClosedEvent event) {
if (autoDeregister) {
executor.execute(new Runnable() {
......
......@@ -5,21 +5,18 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
public class AdminClientPropertiesTest {
......@@ -35,39 +32,27 @@ public class AdminClientPropertiesTest {
@Test
public void test_isServerStarted_false() {
assertFalse(new AdminClientProperties().isServerInitialized());
assertFalse(new AdminClientProperties().isReady());
}
@Test
public void test_isServerStarted_true_embedded() {
public void test_isServerStarted_true() {
load();
AdminClientProperties clientProperties = new AdminClientProperties();
clientProperties.setServiceUrl("http://localhost");
publishServletContainerInitializedEvent(clientProperties, 8080, null);
assertTrue(clientProperties.isServerInitialized());
}
@Test
public void test_isServerStarted_true_war() {
AdminClientProperties clientProperties = new AdminClientProperties();
clientProperties.setServiceUrl("http://localhost");
publishContextRefreshedEvent(clientProperties);
assertTrue(clientProperties.isServerInitialized());
}
publishApplicationReadyEvent(clientProperties);
@Test(expected = RuntimeException.class)
public void test_isServerStarted_exception_war() {
AdminClientProperties clientProperties = new AdminClientProperties();
publishContextRefreshedEvent(clientProperties);
assertTrue(clientProperties.isReady());
}
@Test
public void test_mgmtPortPath() {
load("management.contextPath=/admin", "endpoints.health.id=alive");
load("management.contextPath=/admin", "endpoints.health.id=alive", "local.server.port=8080",
"local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 8080, null);
publishServletContainerInitializedEvent(clientProperties, 8081, "management");
publishApplicationReadyEvent(clientProperties);
assertThat(clientProperties.getManagementUrl(),
is("http://" + getHostname() + ":8081/admin"));
......@@ -77,26 +62,12 @@ public class AdminClientPropertiesTest {
}
@Test
public void test_mgmtPort() {
load();
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 8080, null);
publishServletContainerInitializedEvent(clientProperties, 8081, "management");
assertThat(clientProperties.getManagementUrl(), is("http://" + getHostname() + ":8081"));
assertThat(clientProperties.getHealthUrl(), is("http://" + getHostname() + ":8081/health"));
assertThat(clientProperties.getServiceUrl(), is("http://" + getHostname() + ":8080"));
}
@Test
public void test_contextPath_mgmtPath() {
load("server.context-path=app", "management.context-path=/admin");
load("server.context-path=app", "management.context-path=/admin", "local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 8080, null);
publishApplicationReadyEvent(clientProperties);
assertThat(clientProperties.getManagementUrl(),
is("http://" + getHostname() + ":8080/app/admin"));
......@@ -107,11 +78,11 @@ public class AdminClientPropertiesTest {
@Test
public void test_contextPath() {
load("server.context-path=app");
load("server.context-path=app", "local.server.port=80");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 80, null);
publishApplicationReadyEvent(clientProperties);
assertThat(clientProperties.getManagementUrl(), is("http://" + getHostname() + ":80/app"));
assertThat(clientProperties.getHealthUrl(),
......@@ -121,11 +92,11 @@ public class AdminClientPropertiesTest {
@Test
public void test_default() {
load();
load("local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 8080, null);
publishApplicationReadyEvent(clientProperties);
assertThat(clientProperties.getManagementUrl(), is("http://" + getHostname() + ":8080"));
assertThat(clientProperties.getHealthUrl(), is("http://" + getHostname() + ":8080/health"));
......@@ -134,11 +105,12 @@ public class AdminClientPropertiesTest {
@Test
public void testSsl() {
load("server.ssl.key-store=somefile.jks", "server.ssl.key-store-password=password");
load("server.ssl.key-store=somefile.jks", "server.ssl.key-store-password=password",
"local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 8080, null);
publishApplicationReadyEvent(clientProperties);
assertThat(clientProperties.getManagementUrl(), is("https://" + getHostname() + ":8080"));
assertThat(clientProperties.getHealthUrl(),
......@@ -148,38 +120,37 @@ public class AdminClientPropertiesTest {
@Test(expected = IllegalArgumentException.class)
public void test_preferIpAddress_serveraddress_missing() {
load();
load("local.server.port=8080");
AdminClientProperties clientProperties = new AdminClientProperties();
clientProperties.setPreferIp(true);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 8080, null);
publishApplicationReadyEvent(clientProperties);
clientProperties.getServiceUrl();
}
@Test(expected = IllegalArgumentException.class)
public void test_preferIpAddress_managementaddress_missing() {
load();
load("local.server.port=8080", "local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties();
clientProperties.setPreferIp(true);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 8080, null);
publishServletContainerInitializedEvent(clientProperties, 8081, "management");
publishApplicationReadyEvent(clientProperties);
clientProperties.getManagementUrl();
}
@Test
public void test_preferIpAddress() {
load("server.address=127.0.0.1", "management.address=127.0.0.2");
load("server.address=127.0.0.1", "management.address=127.0.0.2", "local.server.port=8080",
"local.management.port=8081");
AdminClientProperties clientProperties = new AdminClientProperties();
clientProperties.setPreferIp(true);
context.getAutowireCapableBeanFactory().autowireBean(clientProperties);
publishServletContainerInitializedEvent(clientProperties, 8080, null);
publishServletContainerInitializedEvent(clientProperties, 8081, "management");
publishApplicationReadyEvent(clientProperties);
assertThat(clientProperties.getManagementUrl(), is("http://127.0.0.2:8081"));
assertThat(clientProperties.getHealthUrl(), is("http://127.0.0.2:8081/health"));
......@@ -194,21 +165,9 @@ public class AdminClientPropertiesTest {
}
}
private void publishServletContainerInitializedEvent(AdminClientProperties client, int port,
String namespace) {
EmbeddedServletContainer eventSource = mock(EmbeddedServletContainer.class);
when(eventSource.getPort()).thenReturn(port);
EmbeddedWebApplicationContext eventContext = mock(EmbeddedWebApplicationContext.class);
when(eventContext.getNamespace()).thenReturn(namespace);
when(eventContext.getEmbeddedServletContainer()).thenReturn(eventSource);
client.onStartedEmbeddedContainer(new EmbeddedServletContainerInitializedEvent(
eventContext, eventSource));
}
private void publishContextRefreshedEvent(AdminClientProperties client) {
client.onStartedDeployedWar(new ContextRefreshedEvent(
mock(EmbeddedWebApplicationContext.class)));
private void publishApplicationReadyEvent(AdminClientProperties client) {
client.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), new String[] {}, context));
}
private void load(String... environment) {
......
......@@ -3,14 +3,12 @@ package de.codecentric.boot.admin.services;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.task.SyncTaskExecutor;
public class RegistrationApplicationListenerTest {
......@@ -18,10 +16,11 @@ public class RegistrationApplicationListenerTest {
@Test
public void test_register_embedded() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, new SyncTaskExecutor());
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
new SyncTaskExecutor());
listener.onStartedEmbeddedServer(new EmbeddedServletContainerInitializedEvent(
mock(EmbeddedWebApplicationContext.class), mock(EmbeddedServletContainer.class)));
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null, null));
verify(registrator).register();
}
......@@ -29,30 +28,20 @@ public class RegistrationApplicationListenerTest {
@Test
public void test_register_war() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, new SyncTaskExecutor());
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
new SyncTaskExecutor());
listener.onStartedDeployedWar(new ContextRefreshedEvent(
mock(EmbeddedWebApplicationContext.class)));
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null, null));
verify(registrator).register();
}
@Test
public void test_no_register_war() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, new SyncTaskExecutor());
EmbeddedWebApplicationContext context = mock(EmbeddedWebApplicationContext.class);
when(context.getEmbeddedServletContainer())
.thenReturn(mock(EmbeddedServletContainer.class));
listener.onStartedDeployedWar(new ContextRefreshedEvent(context));
verify(registrator, never()).register();
}
@Test
public void test_no_deregister() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, new SyncTaskExecutor());
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
new SyncTaskExecutor());
listener.onClosedContext(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
......@@ -62,7 +51,8 @@ public class RegistrationApplicationListenerTest {
@Test
public void test_deregister() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, new SyncTaskExecutor());
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator,
new SyncTaskExecutor());
listener.setAutoDeregister(true);
listener.onClosedContext(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment