public interface Service
This interface denotes a “Ratpack service”, which is something that gets notified of server start and stop. It has no further application level semantics (e.g. the database service).
When the application starts, all services in the server registry will be notified. Similarly when the application stops.
import ratpack.server.RatpackServer;
import ratpack.server.ServerConfig;
import ratpack.server.Service;
import ratpack.server.StartEvent;
import ratpack.server.StopEvent;
import ratpack.test.http.TestHttpClient;
import java.util.List;
import java.util.LinkedList;
import static org.junit.Assert.*;
public class Example {
static class RecordingService implements Service {
public List<String> events = new LinkedList<>();
public void onStart(StartEvent startEvent) { events.add("start"); }
public void onStop(StopEvent startEvent) { events.add("stop"); }
}
public static void main(String... args) throws Exception {
RecordingService service = new RecordingService();
RatpackServer server = RatpackServer.of(s -> s
.serverConfig(ServerConfig.embedded())
.registryOf(r -> r.add(service))
.handler(r ->
ctx -> ctx.render("ok")
)
);
assertEquals("[]", service.events.toString());
server.start();
assertEquals("[start]", service.events.toString());
server.reload();
assertEquals("[start, stop, start]", service.events.toString());
server.stop();
assertEquals("[start, stop, start, stop]", service.events.toString());
}
}
Modifier and Type | Method and Description |
---|---|
default void |
onStart(StartEvent event)
Server startup event.
|
default void |
onStop(StopEvent event)
Server stop event.
|
default void onStart(StartEvent event) throws Exception
event
- meta information about the startup eventException
- any