A Jooby app looks like:
public class App extends Jooby { // 1.
{
// 2. add a route
get("/", () -> "Hello");
}
public static void main(String[] args) {
// 3. run my app
run(App::new, args);
}
}-
Create a new App extending Jooby
-
Define your application in the
instance initializer -
Run the application.
Application provides start and stop events. These events are useful for starting/stopping services.
Start/stop callbacks are accessible via application:
{
onStart(() -> {
log.info("starting app");
});
onStop(() -> {
log.info("stopping app");
});
onStarted(() -> {
log.info("app started");
});
}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");
});
env.onStarted(() -> {
log.info("app started");
});
}
}The onStart callbacks are part of bootstrap and executed before the server is ready. The onStarted callbacks are executed when the server is ready.
Modules are covered later all you need to know now is that you can start/stop module as you usually do from your application.
Callback order is preserved:
{
onStart(() -> {
log.info("first");
});
onStart(() -> {
log.info("second");
});
onStart(() -> {
log.info("third");
});
}Order is useful for service dependencies, for example if ServiceB should be started after ServiceA.
You have access to the the service registry from start/stop events:
{
onStart(registry -> {
MyService service = registry.require(MyService.class);
service.start();
});
onStop(registry -> {
MyService service = registry.require(MyService.class);
service.stop();
});
}If you prefer annotations you can do:
@Singleton
public class MyService {
@PostConstruct
public void start() {
// ...
}
@PreDestroy
public void stop() {
// ...
}
}
App.java:
{
lifeCycle(MyService.class);
}Service must be a Singleton object.