Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.metrics.core;

A health check for a component of your application.
  
  public abstract class HealthCheck {
    
The result of a HealthCheck being run. It can be healthy (with an optional message) or unhealthy (with either an error message or a thrown exception).
 
     public static class Result {
         private static final Result HEALTHY = new Result(truenullnull);
         private static final int PRIME = 31;

        
Returns a healthy HealthCheck.Result with no additional message.

Returns:
a healthy HealthCheck.Result with no additional message
 
         public static Result healthy() {
             return ;
         }

        
Returns a healthy HealthCheck.Result with an additional message.

Parameters:
message an informative message
Returns:
a healthy HealthCheck.Result with an additional message
 
         public static Result healthy(String message) {
             return new Result(truemessagenull);
         }

        
Returns a healthy HealthCheck.Result with a formatted message. Message formatting follows the same rules as java.lang.String.format(java.lang.String,java.lang.Object[]).

Parameters:
message a message format
args the arguments apply to the message format
Returns:
a healthy HealthCheck.Result with an additional message
See also:
java.lang.String.format(java.lang.String,java.lang.Object[])
 
         public static Result healthy(String messageObject... args) {
             return healthy(String.format(messageargs));
         }

        
Returns an unhealthy HealthCheck.Result with the given message.

Parameters:
message an informative message describing how the health check failed
Returns:
an unhealthy HealthCheck.Result with the given message
 
         public static Result unhealthy(String message) {
             return new Result(falsemessagenull);
         }

        
Returns an unhealthy HealthCheck.Result with a formatted message. Message formatting follows the same rules as java.lang.String.format(java.lang.String,java.lang.Object[]).

Parameters:
message a message format
args the arguments apply to the message format
Returns:
an unhealthy HealthCheck.Result with an additional message
See also:
java.lang.String.format(java.lang.String,java.lang.Object[])
 
         public static Result unhealthy(String messageObject... args) {
             return unhealthy(String.format(messageargs));
         }

        
Returns an unhealthy HealthCheck.Result with the given error.

Parameters:
error an exception thrown during the health check
Returns:
an unhealthy HealthCheck.Result with the given error
 
         public static Result unhealthy(Throwable error) {
             return new Result(falseerror.getMessage(), error);
         }
 
         private final boolean healthy;
         private final String message;
         private final Throwable error;
 
         private Result(boolean isHealthyString messageThrowable error) {
             this. = isHealthy;
             this. = message;
             this. = error;
         }

        
Returns true if the result indicates the component is healthy; false otherwise.

Returns:
true if the result indicates the component is healthy
 
         public boolean isHealthy() {
            return ;
        }

        
Returns any additional message for the result, or null if the result has no message.

Returns:
any additional message for the result, or null
        public String getMessage() {
            return ;
        }

        
Returns any exception for the result, or null if the result has no exception.

Returns:
any exception for the result, or null
        public Throwable getError() {
            return ;
        }
        @Override
        public boolean equals(Object o) {
            if (this == o) { return true; }
            if (o == null || getClass() != o.getClass()) { return false; }
            final Result result = (Resulto;
            return  == result.healthy &&
                    !( != null ? !.equals(result.error) : result.error != null) &&
                    !( != null ? !.equals(result.message) : result.message != null);
        }
        @Override
        public int hashCode() {
            int result = ( ? 1 : 0);
            result =  * result + ( != null ? .hashCode() : 0);
            result =  * result + ( != null ? .hashCode() : 0);
            return result;
        }
        @Override
        public String toString() {
            final StringBuilder builder = new StringBuilder("Result{isHealthy=");
            builder.append();
            if ( != null) {
                builder.append(", message=").append();
            }
            if ( != null) {
                builder.append(", error=").append();
            }
            builder.append('}');
            return builder.toString();
        }
    }
    private final String name;

    
Create a new HealthCheck instance with the given name.

Parameters:
name the name of the health check (and, ideally, the name of the underlying component the health check tests)
    protected HealthCheck(String name) {
        this. = name;
    }

    
Returns the health check's name.

Returns:
the health check's name
    public String getName() {
        return ;
    }

    
Perform a check of the application component.

Returns:
if the component is healthy, a healthy HealthCheck.Result; otherwise, an unhealthy HealthCheck.Result with a descriptive error message or exception
Throws:
java.lang.Exception if there is an unhandled error during the health check; this will result in a failed health check
    protected abstract Result check() throws Exception;

    
Executes the health check, catching and handling any exceptions raised by check().

Returns:
if the component is healthy, a healthy HealthCheck.Result; otherwise, an unhealthy HealthCheck.Result with a descriptive error message or exception
    public Result execute() {
        try {
            return check();
        } catch (Error e) {
            throw e;
        } catch (Throwable e) {
            return Result.unhealthy(e);
        }
    }
New to GrepCode? Check out our FAQ X