Search in sources :

Example 1 with AsyncSession

use of org.neo4j.driver.async.AsyncSession in project micronaut-neo4j by micronaut-projects.

the class Neo4jHealthIndicator method getResult.

@Override
public Publisher<HealthResult> getResult() {
    try {
        Mono<HealthResult> healthResultSingle = Mono.create(emitter -> {
            AsyncSession session = boltDriver.asyncSession();
            CompletionStage<ResultSummary> query = session.writeTransactionAsync(tx -> tx.runAsync("RETURN 1 AS result").thenCompose(ResultCursor::consumeAsync));
            query.handleAsync((resultSummaryStage, throwable) -> {
                if (throwable != null) {
                    return buildErrorResult(throwable);
                } else {
                    HealthResult.Builder status = HealthResult.builder(NAME, HealthStatus.UP);
                    ServerInfo serverInfo = resultSummaryStage.server();
                    status.details(Collections.singletonMap("server", serverInfo.version() + "@" + serverInfo.address()));
                    return status.build();
                }
            }).thenComposeAsync(status -> session.closeAsync().handle((signal, throwable) -> status)).thenAccept(emitter::success);
        });
        return healthResultSingle.subscribeOn(Schedulers.fromExecutorService(ioExecutor));
    } catch (Throwable e) {
        return Mono.just(buildErrorResult(e));
    }
}
Also used : Driver(org.neo4j.driver.Driver) Publisher(org.reactivestreams.Publisher) Singleton(jakarta.inject.Singleton) HealthIndicator(io.micronaut.management.health.indicator.HealthIndicator) Mono(reactor.core.publisher.Mono) HealthStatus(io.micronaut.health.HealthStatus) ServerInfo(org.neo4j.driver.summary.ServerInfo) TaskExecutors(io.micronaut.scheduling.TaskExecutors) CompletionStage(java.util.concurrent.CompletionStage) AsyncSession(org.neo4j.driver.async.AsyncSession) ResultSummary(org.neo4j.driver.summary.ResultSummary) Requires(io.micronaut.context.annotation.Requires) Schedulers(reactor.core.scheduler.Schedulers) ResultCursor(org.neo4j.driver.async.ResultCursor) HealthResult(io.micronaut.management.health.indicator.HealthResult) Collections(java.util.Collections) ExecutorService(java.util.concurrent.ExecutorService) Named(jakarta.inject.Named) ServerInfo(org.neo4j.driver.summary.ServerInfo) AsyncSession(org.neo4j.driver.async.AsyncSession) ResultSummary(org.neo4j.driver.summary.ResultSummary) HealthResult(io.micronaut.management.health.indicator.HealthResult)

Aggregations

Requires (io.micronaut.context.annotation.Requires)1 HealthStatus (io.micronaut.health.HealthStatus)1 HealthIndicator (io.micronaut.management.health.indicator.HealthIndicator)1 HealthResult (io.micronaut.management.health.indicator.HealthResult)1 TaskExecutors (io.micronaut.scheduling.TaskExecutors)1 Named (jakarta.inject.Named)1 Singleton (jakarta.inject.Singleton)1 Collections (java.util.Collections)1 CompletionStage (java.util.concurrent.CompletionStage)1 ExecutorService (java.util.concurrent.ExecutorService)1 Driver (org.neo4j.driver.Driver)1 AsyncSession (org.neo4j.driver.async.AsyncSession)1 ResultCursor (org.neo4j.driver.async.ResultCursor)1 ResultSummary (org.neo4j.driver.summary.ResultSummary)1 ServerInfo (org.neo4j.driver.summary.ServerInfo)1 Publisher (org.reactivestreams.Publisher)1 Mono (reactor.core.publisher.Mono)1 Schedulers (reactor.core.scheduler.Schedulers)1