Skip to content

Commit 7127648

Browse files
committed
Jooby.stop idempotent fix jooby-project#1351
1 parent 8fc2cea commit 7127648

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
lines changed

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Spliterator;
3131
import java.util.concurrent.Executor;
3232
import java.util.concurrent.ExecutorService;
33+
import java.util.concurrent.atomic.AtomicBoolean;
3334
import java.util.function.BiConsumer;
3435
import java.util.function.Consumer;
3536
import java.util.function.Predicate;
@@ -72,6 +73,8 @@ public class Jooby implements Router, Registry {
7273

7374
private static final String JOOBY_RUN_HOOK = "___jooby_run_hook__";
7475

76+
private final transient AtomicBoolean started = new AtomicBoolean(true);
77+
7578
private RouterImpl router;
7679

7780
private ExecutionMode mode;
@@ -599,8 +602,8 @@ public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
599602
}
600603

601604
/**
602-
* Callback method that indicates application was successfully started it and it is ready
603-
* listening for connections.
605+
* Callback method that indicates application was successfully started it and listening for
606+
* connections.
604607
*
605608
* @param server Server.
606609
* @return This application.
@@ -633,19 +636,32 @@ public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
633636
/**
634637
* Stop application, fire the stop event to cleanup resources.
635638
*
639+
* This method is usually invoked by {@link Server#stop()} using a shutdown hook.
640+
*
641+
* The next example shows how to successfully stop the web server and application:
642+
*
643+
* <pre>{@code
644+
* Jooby app = new Jooby();
645+
*
646+
* Server server = app.start();
647+
*
648+
* ...
649+
*
650+
* server.stop();
651+
* }</pre>
652+
*
636653
* @return This application.
637654
*/
638655
public @Nonnull Jooby stop() {
639-
Logger log = getLog();
640-
log.debug("Stopping {}", System.getProperty(APP_NAME, getClass().getSimpleName()));
641-
if (router != null) {
656+
if (started.compareAndSet(true, false)) {
657+
Logger log = getLog();
658+
log.debug("Stopping {}", System.getProperty(APP_NAME, getClass().getSimpleName()));
642659
router.destroy();
643-
router = null;
644-
}
645660

646-
fireStop();
661+
fireStop();
647662

648-
log.info("Stopped {}", System.getProperty(APP_NAME, getClass().getSimpleName()));
663+
log.info("Stopped {}", System.getProperty(APP_NAME, getClass().getSimpleName()));
664+
}
649665
return this;
650666
}
651667

@@ -665,7 +681,7 @@ public Jooby setLateInit(boolean lateInit) {
665681
}
666682

667683
@Override public String toString() {
668-
return router.toString();
684+
return getClass().getSimpleName();
669685
}
670686

671687
/**

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ public void destroy() {
435435
routes.clear();
436436
routes = null;
437437
chi.destroy();
438-
chi = null;
439438
if (errorCodes != null) {
440439
errorCodes.clear();
441440
errorCodes = null;
@@ -524,14 +523,16 @@ public void destroy() {
524523

525524
@Override public String toString() {
526525
StringBuilder buff = new StringBuilder();
527-
int size = IntStream.range(0, routes.size())
528-
.map(i -> routes.get(i).getMethod().length() + 1)
529-
.max()
530-
.orElse(0);
531-
532-
routes.forEach(
533-
r -> buff.append(String.format("\n %-" + size + "s", r.getMethod()))
534-
.append(r.getPattern()));
526+
if (routes != null) {
527+
int size = IntStream.range(0, routes.size())
528+
.map(i -> routes.get(i).getMethod().length() + 1)
529+
.max()
530+
.orElse(0);
531+
532+
routes.forEach(
533+
r -> buff.append(String.format("\n %-" + size + "s", r.getMethod()))
534+
.append(r.getPattern()));
535+
}
535536
return buff.length() > 0 ? buff.substring(1) : "";
536537
}
537538

modules/jooby-jetty/src/main/java/io/jooby/jetty/Jetty.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,16 @@ public class Jetty extends io.jooby.Server.Base {
115115
return this;
116116
}
117117

118-
@Nonnull @Override public io.jooby.Server stop() {
118+
@Nonnull @Override public synchronized io.jooby.Server stop() {
119119
fireStop(applications);
120120
if (server != null) {
121121
try {
122122
server.stop();
123123
} catch (Exception x) {
124124
throw SneakyThrows.propagate(x);
125+
} finally {
126+
server = null;
125127
}
126-
server = null;
127128
}
128129
return this;
129130
}

modules/jooby-netty/src/main/java/io/jooby/netty/Netty.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ public class Netty extends Server.Base {
127127
return this;
128128
}
129129

130-
@Nonnull @Override public Server stop() {
130+
@Nonnull @Override public synchronized Server stop() {
131+
System.out.println("ssss");
131132
fireStop(applications);
132-
applications = null;
133133
if (acceptor != null) {
134134
acceptor.shutdownGracefully();
135135
acceptor = null;

modules/jooby-utow/src/main/java/io/jooby/utow/Utow.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@ public class Utow extends Server.Base {
102102
}
103103
}
104104

105-
@Nonnull @Override public Server stop() {
105+
@Nonnull @Override public synchronized Server stop() {
106106
fireStop(applications);
107107
applications = null;
108108
if (server != null) {
109-
server.stop();
110-
server = null;
109+
try {
110+
server.stop();
111+
} finally {
112+
server = null;
113+
}
111114
}
112115
return this;
113116
}

0 commit comments

Comments
 (0)