Support reactive web servers with LocalServerPort
This commit refactors the `EmbeddedWebServerInitializedEvent` hierarchy to have one specialized event for Servlet based apps and another one for reactive apps. Each event implementation has: * a specific `ApplicationContext` implementation for the app * a custom `getServerId` implementation that differentiates the application server from the management server Closes gh-8348
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.context.embedded;
|
||||
|
||||
/**
|
||||
* Event to be published after the {@link ReactiveWebApplicationContext} is
|
||||
* refreshed and the {@link EmbeddedWebServer} is ready. Useful for
|
||||
* obtaining the local port of a running server.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class EmbeddedReactiveWebServerInitializedEvent extends EmbeddedWebServerInitializedEvent {
|
||||
|
||||
private final ReactiveWebApplicationContext applicationContext;
|
||||
|
||||
public EmbeddedReactiveWebServerInitializedEvent(
|
||||
EmbeddedWebServer source,
|
||||
ReactiveWebApplicationContext applicationContext) {
|
||||
super(source);
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReactiveWebApplicationContext getApplicationContext() {
|
||||
return this.applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerId() {
|
||||
return "server";
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.boot.context.embedded;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Event to be published after the {@link EmbeddedWebApplicationContext} is
|
||||
* refreshed and the {@link EmbeddedWebServer} is ready. Useful for
|
||||
@@ -44,8 +46,18 @@ public class EmbeddedServletContainerInitializedEvent extends EmbeddedWebServerI
|
||||
* context) before acting on the server container itself.
|
||||
* @return the applicationContext that the container was created from
|
||||
*/
|
||||
@Override
|
||||
public EmbeddedWebApplicationContext getApplicationContext() {
|
||||
return this.applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerId() {
|
||||
String name = this.applicationContext.getNamespace();
|
||||
if (StringUtils.isEmpty(name)) {
|
||||
name = "server";
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.context.embedded;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
@@ -24,13 +25,15 @@ import org.springframework.context.ApplicationEvent;
|
||||
* obtaining the local port of a running server.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
* @see EmbeddedServletContainerInitializedEvent
|
||||
* @see EmbeddedReactiveWebServerInitializedEvent
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class EmbeddedWebServerInitializedEvent extends ApplicationEvent {
|
||||
public abstract class EmbeddedWebServerInitializedEvent extends ApplicationEvent {
|
||||
|
||||
public EmbeddedWebServerInitializedEvent(EmbeddedWebServer source) {
|
||||
protected EmbeddedWebServerInitializedEvent(EmbeddedWebServer source) {
|
||||
super(source);
|
||||
}
|
||||
|
||||
@@ -42,6 +45,14 @@ public class EmbeddedWebServerInitializedEvent extends ApplicationEvent {
|
||||
return getSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the application context that the container was created in. Sometimes it is
|
||||
* prudent to check that this matches expectations (like being equal to the current
|
||||
* context) before acting on the server container itself.
|
||||
* @return the applicationContext that the container was created from
|
||||
*/
|
||||
public abstract ApplicationContext getApplicationContext();
|
||||
|
||||
/**
|
||||
* Access the source of the event (an {@link EmbeddedWebServer}).
|
||||
* @return the embedded web server
|
||||
@@ -50,4 +61,12 @@ public class EmbeddedWebServerInitializedEvent extends ApplicationEvent {
|
||||
public EmbeddedWebServer getSource() {
|
||||
return (EmbeddedWebServer) super.getSource();
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the {@link EmbeddedWebServer} Id used internally
|
||||
* to differentiate application / management servers.
|
||||
* @return the server internal Id
|
||||
*/
|
||||
public abstract String getServerId();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user