Skip to content

Commit c0757ef

Browse files
committed
CAMEL-19648: readiness/liveness probes
1 parent 9c5f25c commit c0757ef

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

core/camel-spring-boot/src/main/docs/spring-boot.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,24 @@ implementing your custom https://docs.spring.io/spring-boot/docs/current/referen
416416
or by providing GraalVM JSON hint files that can be generated by the https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html#native-image.advanced.using-the-tracing-agent[Tracing Agent].
417417

418418
For more details about `GraalVM Native Image Support` in Spring Boot please refer to https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
419+
420+
== Camel Readiness and Liveness State Indicators
421+
422+
Camel specific Readiness and Liveness checks can be added to a Spring Boot 3 application including respectively in the
423+
readiness and livenss groups camelLivenessStateHealthIndicator and camelReadinessStateHealthIndicator. In particular:
424+
425+
[source,properties]
426+
----
427+
management.endpoint.health.group.liveness.include=livenessState,camelLivenessState
428+
management.endpoint.health.group.readiness.include=readinessState,camelReadinessState
429+
----
430+
431+
Using Camel specific readiness and liveness health indicators, the probes will be augmented with camel components
432+
health checks that support this feature. In enable the probes locally, they need to be enabled
433+
434+
[source,properties]
435+
----
436+
management.endpoint.health.probes.enabled=true
437+
----
438+
439+
Finally, http://localhost:8080/actuator/health/liveness will show the updated probe.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.spring.boot.actuate.health;
18+
19+
import org.apache.camel.CamelContext;
20+
import org.apache.camel.spring.boot.CamelAutoConfiguration;
21+
import org.apache.camel.spring.boot.actuate.health.liveness.CamelLivenessStateHealthIndicator;
22+
import org.apache.camel.spring.boot.actuate.health.readiness.CamelReadinessStateHealthIndicator;
23+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
24+
import org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.availability.ApplicationAvailability;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
31+
@Configuration(proxyBeanMethods = false)
32+
@AutoConfigureAfter({CamelAutoConfiguration.class, ApplicationAvailabilityAutoConfiguration.class})
33+
@ConditionalOnBean(CamelAutoConfiguration.class)
34+
public class CamelAvailabilityCheckAutoConfiguration {
35+
36+
@Bean
37+
@ConditionalOnMissingBean(CamelLivenessStateHealthIndicator.class)
38+
public CamelLivenessStateHealthIndicator camelLivenessStateHealthIndicator(ApplicationAvailability applicationAvailability,
39+
CamelContext camelContext) {
40+
return new CamelLivenessStateHealthIndicator(applicationAvailability, camelContext);
41+
}
42+
43+
@Bean
44+
@ConditionalOnMissingBean({CamelReadinessStateHealthIndicator.class})
45+
public CamelReadinessStateHealthIndicator camelReadinessStateHealthIndicator(
46+
ApplicationAvailability applicationAvailability,
47+
CamelContext camelContext) {
48+
return new CamelReadinessStateHealthIndicator(applicationAvailability, camelContext);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.spring.boot.actuate.health.liveness;
18+
19+
import org.apache.camel.CamelContext;
20+
import org.apache.camel.health.HealthCheck;
21+
import org.apache.camel.health.HealthCheckHelper;
22+
import org.apache.camel.spring.boot.actuate.health.readiness.CamelReadinessStateHealthIndicator;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
import org.springframework.boot.actuate.availability.LivenessStateHealthIndicator;
26+
import org.springframework.boot.availability.ApplicationAvailability;
27+
import org.springframework.boot.availability.AvailabilityState;
28+
import org.springframework.boot.availability.LivenessState;
29+
30+
import java.util.Collection;
31+
32+
public class CamelLivenessStateHealthIndicator extends LivenessStateHealthIndicator {
33+
34+
private static final Logger LOG = LoggerFactory.getLogger(CamelLivenessStateHealthIndicator.class);
35+
36+
private CamelContext camelContext;
37+
38+
public CamelLivenessStateHealthIndicator(
39+
ApplicationAvailability availability,
40+
CamelContext camelContext) {
41+
super(availability);
42+
43+
this.camelContext = camelContext;
44+
}
45+
46+
@Override
47+
protected AvailabilityState getState(ApplicationAvailability applicationAvailability) {
48+
Collection<HealthCheck.Result> results = HealthCheckHelper.invokeLiveness(camelContext);
49+
50+
boolean isLive = CamelReadinessStateHealthIndicator.checkState(results, LOG);
51+
52+
return isLive ?
53+
LivenessState.CORRECT : LivenessState.BROKEN;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.spring.boot.actuate.health.readiness;
18+
19+
import org.apache.camel.CamelContext;
20+
import org.apache.camel.health.HealthCheck;
21+
import org.apache.camel.health.HealthCheckHelper;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import org.springframework.boot.actuate.availability.ReadinessStateHealthIndicator;
25+
import org.springframework.boot.availability.ApplicationAvailability;
26+
import org.springframework.boot.availability.AvailabilityState;
27+
import org.springframework.boot.availability.ReadinessState;
28+
29+
import java.util.Collection;
30+
31+
public class CamelReadinessStateHealthIndicator extends ReadinessStateHealthIndicator {
32+
33+
private static final Logger LOG = LoggerFactory.getLogger(CamelReadinessStateHealthIndicator.class);
34+
35+
private CamelContext camelContext;
36+
37+
public CamelReadinessStateHealthIndicator(
38+
ApplicationAvailability availability,
39+
CamelContext camelContext) {
40+
super(availability);
41+
42+
this.camelContext = camelContext;
43+
}
44+
45+
@Override
46+
protected AvailabilityState getState(ApplicationAvailability applicationAvailability) {
47+
Collection<HealthCheck.Result> results = HealthCheckHelper.invokeReadiness(camelContext);
48+
49+
boolean isReady = checkState(results, LOG);
50+
51+
return isReady ?
52+
ReadinessState.ACCEPTING_TRAFFIC : ReadinessState.REFUSING_TRAFFIC;
53+
}
54+
55+
public static boolean checkState(Collection<HealthCheck.Result> results, Logger log) {
56+
boolean isUp = true;
57+
for (HealthCheck.Result result : results) {
58+
if (!HealthCheck.State.UP.equals(result.getState())) {
59+
isUp = false;
60+
61+
result.getError().ifPresent(error -> log.warn(result.getCheck().getId(), error));
62+
}
63+
}
64+
65+
return isUp;
66+
}
67+
}

core/camel-spring-boot/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ org.apache.camel.spring.boot.actuate.console.CamelDevConsoleAutoConfiguration
2020
org.apache.camel.spring.boot.actuate.endpoint.CamelRouteControllerEndpointAutoConfiguration
2121
org.apache.camel.spring.boot.actuate.endpoint.CamelRoutesEndpointAutoConfiguration
2222
org.apache.camel.spring.boot.actuate.health.CamelHealthCheckAutoConfiguration
23+
org.apache.camel.spring.boot.actuate.health.CamelAvailabilityCheckAutoConfiguration
2324
org.apache.camel.spring.boot.actuate.info.CamelInfoAutoConfiguration
2425
org.apache.camel.spring.boot.cloud.CamelCloudAutoConfiguration
2526
org.apache.camel.spring.boot.cloud.CamelCloudServiceCallConfigurationAutoConfiguration

0 commit comments

Comments
 (0)