Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 4368999

Browse files
committed
add app param to start/stop callback jooby-project#357
1 parent a0ace50 commit 4368999

3 files changed

Lines changed: 62 additions & 11 deletions

File tree

jooby-jdbc/src/test/java/org/jooby/jdbc/JdbcTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import com.zaxxer.hikari.HikariConfig;
3838

3939
import javaslang.control.Try.CheckedConsumer;
40-
import javaslang.control.Try.CheckedRunnable;
4140

4241
public class JdbcTest {
4342

@@ -1059,7 +1058,7 @@ public Routes routes() throws UnsupportedOperationException {
10591058
}
10601059

10611060
@Override
1062-
public Env onStop(final CheckedRunnable shutdownTask) {
1061+
public Env onStop(final CheckedConsumer<Jooby> shutdownTask) {
10631062
throw new UnsupportedOperationException();
10641063
}
10651064

@@ -1069,7 +1068,7 @@ public List<CheckedConsumer<Jooby>> stopTasks() {
10691068
}
10701069

10711070
@Override
1072-
public Env onStart(final CheckedRunnable task) {
1071+
public Env onStart(final CheckedConsumer<Jooby> task) {
10731072
throw new UnsupportedOperationException();
10741073
}
10751074

jooby/src/main/java/org/jooby/Env.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ public List<CheckedConsumer<Jooby>> stopTasks() {
140140
}
141141

142142
@Override
143-
public Env onStop(final CheckedRunnable task) {
144-
this.shutdown.add(e -> task.run());
143+
public Env onStop(final CheckedConsumer<Jooby> task) {
144+
this.shutdown.add(task);
145145
return this;
146146
}
147147

148148
@Override
149-
public Env onStart(final CheckedRunnable task) {
150-
this.start.add(e -> task.run());
149+
public Env onStart(final CheckedConsumer<Jooby> task) {
150+
this.start.add(task);
151151
return this;
152152
}
153153

@@ -373,7 +373,37 @@ default <T> Option<T> when(final Predicate<String> predicate, final T result) {
373373
* @param task Task to run.
374374
* @return This env.
375375
*/
376-
Env onStart(CheckedRunnable task);
376+
Env onStart(CheckedConsumer<Jooby> task);
377+
378+
/**
379+
* Add a start task, useful for initialize and/or start services at startup time.
380+
*
381+
* You should ONLY call this method while the application is been initialized, usually executing
382+
* {@link Jooby.Module#configure(Env, Config, com.google.inject.Binder)}.
383+
*
384+
* The behaviour of this method once application has been initialized is <code>undefined</code>.
385+
*
386+
* @param task Task to run.
387+
* @return This env.
388+
*/
389+
default Env onStart(final CheckedRunnable task) {
390+
return onStart(app -> task.run());
391+
}
392+
393+
/**
394+
* Add a stop task, useful for cleanup and/or stop service at stop time.
395+
*
396+
* You should ONLY call this method while the application is been initialized, usually executing
397+
* {@link Jooby.Module#configure(Env, Config, com.google.inject.Binder)}.
398+
*
399+
* The behaviour of this method once application has been initialized is <code>undefined</code>.
400+
*
401+
* @param task Task to run.
402+
* @return This env.
403+
*/
404+
default Env onStop(final CheckedRunnable task) {
405+
return onStop(app -> task.run());
406+
}
377407

378408
/**
379409
* Add a stop task, useful for cleanup and/or stop service at stop time.
@@ -386,7 +416,7 @@ default <T> Option<T> when(final Predicate<String> predicate, final T result) {
386416
* @param task Task to run.
387417
* @return This env.
388418
*/
389-
Env onStop(CheckedRunnable task);
419+
Env onStop(CheckedConsumer<Jooby> task);
390420

391421
/**
392422
* @return List of start tasks.

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,18 @@ public Jooby env(final Env.Builder env) {
711711
*/
712712
public Jooby onStart(final CheckedRunnable callback) {
713713
requireNonNull(callback, "Callback is required.");
714-
onStart.add(e -> callback.run());
714+
return onStart(a -> callback.run());
715+
}
716+
717+
/**
718+
* Run code at application startup time.
719+
*
720+
* @param callback A callback to run.
721+
* @return This instance.
722+
*/
723+
public Jooby onStart(final CheckedConsumer<Jooby> callback) {
724+
requireNonNull(callback, "Callback is required.");
725+
onStart.add(callback);
715726
return this;
716727
}
717728

@@ -723,7 +734,18 @@ public Jooby onStart(final CheckedRunnable callback) {
723734
*/
724735
public Jooby onStop(final CheckedRunnable callback) {
725736
requireNonNull(callback, "Callback is required.");
726-
onStop.add(e -> callback.run());
737+
return onStop(e -> callback.run());
738+
}
739+
740+
/**
741+
* Run code at application shutdown time.
742+
*
743+
* @param callback A callback to run.
744+
* @return This instance.
745+
*/
746+
public Jooby onStop(final CheckedConsumer<Jooby> callback) {
747+
requireNonNull(callback, "Callback is required.");
748+
onStop.add(callback);
727749
return this;
728750
}
729751

0 commit comments

Comments
 (0)