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;
}
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;
}
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));
}
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();
}
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();
}
Aggregations