Skip to content

Commit

Permalink
feat(Actuator) : Add health indicator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Wojtczak committed May 18, 2017
1 parent 85315d1 commit e6e3a00
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 32 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-docs</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/io/github/maxwo/healthcheck/HealthCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

Expand All @@ -33,13 +37,28 @@
@EnableAutoConfiguration
@EnableSwagger2
public class HealthCheck {

/**
* Launch spring boot application.
*
* @param args Arguments.
* @param args
* Arguments.
*/
public static void main( String[] args ) {
public static void main(String[] args) {
SpringApplication.run(HealthCheck.class, args);
}
}

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(final CorsRegistry registry) {
registry
.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "DELETE", "PUT");
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
*/
package io.github.maxwo.healthcheck.controller;

import org.springframework.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.github.maxwo.healthcheck.dao.HealthCheckDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand All @@ -41,36 +39,28 @@
* @author Maxime Wojtczak
*/
@Api(
value = "/healthcheck",
tags = {"test", "healthcheck"}
value = "/healthcheck"
)
@Controller
@RestController
@RequestMapping(value = "/healthcheck")
public class HealthCheckController {

/**
* Healthy indicator.
*/
private boolean healthy = false;
/** DAO keeping the healthcheck state */
@Autowired
private HealthCheckDao healthCheckDao;

/**
* Indicates whether the service is healthy or not.
* Retrieve the healthy flag.
*
* @return Healthy state.
*/
@ApiOperation(value = "Check if the service is healthy.")
@ApiOperation(value = "Retrieve the healthy flag.")
@ApiResponses(value = {
@ApiResponse(code = 204, message = "Service is healthy."),
@ApiResponse(code = 500, message = "Service is unhealthy.")
@ApiResponse(code = 200, message = "Health state.", response = Boolean.class),
})
@CrossOrigin
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Void> isHealthy() {
if (healthy) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
public boolean isHealthy() {
return healthCheckDao.getState();
}

/**
Expand All @@ -82,13 +72,11 @@ public ResponseEntity<Void> isHealthy() {
value = "Sets the healthy state.",
consumes = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses(value = {
@ApiResponse(code = 204, message = "Healthy state is set.")
@ApiResponse(code = 200, message = "Health state is set.")
})
@CrossOrigin
@ResponseStatus(HttpStatus.NO_CONTENT)
@RequestMapping(method = RequestMethod.PUT, consumes = {MediaType.APPLICATION_JSON_VALUE})
public void setHealty(@RequestBody boolean healthy) {
this.healthy = healthy;
public void setHealty(@RequestBody final boolean healthy) {
healthCheckDao.setState(healthy);
}

}
16 changes: 16 additions & 0 deletions src/main/java/io/github/maxwo/healthcheck/dao/HealthCheckDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
*
*/
package io.github.maxwo.healthcheck.dao;

/**
* @author max
*
*/
public interface HealthCheckDao {

void setState(final boolean state);

boolean getState();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
*
*/
package io.github.maxwo.healthcheck.dao.impl;

import org.springframework.stereotype.Component;

import io.github.maxwo.healthcheck.dao.HealthCheckDao;

/**
* @author max
*
*/
@Component
public class HealthCheckDaoImpl implements HealthCheckDao {

private boolean state = false;

/* (non-Javadoc)
* @see io.github.maxwo.healthcheck.dao.HealthCheckDao#setState(boolean)
*/
@Override
public void setState(boolean state) {
this.state = state;
}

/* (non-Javadoc)
* @see io.github.maxwo.healthcheck.dao.HealthCheckDao#getState()
*/
@Override
public boolean getState() {
return state;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2017 Maxime Wojtczak
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTIO
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package io.github.maxwo.healthcheck.indicator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

import io.github.maxwo.healthcheck.dao.HealthCheckDao;

/**
* Spring health indicator based on health state.
*
* @author Maxime Wojtczak
*/
@Component
public class HealthStateIndicator implements HealthIndicator {

/** DAO for health state */
@Autowired
private HealthCheckDao healthCheckDao;

/* (non-Javadoc)
* @see org.springframework.boot.actuate.health.HealthIndicator#health()
*/
@Override
public Health health() {
if (healthCheckDao.getState()) {
return Health
.up()
.withDetail("manualState", "Service status has been manually set to up")
.build();
} else {
return Health
.down()
.withDetail("manualState", "Service status has been manually set to down")
.build();
}
}

}
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
endpoints.health.enabled=true
endpoints.health.sensitive=false

spring.main.banner-mode=off

0 comments on commit e6e3a00

Please sign in to comment.