Skip to content

Commit

Permalink
Adds an afterStart pseudo-event to LoomServer observable by Router im…
Browse files Browse the repository at this point in the history
…plementations

Signed-off-by: Laird Nelson <[email protected]>
  • Loading branch information
ljnelson committed Jan 16, 2025
1 parent 867a60e commit 25b0684
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
* Copyright (c) 2022, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,6 +53,7 @@ class LoomServer implements WebServer {
private static final AtomicInteger WEBSERVER_COUNTER = new AtomicInteger(1);

private final Map<String, ServerListener> listeners;
private final Runnable afterStartNotifier;
private final AtomicBoolean running = new AtomicBoolean();
private final Lock lifecycleLock = new ReentrantLock();
private final ExecutorService executorService;
Expand Down Expand Up @@ -101,6 +102,7 @@ class LoomServer implements WebServer {
});

listeners = Map.copyOf(listenerMap);
afterStartNotifier = () -> listeners.values().forEach(l -> l.router().afterStart(this));
}

@Override
Expand Down Expand Up @@ -225,6 +227,8 @@ private void startIt() {
+ uptime + " milliseconds since JVM startup. "
+ "Java " + Runtime.version());

afterStartNotifier.run();

if ("!".equals(System.getProperty(EXIT_ON_STARTED_KEY))) {
LOGGER.log(System.Logger.Level.INFO, String.format("Exiting, -D%s set.", EXIT_ON_STARTED_KEY));
// we need to run the system exit on a different thread, to correctly finish whatever was happening on main
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,6 +61,14 @@ static Router empty() {
*/
void beforeStart();

/**
* This is called after the server's listeners have successfully started.
*
* @param webServer the {@link WebServer} that has successfully started
*/
default void afterStart(WebServer webServer) {
}

/**
* List of all conifgured routings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public void beforeStart() {
}
}

@Override
public void afterStart(WebServer webServer) {
for (Routing value : routings.values()) {
value.afterStart(webServer);
}
}

@Override
public List<? extends Routing> routings() {
return List.copyOf(routings.values());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,14 @@ public interface ServerLifecycle {
default void beforeStart() {
}

/**
* After server start.
*
* @param webServer the {@link WebServer} that was started
*/
default void afterStart(WebServer webServer) {
}

/**
* After server stop.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
* Copyright (c) 2022, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -212,6 +212,10 @@ int port() {
return connectedPort;
}

Router router() {
return router;
}

InetSocketAddress configuredAddress() {
return configuredAddress;
}
Expand Down

0 comments on commit 25b0684

Please sign in to comment.