Search in sources :

Example 11 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class DbClientTracing method apply.

@Override
protected Single<DbClientServiceContext> apply(DbClientServiceContext serviceContext) {
    SpanTracingConfig spanConfig = TracingConfigUtil.spanConfig("dbclient", "statement");
    if (!spanConfig.enabled()) {
        return Single.just(serviceContext);
    }
    Context context = serviceContext.context();
    Tracer tracer = context.get(Tracer.class).orElseGet(GlobalTracer::get);
    // now if span context is missing, we build a span without a parent
    Tracer.SpanBuilder spanBuilder = tracer.buildSpan(serviceContext.statementName());
    context.get(SpanContext.class).ifPresent(spanBuilder::asChildOf);
    Span span = spanBuilder.start();
    span.setTag("db.operation", serviceContext.statementType().toString());
    if (spanConfig.logEnabled("statement", true)) {
        Tags.DB_STATEMENT.set(span, serviceContext.statement());
    }
    Tags.COMPONENT.set(span, "dbclient");
    Tags.DB_TYPE.set(span, serviceContext.dbType());
    serviceContext.statementFuture().thenAccept(nothing -> {
        if (spanConfig.logEnabled("statement-finish", true)) {
            span.log(Map.of("type", "statement"));
        }
    });
    serviceContext.resultFuture().thenAccept(count -> {
        if (spanConfig.logEnabled("result-finish", true)) {
            span.log(Map.of("type", "result", "count", count));
        }
        span.finish();
    }).exceptionally(throwable -> {
        Tags.ERROR.set(span, Boolean.TRUE);
        span.log(Map.of("event", "error", "error.kind", "Exception", "error.object", throwable, "message", throwable.getMessage()));
        span.finish();
        return null;
    });
    return Single.just(serviceContext);
}
Also used : Context(io.helidon.common.context.Context) DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) SpanContext(io.opentracing.SpanContext) DbClientServiceBase(io.helidon.dbclient.common.DbClientServiceBase) Tracer(io.opentracing.Tracer) Config(io.helidon.config.Config) TracingConfigUtil(io.helidon.tracing.config.TracingConfigUtil) Context(io.helidon.common.context.Context) GlobalTracer(io.opentracing.util.GlobalTracer) DbClientServiceContext(io.helidon.dbclient.DbClientServiceContext) Tags(io.opentracing.tag.Tags) SpanContext(io.opentracing.SpanContext) Map(java.util.Map) Single(io.helidon.common.reactive.Single) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig) SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) GlobalTracer(io.opentracing.util.GlobalTracer) GlobalTracer(io.opentracing.util.GlobalTracer) Span(io.opentracing.Span) SpanTracingConfig(io.helidon.tracing.config.SpanTracingConfig)

Example 12 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class Main method startServer.

/**
 * Start the server.
 * @return the created WebServer instance
 */
public static Single<WebServer> startServer() {
    // load logging configuration
    LogConfig.configureRuntime();
    // By default this will pick up application.yaml from the classpath
    Config config = Config.create();
    Single<WebServer> server = WebServer.builder(createRouting(config)).config(config.get("server")).addMediaSupport(JsonpSupport.create()).addMediaSupport(JsonbSupport.create()).build().start();
    server.thenAccept(ws -> {
        System.out.println("WEB server is up! http://localhost:" + ws.port() + "/api/movies");
        ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    return server;
}
Also used : Driver(org.neo4j.driver.Driver) JsonbSupport(io.helidon.media.jsonb.JsonbSupport) Config(io.helidon.config.Config) LogManager(java.util.logging.LogManager) IOException(java.io.IOException) MovieRepository(io.helidon.examples.integrations.neo4j.se.domain.MovieRepository) HealthSupport(io.helidon.health.HealthSupport) Neo4j(io.helidon.integrations.neo4j.Neo4j) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) Neo4jHealthCheck(io.helidon.integrations.neo4j.health.Neo4jHealthCheck) Neo4jMetricsSupport(io.helidon.integrations.neo4j.metrics.Neo4jMetricsSupport) WebServer(io.helidon.webserver.WebServer) Single(io.helidon.common.reactive.Single) MetricsSupport(io.helidon.metrics.MetricsSupport) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) InputStream(java.io.InputStream) HealthChecks(io.helidon.health.checks.HealthChecks) WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig)

Example 13 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class Main method startServer.

/**
 * Start the server.
 *
 * @return the created {@link WebServer} instance
 */
static Single<WebServer> startServer(Config config) {
    // load logging configuration
    LogConfig.configureRuntime();
    // Build server using three ports:
    // default public port, admin port, private port
    WebServer server = WebServer.builder(createPublicRouting(config)).config(config.get("server")).addMediaSupport(JsonpSupport.create()).build();
    Single<WebServer> webServerSingle = server.start();
    // Try to start the server. If successful, print some info and arrange to
    // print a message at shutdown. If unsuccessful, print the exception.
    webServerSingle.thenAccept(ws -> {
        System.out.println("WEB server is up! http://localhost:" + ws.port());
        ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    }).exceptionallyAccept(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
    });
    return webServerSingle;
}
Also used : JsonpSupport(io.helidon.media.jsonp.JsonpSupport) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) Single(io.helidon.common.reactive.Single) MetricsSupport(io.helidon.metrics.MetricsSupport) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) HealthSupport(io.helidon.health.HealthSupport) HealthChecks(io.helidon.health.checks.HealthChecks) WebServer(io.helidon.webserver.WebServer)

Example 14 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class BulkheadTest method testBulkheadQueue.

@Test
void testBulkheadQueue() throws InterruptedException {
    Bulkhead bulkhead = Bulkhead.builder().limit(1).queueLength(1000).build();
    Request inProgress = new Request(0);
    bulkhead.invoke(inProgress::invoke);
    Request[] aLotRequests = new Request[999];
    Single[] aLotResults = new Single[999];
    for (int i = 0; i < aLotRequests.length; i++) {
        Request req = new Request(i);
        aLotRequests[i] = req;
        aLotResults[i] = bulkhead.invoke(req::invoke);
    }
    for (Request req : aLotRequests) {
        req.releaseCdl.countDown();
    }
    inProgress.releaseCdl.countDown();
    if (inProgress.invokedCdl.await(1, TimeUnit.SECONDS)) {
        for (Single result : aLotResults) {
            result.await(1, TimeUnit.SECONDS);
        }
    } else {
        fail("Should have invoked the first");
    }
}
Also used : Single(io.helidon.common.reactive.Single) Test(org.junit.jupiter.api.Test)

Example 15 with Single

use of io.helidon.common.reactive.Single in project helidon by oracle.

the class Main method startServer.

/**
 * Start the server.
 * @return the created {@link WebServer} instance
 */
static Single<WebServer> startServer(Config config) {
    // load logging configuration
    LogConfig.configureRuntime();
    // Build server using three ports:
    // default public port, admin port, private port
    WebServer server = WebServer.builder(createPublicRouting()).config(config.get("server")).addNamedRouting("admin", createAdminRouting()).addNamedRouting("private", createPrivateRouting()).build();
    Single<WebServer> webserver = server.start();
    // Try to start the server. If successful, print some info and arrange to
    // print a message at shutdown. If unsuccessful, print the exception.
    webserver.thenAccept(ws -> {
        System.out.println("WEB server is up! http://localhost:" + ws.port());
        ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    return webserver;
}
Also used : Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) Single(io.helidon.common.reactive.Single) MetricsSupport(io.helidon.metrics.MetricsSupport) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) HealthSupport(io.helidon.health.HealthSupport) HealthChecks(io.helidon.health.checks.HealthChecks) WebServer(io.helidon.webserver.WebServer)

Aggregations

Single (io.helidon.common.reactive.Single)39 Config (io.helidon.config.Config)23 Routing (io.helidon.webserver.Routing)18 WebServer (io.helidon.webserver.WebServer)18 LogConfig (io.helidon.common.LogConfig)16 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)15 MetricsSupport (io.helidon.metrics.MetricsSupport)12 Optional (java.util.Optional)12 Logger (java.util.logging.Logger)12 Http (io.helidon.common.http.Http)11 MediaType (io.helidon.common.http.MediaType)9 DataChunk (io.helidon.common.http.DataChunk)8 HealthSupport (io.helidon.health.HealthSupport)8 HealthChecks (io.helidon.health.checks.HealthChecks)8 WebClient (io.helidon.webclient.WebClient)8 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)8 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)8 JsonObject (jakarta.json.JsonObject)8 Supplier (java.util.function.Supplier)8 Contexts (io.helidon.common.context.Contexts)7