0
# Health Monitoring
1
2
Spring Boot Actuator's health monitoring system provides comprehensive health checking capabilities with built-in indicators for databases, messaging systems, and infrastructure components.
3
4
## Capabilities
5
6
### Core Health API
7
8
Basic health checking interfaces for both blocking and reactive implementations.
9
10
```java { .api }
11
/**
12
* Strategy interface used to provide an indication of application health
13
*/
14
public interface HealthIndicator extends HealthContributor {
15
/**
16
* Return an indication of health
17
* @param includeDetails if details should be included or removed
18
* @return the health
19
* @since 2.2.0
20
*/
21
default Health getHealth(boolean includeDetails) {
22
Health health = health();
23
return includeDetails ? health : health.withoutDetails();
24
}
25
26
/**
27
* Return an indication of health
28
* @return the health for the application
29
*/
30
Health health();
31
}
32
33
/**
34
* Reactive variant of HealthIndicator
35
*/
36
public interface ReactiveHealthIndicator {
37
/**
38
* Return a Mono that provides an indication of health
39
* @return a Mono that provides the health for the application
40
*/
41
Mono<Health> health();
42
}
43
44
/**
45
* Strategy interface used to contribute Health to the results returned by HealthEndpoint
46
*/
47
public interface HealthContributor {
48
// Marker interface for health contributors
49
}
50
```
51
52
**Usage Example:**
53
54
```java
55
@Component
56
public class DatabaseHealthIndicator implements HealthIndicator {
57
58
private final DataSource dataSource;
59
60
public DatabaseHealthIndicator(DataSource dataSource) {
61
this.dataSource = dataSource;
62
}
63
64
@Override
65
public Health health() {
66
try (Connection connection = dataSource.getConnection()) {
67
if (connection.isValid(5)) {
68
return Health.up()
69
.withDetail("database", "PostgreSQL")
70
.withDetail("status", "Connection successful")
71
.build();
72
} else {
73
return Health.down()
74
.withDetail("error", "Invalid connection")
75
.build();
76
}
77
} catch (SQLException e) {
78
return Health.down()
79
.withDetail("error", e.getMessage())
80
.withException(e)
81
.build();
82
}
83
}
84
}
85
```
86
87
### Health Builder and Status
88
89
Fluent API for building health responses with status and details.
90
91
```java { .api }
92
/**
93
* Carries information about the health of a component or subsystem
94
*/
95
public final class Health {
96
97
/**
98
* Create a new Builder instance with an UP status
99
*/
100
public static Builder up();
101
102
/**
103
* Create a new Builder instance with a DOWN status
104
*/
105
public static Builder down();
106
107
/**
108
* Create a new Builder instance with an UNKNOWN status
109
*/
110
public static Builder unknown();
111
112
/**
113
* Create a new Builder instance with an OUT_OF_SERVICE status
114
*/
115
public static Builder outOfService();
116
117
/**
118
* Create a new Builder instance with a specific status
119
*/
120
public static Builder status(Status status);
121
122
public Status getStatus();
123
public Map<String, Object> getDetails();
124
125
/**
126
* Return a new instance of this Health with all details removed
127
* @return a new instance without details
128
* @since 2.2.0
129
*/
130
Health withoutDetails();
131
132
/**
133
* Builder for Health instances
134
*/
135
public static class Builder {
136
/**
137
* Record detail using given key and value
138
*/
139
public Builder withDetail(String key, Object value);
140
141
/**
142
* Record details from given map
143
*/
144
public Builder withDetails(Map<String, ?> details);
145
146
/**
147
* Record exception details
148
*/
149
public Builder withException(Throwable ex);
150
151
/**
152
* Set status for the health being built
153
*/
154
public Builder status(Status status);
155
156
/**
157
* Set status using string code
158
*/
159
public Builder status(String statusCode);
160
161
/**
162
* Create the Health instance
163
*/
164
public Health build();
165
166
/**
167
* Create Health instance with UP status
168
*/
169
public Health up();
170
171
/**
172
* Create Health instance with DOWN status
173
*/
174
public Health down();
175
176
/**
177
* Create Health instance with UNKNOWN status
178
*/
179
public Health unknown();
180
181
/**
182
* Create Health instance with OUT_OF_SERVICE status
183
*/
184
public Health outOfService();
185
}
186
}
187
188
/**
189
* Health status with code and description
190
*/
191
public final class Status {
192
public static final Status UP = new Status("UP");
193
public static final Status DOWN = new Status("DOWN");
194
public static final Status OUT_OF_SERVICE = new Status("OUT_OF_SERVICE");
195
public static final Status UNKNOWN = new Status("UNKNOWN");
196
197
public Status(String code);
198
public Status(String code, String description);
199
200
public String getCode();
201
public String getDescription();
202
203
@Override
204
public boolean equals(Object obj);
205
206
@Override
207
public int hashCode();
208
209
@Override
210
public String toString();
211
}
212
```
213
214
### Database Health Indicators
215
216
Built-in health indicators for various database technologies.
217
218
```java { .api }
219
/**
220
* Health indicator for JDBC DataSource
221
*/
222
public class DataSourceHealthIndicator extends AbstractHealthIndicator {
223
public DataSourceHealthIndicator(DataSource dataSource);
224
public DataSourceHealthIndicator(DataSource dataSource, String query);
225
226
@Override
227
protected void doHealthCheck(Health.Builder builder) throws Exception;
228
}
229
230
/**
231
* Health indicator for JDBC connections
232
*/
233
public class JdbcHealthIndicator extends AbstractDbHealthIndicator {
234
public JdbcHealthIndicator(DataSource dataSource);
235
public JdbcHealthIndicator(DataSource dataSource, String query);
236
}
237
238
/**
239
* Health indicator for R2DBC reactive database connections
240
*/
241
public class R2dbcHealthIndicator implements ReactiveHealthIndicator {
242
public R2dbcHealthIndicator(ConnectionFactory connectionFactory);
243
public R2dbcHealthIndicator(ConnectionFactory connectionFactory, String query);
244
245
@Override
246
public Mono<Health> health();
247
}
248
249
/**
250
* Health indicator for Neo4j graph database
251
*/
252
public class Neo4jHealthIndicator extends AbstractHealthIndicator {
253
public Neo4jHealthIndicator(Driver driver);
254
255
@Override
256
protected void doHealthCheck(Health.Builder builder) throws Exception;
257
}
258
259
/**
260
* Reactive health indicator for Neo4j
261
*/
262
public class Neo4jReactiveHealthIndicator implements ReactiveHealthIndicator {
263
public Neo4jReactiveHealthIndicator(Driver driver);
264
265
@Override
266
public Mono<Health> health();
267
}
268
269
/**
270
* Health indicator for MongoDB
271
*/
272
public class MongoHealthIndicator extends AbstractHealthIndicator {
273
public MongoHealthIndicator(MongoTemplate mongoTemplate);
274
275
@Override
276
protected void doHealthCheck(Health.Builder builder) throws Exception;
277
}
278
279
/**
280
* Reactive health indicator for MongoDB
281
*/
282
public class MongoReactiveHealthIndicator implements ReactiveHealthIndicator {
283
public MongoReactiveHealthIndicator(ReactiveMongoTemplate reactiveMongoTemplate);
284
285
@Override
286
public Mono<Health> health();
287
}
288
289
/**
290
* Health indicator for Cassandra
291
*/
292
public class CassandraHealthIndicator extends AbstractHealthIndicator {
293
public CassandraHealthIndicator(CqlSession session);
294
295
@Override
296
protected void doHealthCheck(Health.Builder builder) throws Exception;
297
}
298
299
/**
300
* Reactive health indicator for Cassandra
301
*/
302
public class CassandraReactiveHealthIndicator implements ReactiveHealthIndicator {
303
public CassandraReactiveHealthIndicator(ReactiveCqlTemplate reactiveCqlTemplate);
304
305
@Override
306
public Mono<Health> health();
307
}
308
```
309
310
### NoSQL and Cache Health Indicators
311
312
Health indicators for NoSQL databases and caching systems.
313
314
```java { .api }
315
/**
316
* Health indicator for Redis
317
*/
318
public class RedisHealthIndicator extends AbstractHealthIndicator {
319
public RedisHealthIndicator(RedisConnectionFactory redisConnectionFactory);
320
321
@Override
322
protected void doHealthCheck(Health.Builder builder) throws Exception;
323
}
324
325
/**
326
* Reactive health indicator for Redis
327
*/
328
public class RedisReactiveHealthIndicator implements ReactiveHealthIndicator {
329
public RedisReactiveHealthIndicator(ReactiveRedisTemplate<?, ?> redisTemplate);
330
331
@Override
332
public Mono<Health> health();
333
}
334
335
/**
336
* Health indicator for Elasticsearch
337
*/
338
public class ElasticsearchHealthIndicator extends AbstractHealthIndicator {
339
public ElasticsearchHealthIndicator(ElasticsearchClient client);
340
341
@Override
342
protected void doHealthCheck(Health.Builder builder) throws Exception;
343
}
344
345
/**
346
* Reactive health indicator for Elasticsearch
347
*/
348
public class ElasticsearchReactiveHealthIndicator implements ReactiveHealthIndicator {
349
public ElasticsearchReactiveHealthIndicator(ReactiveElasticsearchClient client);
350
351
@Override
352
public Mono<Health> health();
353
}
354
355
/**
356
* Health indicator for Couchbase
357
*/
358
public class CouchbaseHealthIndicator extends AbstractHealthIndicator {
359
public CouchbaseHealthIndicator(Cluster cluster);
360
361
@Override
362
protected void doHealthCheck(Health.Builder builder) throws Exception;
363
}
364
365
/**
366
* Reactive health indicator for Couchbase
367
*/
368
public class CouchbaseReactiveHealthIndicator implements ReactiveHealthIndicator {
369
public CouchbaseReactiveHealthIndicator(Cluster cluster);
370
371
@Override
372
public Mono<Health> health();
373
}
374
375
/**
376
* Health indicator for Hazelcast
377
*/
378
public class HazelcastHealthIndicator extends AbstractHealthIndicator {
379
public HazelcastHealthIndicator(HazelcastInstance hazelcastInstance);
380
381
@Override
382
protected void doHealthCheck(Health.Builder builder) throws Exception;
383
}
384
385
/**
386
* Health indicator for InfluxDB
387
*/
388
public class InfluxDbHealthIndicator extends AbstractHealthIndicator {
389
public InfluxDbHealthIndicator(InfluxDB influxDb);
390
391
@Override
392
protected void doHealthCheck(Health.Builder builder) throws Exception;
393
}
394
```
395
396
### Messaging Health Indicators
397
398
Health indicators for messaging systems and mail servers.
399
400
```java { .api }
401
/**
402
* Health indicator for RabbitMQ
403
*/
404
public class RabbitHealthIndicator extends AbstractHealthIndicator {
405
public RabbitHealthIndicator(RabbitTemplate rabbitTemplate);
406
407
@Override
408
protected void doHealthCheck(Health.Builder builder) throws Exception;
409
}
410
411
/**
412
* Health indicator for JMS messaging
413
*/
414
public class JmsHealthIndicator extends AbstractHealthIndicator {
415
public JmsHealthIndicator(ConnectionFactory connectionFactory);
416
417
@Override
418
protected void doHealthCheck(Health.Builder builder) throws Exception;
419
}
420
421
/**
422
* Health indicator for mail servers
423
*/
424
public class MailHealthIndicator extends AbstractHealthIndicator {
425
public MailHealthIndicator(JavaMailSenderImpl mailSender);
426
427
@Override
428
protected void doHealthCheck(Health.Builder builder) throws Exception;
429
}
430
```
431
432
### Infrastructure Health Indicators
433
434
Health indicators for infrastructure components and system resources.
435
436
```java { .api }
437
/**
438
* Health indicator for LDAP servers
439
*/
440
public class LdapHealthIndicator extends AbstractHealthIndicator {
441
public LdapHealthIndicator(LdapTemplate ldapTemplate);
442
443
@Override
444
protected void doHealthCheck(Health.Builder builder) throws Exception;
445
}
446
447
/**
448
* Simple ping health indicator
449
*/
450
public class PingHealthIndicator implements HealthIndicator {
451
@Override
452
public Health health();
453
}
454
455
/**
456
* Health indicator for disk space
457
*/
458
public class DiskSpaceHealthIndicator extends AbstractHealthIndicator {
459
public DiskSpaceHealthIndicator(File path, long threshold);
460
461
@Override
462
protected void doHealthCheck(Health.Builder builder) throws Exception;
463
}
464
465
/**
466
* Health indicator for SSL certificates
467
*/
468
public class SslHealthIndicator extends AbstractHealthIndicator {
469
public SslHealthIndicator(SslBundles sslBundles, Duration certificateValidityWarningThreshold);
470
471
@Override
472
protected void doHealthCheck(Health.Builder builder) throws Exception;
473
}
474
```
475
476
### Application State Health Indicators
477
478
Health indicators for Kubernetes and application availability states.
479
480
```java { .api }
481
/**
482
* Health indicator for liveness state (Kubernetes liveness probe)
483
*/
484
public class LivenessStateHealthIndicator implements HealthIndicator {
485
public LivenessStateHealthIndicator(ApplicationAvailability applicationAvailability);
486
487
@Override
488
public Health health();
489
}
490
491
/**
492
* Health indicator for readiness state (Kubernetes readiness probe)
493
*/
494
public class ReadinessStateHealthIndicator implements HealthIndicator {
495
public ReadinessStateHealthIndicator(ApplicationAvailability applicationAvailability);
496
497
@Override
498
public Health health();
499
}
500
501
/**
502
* Base health indicator for availability states
503
*/
504
public class AvailabilityStateHealthIndicator implements HealthIndicator {
505
public AvailabilityStateHealthIndicator(ApplicationAvailability applicationAvailability,
506
AvailabilityState failureState,
507
String statusDescription);
508
509
@Override
510
public Health health();
511
}
512
```
513
514
### Health Endpoint
515
516
Built-in endpoint for exposing health information.
517
518
```java { .api }
519
/**
520
* Endpoint to expose application health information
521
*/
522
@Endpoint(id = "health")
523
public class HealthEndpoint {
524
public HealthEndpoint(HealthContributorRegistry registry, HealthEndpointGroups groups);
525
526
@ReadOperation
527
public HealthComponent health();
528
529
@ReadOperation
530
public HealthComponent healthForPath(@Selector String... path);
531
}
532
533
/**
534
* Base class for health components
535
*/
536
public abstract class HealthComponent {
537
public abstract Status getStatus();
538
}
539
540
/**
541
* Registry for health contributors
542
*/
543
public interface HealthContributorRegistry extends NamedContributorRegistry<HealthContributor> {
544
void registerContributor(String name, HealthContributor contributor);
545
HealthContributor unregisterContributor(String name);
546
HealthContributor getContributor(String name);
547
}
548
```
549
550
**Usage Example:**
551
552
```java
553
@Component
554
public class CustomHealthConfiguration {
555
556
@Bean
557
public HealthIndicator customServiceHealthIndicator() {
558
return () -> {
559
boolean serviceAvailable = checkServiceAvailability();
560
if (serviceAvailable) {
561
return Health.up()
562
.withDetail("service", "custom-service")
563
.withDetail("version", "2.1.0")
564
.withDetail("connections", getActiveConnections())
565
.build();
566
} else {
567
return Health.down()
568
.withDetail("service", "custom-service")
569
.withDetail("error", "Service unavailable")
570
.build();
571
}
572
};
573
}
574
575
@Bean
576
public ReactiveHealthIndicator reactiveCustomHealthIndicator() {
577
return () -> checkServiceHealthReactively()
578
.map(healthy -> healthy ?
579
Health.up().withDetail("reactive", true).build() :
580
Health.down().withDetail("reactive", false).build()
581
);
582
}
583
}
584
```