public interface LifeCycle
Listen for application start and stop events. Useful for starting/stopping services.
Start/stop callbacks are accessible via application:
{
onStart(() -> {
log.info("starting app");
});
onStop(() -> {
log.info("stopping app");
});
}
Or via module:
public class MyModule implements Jooby.Module {
public void configure(Env env, Config conf, Binder binder) {
env.onStart(() -> {
log.info("starting module");
});
env.onStop(() -> {
log.info("stopping module");
});
}
}
Callback order is preserved:
{
onStart(() -> {
log.info("first");
});
onStart(() -> {
log.info("second");
});
onStart(() -> {
log.info("third");
});
}
Order is useful for service dependencies, like ServiceB should be started after ServiceA.
You can also request for a service and start or stop it:
{
onStart(registry -> {
MyService service = registry.require(MyService.class);
service.start();
});
onStop(registry -> {
MyService service = registry.require(MyService.class);
service.stop();
});
}
If you prefer the annotation way... you can too:
@Singleton
public class MyService {
@PostConstruct
public void start() {
}
@PreDestroy
public void stop() {
}
}
App.java:
{
lifeCycle(MyService.class);
}
It works as expected just make sure MyService
is a Singleton object.
Modifier and Type | Method and Description |
---|---|
default LifeCycle |
lifeCycle(Class<?> service)
Add to lifecycle the given service.
|
static Optional<org.jooby.funzy.Throwing.Consumer<Object>> |
lifeCycleAnnotation(Class<?> rawType, Class<? extends Annotation> annotation)
Find a single method annotated with the given annotation in the provided type.
|
LifeCycle |
onStart(org.jooby.funzy.Throwing.Consumer<Registry> task)
Add a start lifecycle event, useful for initialize and/or start services at startup time.
|
default LifeCycle |
onStart(org.jooby.funzy.Throwing.Runnable task)
Add a start lifecycle event, useful for initialize and/or start services at startup time.
|
LifeCycle |
onStarted(org.jooby.funzy.Throwing.Consumer<Registry> task)
Add a started lifecycle event.
|
default LifeCycle |
onStarted(org.jooby.funzy.Throwing.Runnable task)
Add a started lifecycle event.
|
LifeCycle |
onStop(org.jooby.funzy.Throwing.Consumer<Registry> task)
Add a stop lifecycle event, useful for cleanup and/or stop service at stop time.
|
default LifeCycle |
onStop(org.jooby.funzy.Throwing.Runnable task)
Add a stop lifecycle event, useful for cleanup and/or stop service at stop time.
|
static Optional<org.jooby.funzy.Throwing.Consumer<Object>> lifeCycleAnnotation(Class<?> rawType, Class<? extends Annotation> annotation)
rawType
- The type to look for a method.
annotation
- Annotation to look for.
@Nonnull default LifeCycle lifeCycle(Class<?> service)
PostConstruct
or PreDestroy
will be executed at application startup or shutdown time. The service must be a Singleton object.
@Singleton
public class MyService {
@PostConstruct
public void start() {
}
@PreDestroy
public void stop() {
}
}
App.java:
{
lifeCycle(MyService.class);
}
You should ONLY call this method while the application is been initialized or while Jooby.Module.configure(Env, Config, com.google.inject.Binder)
is been executed. The behavior of this method once application has been initialized is undefined
.
service
- Service type. Must be a singleton object.
@Nonnull LifeCycle onStart(org.jooby.funzy.Throwing.Consumer<Registry> task)
Jooby.Module.configure(Env, Config, com.google.inject.Binder)
. The behavior of this method once application has been initialized is undefined
.
task
- Task to run.
@Nonnull LifeCycle onStarted(org.jooby.funzy.Throwing.Consumer<Registry> task)
Jooby.Module.configure(Env, Config, com.google.inject.Binder)
. The behavior of this method once application has been initialized is undefined
.
task
- Task to run.
@Nonnull default LifeCycle onStart(org.jooby.funzy.Throwing.Runnable task)
Jooby.Module.configure(Env, Config, com.google.inject.Binder)
. The behavior of this method once application has been initialized is undefined
.
task
- Task to run.
@Nonnull default LifeCycle onStarted(org.jooby.funzy.Throwing.Runnable task)
Jooby.Module.configure(Env, Config, com.google.inject.Binder)
. The behavior of this method once application has been initialized is undefined
.
task
- Task to run.
@Nonnull default LifeCycle onStop(org.jooby.funzy.Throwing.Runnable task)
Jooby.Module.configure(Env, Config, com.google.inject.Binder)
. The behavior of this method once application has been initialized is undefined
.
task
- Task to run.
@Nonnull LifeCycle onStop(org.jooby.funzy.Throwing.Consumer<Registry> task)
Jooby.Module.configure(Env, Config, com.google.inject.Binder)
. The behaviour of this method once application has been initialized is undefined
.
task
- Task to run.
Copyright © 2019. All rights reserved.