Class Health
Liveness answers "should this process be restarted?". With no liveness checks
it is up whenever the process can answer. Readiness answers "should this
instance receive traffic?". It is down while the application is not running, once beginDrain() was called, and while any readiness check is unhealthy. A prober should stop
sending traffic on a failing readiness probe and restart only on a failing liveness probe.
var health = Health.builder(app)
.readiness("database", () -> pool.isValid())
.build();
health.register(app, security.hasRole("ops")); // GET /health/live and GET /health/ready
// on SIGTERM: health.beginDrain(); wait for the balancer to notice; app.close();
Exposure. The routes answer without authentication unless the middleware
passed to register(com.jsgalactic.axiom.routing.RouteGroup, com.jsgalactic.axiom.context.Middleware...) requires it. Check names and states are operational detail: do not
expose them to the public internet without security middleware or a private listener.
Startup. The startup probe answers "has this instance finished starting?". It is DOWN until the application is running and every startup check passes, then UP for good: once it has succeeded no startup check runs again, so an expensive warm-up check costs nothing afterwards. An orchestrator holds back its liveness and readiness probes until it succeeds.
Draining. Building a Health registers beginDrain() with the
application (see Application.onDrain(java.lang.Runnable)), so a JVM shutdown hook opted into with Application.closeOnJvmShutdown(Duration) turns readiness DOWN first, waits the drain delay for
the balancer to notice, and only then closes the application and its listeners.
Caching. Health.Builder.cacheFor(java.time.Duration) keeps each check's result for a short time,
so a frequently polled expensive check runs at most once per interval however many probes arrive.
An instance is immutable apart from the drain flag, the startup latch and the result caches, and is thread-safe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classCollects checks beforeHealth.Builder.build().static final recordThe outcome of one probe.static enumOutcome of a check or probe. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationProbe timeout when none is configured.static final StringPath of the liveness route.static final DurationLongest time a check result may be cached.static final intMost checks per probe.static final DurationLongest probe timeout.static final StringPath of the readiness route.static final StringPath of the startup route. -
Method Summary
Modifier and TypeMethodDescriptionvoidMakes readiness report DOWN from now on, without running readiness checks.static Health.Builderbuilder(Application application) Starts building health for an application.booleanReports whetherbeginDrain()was called.liveness()Runs the liveness checks.Evaluates readiness: DOWN without running checks when draining or when the application is not running, otherwise the readiness checks.voidregister(RouteGroup routes, Middleware... middleware) RegistersGET /health/startup,GET /health/liveandGET /health/readyon an application or group (under its prefix).startup()Evaluates the startup probe: UP once the application is running and every startup check passes, and UP from then on without running the checks again.
-
Field Details
-
LIVE_PATH
Path of the liveness route.- See Also:
-
READY_PATH
Path of the readiness route.- See Also:
-
STARTUP_PATH
Path of the startup route.- See Also:
-
DEFAULT_TIMEOUT
Probe timeout when none is configured. -
MAX_TIMEOUT
Longest probe timeout. -
MAX_CHECKS
public static final int MAX_CHECKSMost checks per probe.- See Also:
-
MAX_CACHE
Longest time a check result may be cached.
-
-
Method Details
-
builder
Starts building health for an application.- Parameters:
application- the application whose lifecycle readiness follows- Returns:
- a builder
-
startup
Evaluates the startup probe: UP once the application is running and every startup check passes, and UP from then on without running the checks again. While the application is still being configured, or has closed before it ever started, the probe is DOWN without running them.- Returns:
- the report; its checks are those of the last evaluation that ran them
-
beginDrain
public void beginDrain()Makes readiness report DOWN from now on, without running readiness checks. Call it when shutdown begins, before closing the application or listener, so a balancer stops sending traffic while running requests finish. It cannot be undone and is idempotent. -
isDraining
public boolean isDraining()Reports whetherbeginDrain()was called.- Returns:
- true once draining
-
liveness
Runs the liveness checks.- Returns:
- UP when every check is up, or when there are none
-
readiness
Evaluates readiness: DOWN without running checks when draining or when the application is not running, otherwise the readiness checks.- Returns:
- the report
-
register
RegistersGET /health/startup,GET /health/liveandGET /health/readyon an application or group (under its prefix). Each answers aJSON reportwithCache-Control: no-store: 200 when up, 503 when down. HEAD works as for any GET route.- Parameters:
routes- where to registermiddleware- middleware applied to both routes, outermost first; pass authentication here- Throws:
IllegalStateException- if configuration has ended
-