Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.codahale.metrics.health;
  
  import org.slf4j.Logger;
  
  import java.util.*;
  
  import static com.codahale.metrics.health.HealthCheck.Result;

A registry for health checks.
 
 public class HealthCheckRegistry {
     private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckRegistry.class);
 
     private final ConcurrentMap<StringHealthCheckhealthChecks;

    
Creates a new HealthCheckRegistry.
 
     public HealthCheckRegistry() {
         this. = new ConcurrentHashMap<StringHealthCheck>();
     }

    
Registers an application HealthCheck.

Parameters:
name the name of the health check
healthCheck the HealthCheck instance
 
     public void register(String nameHealthCheck healthCheck) {
         .putIfAbsent(namehealthCheck);
     }

    
Unregisters the application HealthCheck with the given name.

Parameters:
name the name of the HealthCheck instance
 
     public void unregister(String name) {
         .remove(name);
     }

    
Returns a set of the names of all registered health checks.

Returns:
the names of all registered health checks
 
     public SortedSet<StringgetNames() {
         return Collections.unmodifiableSortedSet(new TreeSet<String>(.keySet()));
     }

    
Runs the health check with the given name.

Parameters:
name the health check's name
Returns:
the result of the health check
Throws:
java.util.NoSuchElementException if there is no health check with the given name
 
     public HealthCheck.Result runHealthCheck(String namethrows NoSuchElementException {
         final HealthCheck healthCheck = .get(name);
         if (healthCheck == null) {
             throw new NoSuchElementException("No health check named " + name + " exists");
         }
         return healthCheck.execute();
     }

    
Runs the registered health checks and returns a map of the results.

Returns:
a map of the health check results
 
         final SortedMap<StringHealthCheck.Resultresults = new TreeMap<StringHealthCheck.Result>();
         for (Map.Entry<StringHealthCheckentry : .entrySet()) {
             final Result result = entry.getValue().execute();
             results.put(entry.getKey(), result);
         }
         return Collections.unmodifiableSortedMap(results);
     }

    
Runs the registered health checks in parallel and returns a map of the results.

Returns:
a map of the health check results
 
         final Map<StringFuture<HealthCheck.Result>> futures = new HashMap<StringFuture<Result>>();
         for (final Map.Entry<StringHealthCheckentry : .entrySet()) {
             futures.put(entry.getKey(), executor.submit(new Callable<Result>() {
                 @Override
                 public Result call() throws Exception {
                     return entry.getValue().execute();
                 }
             }));
         }
 
         final SortedMap<StringHealthCheck.Resultresults = new TreeMap<StringHealthCheck.Result>();
        for (Map.Entry<StringFuture<Result>> entry : futures.entrySet()) {
            try {
                results.put(entry.getKey(), entry.getValue().get());
            } catch (Exception e) {
                .warn("Error executing health check {}"entry.getKey(), e);
            }
        }
        return Collections.unmodifiableSortedMap(results);
    }
New to GrepCode? Check out our FAQ X