Java Code Examples for org.eclipse.microprofile.health.HealthCheckResponse

The following examples show how to use org.eclipse.microprofile.health.HealthCheckResponse. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: boost   Source File: DBHealthCheck.java    License: Eclipse Public License 1.0 7 votes vote down vote up
@Override
public HealthCheckResponse call() {

    HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("membership");
    try {
        Connection connection = datasource.getConnection();
        boolean isValid = connection.isValid(5);

        DatabaseMetaData metaData = connection.getMetaData();

        responseBuilder = responseBuilder
                    .withData("databaseProductName", metaData.getDatabaseProductName())
                    .withData("databaseProductVersion", metaData.getDatabaseProductVersion())
                    .withData("driverName", metaData.getDriverName())
                    .withData("driverVersion", metaData.getDriverVersion())
                    .withData("isValid", isValid);

        return responseBuilder.state(isValid).build();


    } catch(SQLException  e) {
        responseBuilder = responseBuilder
               .withData("exceptionMessage", e.getMessage());
        return responseBuilder.down().build();
    }
}
 
Example 2
@Override
public HealthCheckResponse call() {

    HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("Database connection health check");

    try {
        simulateDatabaseConnectionVerification();
        responseBuilder.up();
    } catch (IllegalStateException e) {
        // cannot access the database
        responseBuilder.down()
            .withData("error", e.getMessage()); // pass the exception message
    }

    return responseBuilder.build();
}
 
Example 3
@Override
public HealthCheckResponse call() {

    HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("Database connection health check");

    try {
        simulateDatabaseConnectionVerification();
        responseBuilder.up();
    } catch (IllegalStateException e) {
        // cannot access the database
        responseBuilder.down()
            .withData("error", e.getMessage()); // pass the exception message
    }

    return responseBuilder.build();
}
 
Example 4
Source Project: kogito-runtimes   Source File: InfinispanHealthCheckIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void testCall() throws Exception {
    InfinispanServerTestResource resource = new InfinispanServerTestResource();
    resource.start();

    this.healthCheck = new InfinispanHealthCheck(instance);

    //testing Up
    HealthCheckResponse response = healthCheck.call();
    Assertions.assertThat(response.getState()).isEqualTo(HealthCheckResponse.State.UP);

    resource.stop();

    //testing Down
    HealthCheckResponse response2 = healthCheck.call();
    Assertions.assertThat(response2.getState()).isEqualTo(HealthCheckResponse.State.DOWN);
}
 
Example 5
@Override
public HealthCheckResponse call() {

    HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("Database connection health check");

    try {
        simulateDatabaseConnectionVerification();
        responseBuilder.up();
    } catch (IllegalStateException e) {
        // cannot access the database
        responseBuilder.down()
                .withData("error", e.getMessage()); // pass the exception message
    }

    return responseBuilder.build();
}
 
Example 6
Source Project: quarkus   Source File: FailingHealthCheck.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public HealthCheckResponse call() {
    return new HealthCheckResponse() {
        @Override
        public String getName() {
            return "failing";
        }

        @Override
        public State getState() {
            return State.DOWN;
        }

        @Override
        public Optional<Map<String, Object>> getData() {
            return Optional.of(Collections.singletonMap("status", "all broken"));
        }
    };
}
 
Example 7
Source Project: quarkus   Source File: BasicHealthCheck.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public HealthCheckResponse call() {
    return new HealthCheckResponse() {
        @Override
        public String getName() {
            return "basic";
        }

        @Override
        public State getState() {
            return State.UP;
        }

        @Override
        public Optional<Map<String, Object>> getData() {
            return Optional.of(Collections.singletonMap("foo", "bar"));
        }
    };
}
 
Example 8
Source Project: quarkus   Source File: SimpleHealthCheck.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public HealthCheckResponse call() {
    return new HealthCheckResponse() {
        @Override
        public String getName() {
            return "basic";
        }

        @Override
        public State getState() {
            return State.UP;
        }

        @Override
        public Optional<Map<String, Object>> getData() {
            return Optional.empty();
        }
    };
}
 
Example 9
Source Project: blog-tutorials   Source File: DatabaseLivenessCheck.java    License: MIT License 6 votes vote down vote up
@Override
public HealthCheckResponse call() {

    try (var connection = dataSource.getConnection()) {
        var metaData = connection.getMetaData();

        return HealthCheckResponse.builder()
                .name(livenessCheckName)
                .withData("version", String.format("%s - %s.%s",
                        metaData.getDatabaseProductName(),
                        metaData.getDatabaseMajorVersion(),
                        metaData.getDatabaseMinorVersion()))
                .withData("schema", connection.getSchema())
                .withData("databaseName", connection.getCatalog())
                .up()
                .build();
    } catch (SQLException e) {
        return HealthCheckResponse.down(livenessCheckName);
    }
}
 
Example 10
Source Project: quarkus   Source File: KafkaStreamsTopicsHealthCheck.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public HealthCheckResponse call() {
    HealthCheckResponseBuilder builder = HealthCheckResponse.named("Kafka Streams topics health check").up();

    try {
        Set<String> missingTopics = manager.getMissingTopics(trimmedTopics);
        List<String> availableTopics = new ArrayList<>(trimmedTopics);
        availableTopics.removeAll(missingTopics);

        if (!availableTopics.isEmpty()) {
            builder.withData("available_topics", String.join(",", availableTopics));
        }
        if (!missingTopics.isEmpty()) {
            builder.down().withData("missing_topics", String.join(",", missingTopics));
        }
    } catch (InterruptedException e) {
        LOGGER.error("error when retrieving missing topics", e);
        builder.down().withData("technical_error", e.getMessage());
    }

    return builder.build();
}
 
Example 11
Source Project: trader   Source File: LivenessProbe.java    License: Apache License 2.0 6 votes vote down vote up
public HealthCheckResponse call() {
	HealthCheckResponse response = null;
	String message = "Live";
	try {
		HealthCheckResponseBuilder builder = HealthCheckResponse.named("Trader");

		if (Summary.error) { //can't run without these env vars
			builder = builder.down();
			message = Summary.message;
			logger.warning("Returning NOT live!");
		} else {
			builder = builder.up();
			logger.fine("Returning live!");
		}

		builder = builder.withData("message", message);

		response = builder.build(); 
	} catch (Throwable t) {
		logger.warning("Exception occurred during health check: "+t.getMessage());
		logException(t);
		throw t;
	}

	return response;
}
 
Example 12
Source Project: thorntail   Source File: ParserTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testHealthCheckResponseBuild() {

    HealthCheckResponse healthStatus = HealthCheckResponse.named("test")
            .up()
            .withData("a", "b")
            .withData("c", "d")
            .build();

    Assert.assertEquals("test", healthStatus.getName());
    Assert.assertSame(State.UP, healthStatus.getState());
    Map<String, Object> data = healthStatus.getData().get();
    Assert.assertEquals(2, data.size());
    Assert.assertEquals("Expected a", "b", data.get("a"));
    Assert.assertEquals("Expected c", "d", data.get("c"));
}
 
Example 13
@Override
public synchronized HealthCheckResponse call() {
    callSuper();
    return HealthCheckResponse.builder()
            .name("PersistenceExceptionLivenessCheck")
            .withData("errorCount", errorCounter)
            .state(up)
            .build();
}
 
Example 14
@Override
public synchronized HealthCheckResponse call() {
    callSuper();
    return HealthCheckResponse.builder()
            .name("PersistenceTimeoutReadinessCheck")
            .withData("errorCount", errorCounter)
            .state(up)
            .build();
}
 
Example 15
Source Project: trellis   Source File: DBHealthCheck.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
    if (dataSource != null) {
        try (final Connection conn = dataSource.getConnection()) {
            return HealthCheckResponse.named(DBHealthCheck.class.getSimpleName())
                .state(!conn.isClosed()).build();
        } catch (final SQLException ex) {
            return HealthCheckResponse.named(DBHealthCheck.class.getSimpleName())
                .withData("exception", ex.getMessage()).down().build();
        }
    }
    return HealthCheckResponse.named(DBHealthCheck.class.getSimpleName()).down().build();
}
 
Example 16
@Override
public synchronized HealthCheckResponse call() {
    callSuper();
    return HealthCheckResponse.builder()
            .name("ResponseErrorLivenessCheck")
            .withData("errorCount", errorCounter)
            .state(up)
            .build();
}
 
Example 17
Source Project: quarkus   Source File: HealthCheckDefaultScopeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
    if (++counter > 1) {
        return HealthCheckResponse.builder().down().name("noScope").build();
    }
    return HealthCheckResponse.builder().up().name("noScope").build();
}
 
Example 18
@Override
public HealthCheckResponse call() {
    HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("MemoryHealthCheck Liveness check");
    long freeMemory = Runtime.getRuntime().freeMemory();

    if (freeMemory >= threshold) {
        responseBuilder.up();
    }
    else {
        responseBuilder.down()
                .withData("error", "Not enough free memory! Please restart application");
    }
    return responseBuilder.build();
}
 
Example 19
Source Project: smallrye-health   Source File: SocketHealthCheckTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSocketHealthCheckNoneServiceOnPort() {
    SocketHealthCheck socketHealthCheck = new SocketHealthCheck("198.51.100.0", 8985);

    final HealthCheckResponse healthCheckResponse = socketHealthCheck.call();
    assertEquals(HealthCheckResponse.State.DOWN, healthCheckResponse.getState());
    assertEquals("connect timed out", healthCheckResponse.getData().get().get("error"));
}
 
Example 20
Source Project: quarkus   Source File: DispatchedThreadTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
    return HealthCheckResponse.named("my-readiness-check")
            .up()
            .withData("thread", Thread.currentThread().getName())
            .withData("request", Arc.container().requestContext().isActive())
            .build();
}
 
Example 21
public HealthCheckResponse call() {
    return HealthCheckResponse.named("service-check")
            .withData("port", 12345)
            .withData("isSecure", true)
            .withData("hostname", "service.jboss.com")
            .up()
            .build();
}
 
Example 22
Source Project: quarkus-in-prod   Source File: DemoLive.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
    return HealthCheckResponse.named("quarkus-demo-live")
            .up()
            .withData("config property", liveProperty)
            .build();
}
 
Example 23
Source Project: ci.maven   Source File: SystemHealth.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
  if (!System.getProperty("wlp.server.name").startsWith("defaultServer")) {
    return HealthCheckResponse.named(HelloWorld.class.getSimpleName())
                              .withData("default server", "not available").down()
                              .build();
  }
  return HealthCheckResponse.named(HelloWorld.class.getSimpleName())
                            .withData("default server", "available").up().build();
}
 
Example 24
@Override
public HealthCheckResponse call() {

    boolean isHealthy = false;

    try {
        Client client = ClientBuilder.newClient();
        Response response = client.target(bookServiceUrl).request(MediaType.APPLICATION_JSON).get();
        
        if (response.getStatus() != 200) {
            isHealthy = false;
        }
        isHealthy = true;
    } catch (Exception e) {
        isHealthy = false;
    }

    if (!isHealthy) {
        return HealthCheckResponse.named("bookservice")
                .withData("service", "not available")
                .down().build();
    }

    return HealthCheckResponse.named("bookservice")
            .withData("service", "available")
            .up().build();
}
 
Example 25
Source Project: microprofile-sandbox   Source File: SystemHealth.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
    if (!System.getProperty("wlp.server.name").equals("BoostServer")) {
        return HealthCheckResponse.named(SystemResource.class.getSimpleName())
                .withData("default server", "not available").down().build();
    }
    return HealthCheckResponse.named(SystemResource.class.getSimpleName()).withData("default server", "available")
            .up().build();
}
 
Example 26
Source Project: intellij-quarkus   Source File: DataHealthCheck.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
    return HealthCheckResponse.named("Health check with data")
        .up()
        .withData("foo", "fooValue")
        .withData("bar", "barValue")
        .build();
}
 
Example 27
Source Project: bouncr   Source File: HealthCheckHandler.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    if (exchange.isInIoThread()) {
        exchange.dispatch(this);
        return;
    }

    HealthCheckResponse response = HealthCheckResponse.builder()
            .name("bouncr-proxy")
            .up()
            .build();
    exchange.setStatusCode(200);
    exchange.getResponseSender().send(mapper.writeValueAsString(response));
}
 
Example 28
Source Project: intellij-quarkus   Source File: DataHealthCheck.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
    return HealthCheckResponse.named("Health check with data")
        .up()
        .withData("foo", "fooValue")
        .withData("bar", "barValue")
        .build();
}
 
Example 29
Source Project: kogito-runtimes   Source File: InfinispanHealthCheck.java    License: Apache License 2.0 5 votes vote down vote up
private HealthCheckResponse buildResponse(ChannelFactory channelFactory, boolean state) {
    return HealthCheckResponse.builder()
            .withData("nodes", Optional.ofNullable(channelFactory.getServers())
                    .orElse(Collections.emptyList())
                    .stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(",")))
            .name(state ? "Infinispan is Up" : "Infinispan is Down")
            .state(state)
            .build();
}
 
Example 30
Source Project: boost   Source File: SystemHealth.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
    if (!System.getProperty("wlp.server.name").equals("defaultServer")) {
        return HealthCheckResponse.named(SystemResource.class.getSimpleName())
                .withData("default server", "not available").down().build();
    }
    return HealthCheckResponse.named(SystemResource.class.getSimpleName()).withData("default server", "available")
            .up().build();
}