forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGracefulShutdown.java
More file actions
47 lines (41 loc) · 1.25 KB
/
GracefulShutdown.java
File metadata and controls
47 lines (41 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import io.jooby.internal.GracefulShutdownHandler;
import javax.annotation.Nonnull;
import java.time.Duration;
/**
* Install a handler that at application shutdown time:
*
* - Waits for existing requests to finished with an optional timeout
* - Incoming requests are resolved as Service Unavailable(503)
*
* NOTE: This extension must be installed at very beginning of your route pipeline.
*
* @author edgar
*/
public class GracefulShutdown implements Extension {
private Duration await;
/**
* Creates a new shutdown handler and waits for existing requests to finish or for specified
* amount of time.
*
* @param await Max time to wait for handlers to complete.
*/
public GracefulShutdown(@Nonnull Duration await) {
this.await = await;
}
/**
* Creates a new shutdown handler and waits for existing request to finish.
*/
public GracefulShutdown() {
}
@Override public void install(@Nonnull Jooby application) throws Exception {
GracefulShutdownHandler handler = new GracefulShutdownHandler(await);
application.decorator(handler);
application.onStop(handler::shutdown);
}
}