Skip to content
Permalink
Browse files
GERONIMO-6738 support spec v2
  • Loading branch information
rmannibucau committed Jul 19, 2019
1 parent 5bf7cd4 commit 19cf1f0ba886546283b4cdaf99e62ac20073d2f9
Showing 6 changed files with 105 additions and 22 deletions.
@@ -35,10 +35,15 @@ public String getName() {
}

@Override
@JsonbTransient
public State getState() {
return state;
}

public State getStatus() {
return state;
}

@JsonbProperty("data")
public Map<String, Object> getRawData() {
return data;
@@ -20,6 +20,8 @@

import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
@@ -34,7 +36,7 @@
@Path("health")
// @ApplicationScoped
public class HealthChecksEndpoint {
private HealthChecksRegistry registry;
private volatile HealthChecksRegistry registry;

public void setRegistry(final HealthChecksRegistry registry) {
this.registry = registry;
@@ -43,16 +45,40 @@ public void setRegistry(final HealthChecksRegistry registry) {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getChecks() {
return toResponse(HealthChecksRegistry::getChecks, r -> {});
}

@GET
@Path("live")
@Produces(MediaType.APPLICATION_JSON)
public Response getLiveChecks() {
return toResponse(HealthChecksRegistry::getLiveness, r -> {});
}

@GET
@Path("ready")
@Produces(MediaType.APPLICATION_JSON)
public Response getReadyChecks() {
return toResponse(HealthChecksRegistry::getReadiness, r -> {});
}

private Response toResponse(final Function<HealthChecksRegistry, Collection<HealthCheck>> extractor,
final Consumer<HealthChecksRegistry> validate) {
if (registry == null) {
registry = HealthChecksRegistry.load();
synchronized (this) {
if (registry == null) {
registry = HealthChecksRegistry.load();
}
}
}
validate.accept(registry);

final List<HealthCheckResponse> checks = registry.getChecks()
.stream()
.map(HealthCheck::call)
.collect(toList());
final List<HealthCheckResponse> checks = extractor.apply(registry)
.stream()
.map(HealthCheck::call)
.collect(toList());
final HealthCheckResponse.State globalState = checks.stream()
.reduce(HealthCheckResponse.State.UP, (a, b) -> combine(a, b.getState()), this::combine);
.reduce(HealthCheckResponse.State.UP, (a, b) -> combine(a, b.getState()), this::combine);
return Response.status(globalState == HealthCheckResponse.State.DOWN ? Response.Status.SERVICE_UNAVAILABLE : Response.Status.OK).entity(new AggregatedResponse(globalState, checks)).build();
}

@@ -74,11 +100,6 @@ public HealthCheckResponse.State getStatus() {
return status;
}

@Deprecated
public HealthCheckResponse.State getOutcome() {
return status;
}

public Collection<HealthCheckResponse> getChecks() {
return checks;
}
@@ -24,6 +24,9 @@

public interface HealthChecksRegistry {
Collection<HealthCheck> getChecks();
Collection<HealthCheck> getReadiness();
Collection<HealthCheck> getLiveness();
boolean isReady();

static HealthChecksRegistry load() {
final Iterator<HealthChecksRegistry> iterator = ServiceLoader.load(HealthChecksRegistry.class).iterator();
@@ -92,7 +92,7 @@
<dependency>
<groupId>org.apache.meecrowave</groupId>
<artifactId>meecrowave-arquillian</artifactId>
<version>1.2.3</version>
<version>1.2.8</version>
<scope>test</scope>
<exclusions>
<exclusion>
@@ -114,8 +114,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<version>3.0.0-M3</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
<trimStackTrace>false</trimStackTrace>
<dependenciesToScan>
<dependency>org.eclipse.microprofile.health:microprofile-health-tck</dependency>
</dependenciesToScan>
@@ -23,6 +23,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
@@ -32,26 +33,61 @@
import javax.enterprise.inject.spi.BeforeShutdown;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.util.AnnotationLiteral;

import org.apache.geronimo.microprofile.common.registry.HealthChecksRegistry;
import org.eclipse.microprofile.health.Health;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.Liveness;
import org.eclipse.microprofile.health.Readiness;

public class GeronimoHealthExtension implements Extension, HealthChecksRegistry {
private final Collection<Bean<?>> beans = new ArrayList<>();
private final Collection<Bean<?>> livenessBeans = new ArrayList<>();
private final Collection<Bean<?>> readinessBeans = new ArrayList<>();
private final Collection<CreationalContext<?>> contexts = new ArrayList<>();
private List<HealthCheck> checks;
private List<HealthCheck> liveness;
private List<HealthCheck> readiness;
private boolean started = false;

private static class LivenessLiteral extends AnnotationLiteral<Liveness> {
private static final Annotation INSTANCE = new LivenessLiteral();
}

private static class ReadinessLiteral extends AnnotationLiteral<Readiness> {
private static final Annotation INSTANCE = new ReadinessLiteral();
}

void findChecks(@Observes final ProcessBean<?> bean) {
if (bean.getAnnotated().isAnnotationPresent(Health.class) && bean.getBean().getTypes().contains(HealthCheck.class)) {
if (!bean.getBean().getTypes().contains(HealthCheck.class)) {
return;
}
if (bean.getAnnotated().isAnnotationPresent(Health.class)) {
beans.add(bean.getBean());
}
if (bean.getBean().getQualifiers().contains(LivenessLiteral.INSTANCE)) {
livenessBeans.add(bean.getBean());
}
if (bean.getBean().getQualifiers().contains(ReadinessLiteral.INSTANCE)) {
readinessBeans.add(bean.getBean());
}
}

void start(@Observes final AfterDeploymentValidation afterDeploymentValidation, final BeanManager beanManager) {
checks = beans.stream()
liveness = livenessBeans.stream()
.map(it -> lookup(it, beanManager))
.collect(toList());
readiness = readinessBeans.stream()
.map(it -> lookup(it, beanManager))
.collect(toList());
checks = Stream.concat(Stream.concat(
beans.stream()
.map(it -> lookup(it, beanManager)),
liveness.stream()),
readiness.stream())
.collect(toList());
started = true;
}

void stop(@Observes final BeforeShutdown beforeShutdown) {
@@ -76,9 +112,24 @@ public List<HealthCheck> getChecks() {
return checks;
}

@Override
public Collection<HealthCheck> getReadiness() {
return readiness;
}

@Override
public Collection<HealthCheck> getLiveness() {
return liveness;
}

@Override
public boolean isReady() {
return started;
}

private HealthCheck lookup(final Bean<?> bean, final BeanManager manager) {
final Class<?> type = bean.getBeanClass() == null ? HealthCheck.class : bean.getBeanClass();
final Set<Bean<?>> beans = manager.getBeans(type, bean.getQualifiers().toArray(new Annotation[bean.getQualifiers().size()]));
final Set<Bean<?>> beans = manager.getBeans(type, bean.getQualifiers().toArray(new Annotation[0]));
final Bean<?> resolvedBean = manager.resolve(beans);
final CreationalContext<Object> creationalContext = manager.createCreationalContext(null);
if (!manager.isNormalScope(resolvedBean.getScope())) {
10 pom.xml
@@ -42,7 +42,7 @@
</scm>

<properties>
<spec.version>1.0</spec.version>
<spec.version>2.0.1</spec.version>
</properties>

<modules>
@@ -59,7 +59,7 @@
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>9.0.7</version>
<version>9.0.20</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -71,13 +71,13 @@
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-json_1.1_spec</artifactId>
<version>1.0</version>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jsonb_1.0_spec</artifactId>
<version>1.0</version>
<version>1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
@@ -87,7 +87,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>

0 comments on commit 19cf1f0

Please sign in to comment.