• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.services;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import io.grpc.BindableService;
22 import io.grpc.health.v1.HealthCheckResponse.ServingStatus;
23 
24 /**
25  * A {@code HealthStatusManager} object manages a health check service. A health check service is
26  * created in the constructor of {@code HealthStatusManager}, and it can be retrieved by the
27  * {@link #getHealthService()} method.
28  * The health status manager can update the health statuses of the server.
29  */
30 @io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/4696")
31 public final class HealthStatusManager {
32 
33   private final HealthServiceImpl healthService;
34 
35   /**
36    * Creates a new health service instance.
37    */
HealthStatusManager()38   public HealthStatusManager() {
39     healthService = new HealthServiceImpl();
40   }
41 
42   /**
43    * Gets the health check service created in the constructor.
44    */
getHealthService()45   public BindableService getHealthService() {
46     return healthService;
47   }
48 
49   /**
50    * Updates the status of the server.
51    * @param service the name of some aspect of the server that is associated with a health status.
52    *     This name can have no relation with the gRPC services that the server is running with.
53    *     It can also be an empty String {@code ""} per the gRPC specification.
54    * @param status is one of the values {@link ServingStatus#SERVING},
55    *     {@link ServingStatus#NOT_SERVING} and {@link ServingStatus#UNKNOWN}.
56    */
setStatus(String service, ServingStatus status)57   public void setStatus(String service, ServingStatus status) {
58     checkNotNull(status, "status");
59     healthService.setStatus(service, status);
60   }
61 
62   /**
63    * Clears the health status record of a service. The health service will respond with NOT_FOUND
64    * error on checking the status of a cleared service.
65    * @param service the name of some aspect of the server that is associated with a health status.
66    *     This name can have no relation with the gRPC services that the server is running with.
67    *     It can also be an empty String {@code ""} per the gRPC specification.
68    */
clearStatus(String service)69   public void clearStatus(String service) {
70     healthService.clearStatus(service);
71   }
72 }
73