Search in sources :

Example 6 with DbClient

use of io.helidon.dbclient.DbClient in project helidon by oracle.

the class ServerMain method startServer.

private static WebServer startServer(final String configFile) {
    final Config config = Config.create(ConfigSources.classpath(configFile));
    final DbClient dbClient = DbClient.builder(config.get("db")).build();
    final LifeCycleService lcResource = new LifeCycleService(dbClient);
    final Routing routing = Routing.builder().register("/LifeCycle", lcResource).register("/HelloWorld", new HelloWorldService(dbClient)).build();
    final WebServer server = WebServer.builder().routing(routing).config(config.get("server")).addMediaSupport(JsonpSupport.create()).build();
    // Set server instance to exit resource.
    lcResource.setServer(server);
    // Start the server and print some info.
    server.start().thenAccept(ws -> {
        System.out.println(String.format("WEB server is up! http://localhost:%d/", ws.port()));
    });
    // Server threads are not daemon. NO need to block. Just react.
    server.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    return server;
}
Also used : DbClient(io.helidon.dbclient.DbClient) WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing)

Example 7 with DbClient

use of io.helidon.dbclient.DbClient in project helidon by oracle.

the class ApplMain method startServer.

private static WebServer startServer(final String configFile) {
    final Config config = Config.create(ConfigSources.classpath(configFile));
    final Config dbConfig = config.get("db");
    // Client services are added through a service loader - see mongoDB example for explicit services
    final DbClient dbClient = DbClient.builder(dbConfig).addService(DbClientMetrics.counter().statementNames("select-pokemon-named-arg", "select-pokemon-order-arg", "insert-pokemon")).addService(DbClientMetrics.timer().statementTypes(DbStatementType.GET)).build();
    final HealthSupport health = HealthSupport.builder().addLiveness(DbClientHealthCheck.builder(dbClient).statementName("ping").build()).build();
    final Map<String, String> statements = dbConfig.get("statements").detach().asMap().get();
    final ExitService exitResource = new ExitService();
    final DbClientService interceptorTestService = new InterceptorService.TestClientService();
    final Routing routing = Routing.builder().register(// Metrics at "/metrics"
    MetricsSupport.create()).register(// Health at "/health"
    health).register("/Init", new InitService(dbClient, dbConfig)).register("/Exit", exitResource).register("/Verify", new VerifyService(dbClient, config)).register("/SimpleGet", new SimpleGetService(dbClient, statements)).register("/SimpleQuery", new SimpleQueryService(dbClient, statements)).register("/SimpleUpdate", new SimpleUpdateService(dbClient, statements)).register("/SimpleInsert", new SimpleInsertService(dbClient, statements)).register("/SimpleDelete", new SimpleDeleteService(dbClient, statements)).register("/TransactionGet", new TransactionGetService(dbClient, statements)).register("/TransactionQueries", new TransactionQueriesService(dbClient, statements)).register("/TransactionUpdate", new TransactionUpdateService(dbClient, statements)).register("/TransactionInsert", new TransactionInsertService(dbClient, statements)).register("/TransactionDelete", new TransactionDeleteService(dbClient, statements)).register("/DmlStatement", new DmlStatementService(dbClient, statements)).register("/GetStatement", new GetStatementService(dbClient, statements)).register("/QueryStatement", new QueryStatementService(dbClient, statements)).register("/FlowControl", new FlowControlService(dbClient, statements)).register("/Mapper", new MapperService(dbClient, statements)).register("/Interceptor", new InterceptorService(DbClient.builder(dbConfig).addService(interceptorTestService).build(), statements, interceptorTestService)).register("/HealthCheck", new HealthCheckService(dbClient, dbConfig, statements)).build();
    // Prepare routing for the server
    final WebServer.Builder serverBuilder = WebServer.builder().routing(routing).config(config.get("server"));
    final WebServer server = serverBuilder.addMediaSupport(JsonpSupport.create()).addMediaSupport(JsonbSupport.create()).build();
    exitResource.setServer(server);
    // Start the server and print some info.
    server.start().thenAccept(ws -> {
        System.out.println("WEB server is up! http://localhost:" + ws.port() + "/");
    });
    // Server threads are not daemon. NO need to block. Just react.
    server.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    return server;
}
Also used : TransactionGetService(io.helidon.tests.integration.dbclient.appl.transaction.TransactionGetService) LogConfig(io.helidon.common.LogConfig) Config(io.helidon.config.Config) HealthSupport(io.helidon.health.HealthSupport) TransactionQueriesService(io.helidon.tests.integration.dbclient.appl.transaction.TransactionQueriesService) DmlStatementService(io.helidon.tests.integration.dbclient.appl.statement.DmlStatementService) TransactionUpdateService(io.helidon.tests.integration.dbclient.appl.transaction.TransactionUpdateService) SimpleQueryService(io.helidon.tests.integration.dbclient.appl.simple.SimpleQueryService) TransactionInsertService(io.helidon.tests.integration.dbclient.appl.transaction.TransactionInsertService) GetStatementService(io.helidon.tests.integration.dbclient.appl.statement.GetStatementService) QueryStatementService(io.helidon.tests.integration.dbclient.appl.statement.QueryStatementService) DbClient(io.helidon.dbclient.DbClient) HealthCheckService(io.helidon.tests.integration.dbclient.appl.health.HealthCheckService) SimpleInsertService(io.helidon.tests.integration.dbclient.appl.simple.SimpleInsertService) Routing(io.helidon.webserver.Routing) SimpleGetService(io.helidon.tests.integration.dbclient.appl.simple.SimpleGetService) SimpleDeleteService(io.helidon.tests.integration.dbclient.appl.simple.SimpleDeleteService) ExitService(io.helidon.tests.integration.dbclient.appl.tools.ExitService) DbClientService(io.helidon.dbclient.DbClientService) WebServer(io.helidon.webserver.WebServer) TransactionDeleteService(io.helidon.tests.integration.dbclient.appl.transaction.TransactionDeleteService) InterceptorService(io.helidon.tests.integration.dbclient.appl.interceptor.InterceptorService) SimpleUpdateService(io.helidon.tests.integration.dbclient.appl.simple.SimpleUpdateService) FlowControlService(io.helidon.tests.integration.dbclient.appl.result.FlowControlService) MapperService(io.helidon.tests.integration.dbclient.appl.mapping.MapperService)

Example 8 with DbClient

use of io.helidon.dbclient.DbClient in project helidon by oracle.

the class InterceptorIT method testStatementInterceptor.

/**
 * Check that statement interceptor was called before statement execution.
 */
@Test
public void testStatementInterceptor() {
    TestClientService interceptor = new TestClientService();
    DbClient dbClient = initDbClient(interceptor);
    Multi<DbRow> rows = dbClient.execute(exec -> exec.createNamedQuery("select-pokemon-named-arg").addParam("name", POKEMONS.get(6).getName()).execute());
    assertThat(interceptor.called(), equalTo(true));
}
Also used : DbRow(io.helidon.dbclient.DbRow) DbClient(io.helidon.dbclient.DbClient) Test(org.junit.jupiter.api.Test)

Example 9 with DbClient

use of io.helidon.dbclient.DbClient in project helidon by oracle.

the class JdbcExampleMain method createRouting.

/**
 * Creates new {@link io.helidon.webserver.Routing}.
 *
 * @param config configuration of this server
 * @return routing configured with JSON support, a health check, and a service
 */
private static Routing createRouting(Config config) {
    Config dbConfig = config.get("db");
    // Client services are added through a service loader - see mongoDB example for explicit services
    DbClient dbClient = DbClient.builder(dbConfig).build();
    // Some relational databases do not support DML statement as ping so using query which works for all of them
    HealthSupport health = HealthSupport.builder().addLiveness(DbClientHealthCheck.create(dbClient, dbConfig.get("health-check"))).build();
    return Routing.builder().register(// Health at "/health"
    health).register(// Metrics at "/metrics"
    MetricsSupport.create()).register("/db", new PokemonService(dbClient)).build();
}
Also used : DbClient(io.helidon.dbclient.DbClient) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) HealthSupport(io.helidon.health.HealthSupport)

Example 10 with DbClient

use of io.helidon.dbclient.DbClient in project helidon by oracle.

the class MongoDbExampleMain method createRouting.

/**
 * Creates new {@link io.helidon.webserver.Routing}.
 *
 * @param config configuration of this server
 * @return routing configured with JSON support, a health check, and a service
 */
private static Routing createRouting(Config config) {
    Config dbConfig = config.get("db");
    DbClient dbClient = DbClient.builder(dbConfig).addService(DbClientMetrics.counter().statementNames("select-all", "select-one")).addService(DbClientMetrics.timer().statementTypes(DbStatementType.DELETE, DbStatementType.UPDATE, DbStatementType.INSERT)).addService(DbClientTracing.create()).build();
    HealthSupport health = HealthSupport.builder().addLiveness(DbClientHealthCheck.create(dbClient, dbConfig.get("health-check"))).build();
    return Routing.builder().register(// Health at "/health"
    health).register(// Metrics at "/metrics"
    MetricsSupport.create()).register("/db", new PokemonService(dbClient)).build();
}
Also used : DbClient(io.helidon.dbclient.DbClient) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) HealthSupport(io.helidon.health.HealthSupport)

Aggregations

DbClient (io.helidon.dbclient.DbClient)10 Config (io.helidon.config.Config)9 LogConfig (io.helidon.common.LogConfig)5 Test (org.junit.jupiter.api.Test)5 HealthSupport (io.helidon.health.HealthSupport)4 TimeUnit (java.util.concurrent.TimeUnit)4 Routing (io.helidon.webserver.Routing)2 WebServer (io.helidon.webserver.WebServer)2 DbClientService (io.helidon.dbclient.DbClientService)1 DbRow (io.helidon.dbclient.DbRow)1 HealthCheckService (io.helidon.tests.integration.dbclient.appl.health.HealthCheckService)1 InterceptorService (io.helidon.tests.integration.dbclient.appl.interceptor.InterceptorService)1 MapperService (io.helidon.tests.integration.dbclient.appl.mapping.MapperService)1 FlowControlService (io.helidon.tests.integration.dbclient.appl.result.FlowControlService)1 SimpleDeleteService (io.helidon.tests.integration.dbclient.appl.simple.SimpleDeleteService)1 SimpleGetService (io.helidon.tests.integration.dbclient.appl.simple.SimpleGetService)1 SimpleInsertService (io.helidon.tests.integration.dbclient.appl.simple.SimpleInsertService)1 SimpleQueryService (io.helidon.tests.integration.dbclient.appl.simple.SimpleQueryService)1 SimpleUpdateService (io.helidon.tests.integration.dbclient.appl.simple.SimpleUpdateService)1 DmlStatementService (io.helidon.tests.integration.dbclient.appl.statement.DmlStatementService)1