Skip to content

Commit

Permalink
Add Dropwizard-JDBC example resource
Browse files Browse the repository at this point in the history
  • Loading branch information
circlespainter committed Oct 22, 2015
1 parent 0e840fa commit b5921fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Expand Up @@ -2,6 +2,7 @@

import co.paralleluniverse.fibers.dropwizard.FiberApplication;
import co.paralleluniverse.fibers.dropwizard.FiberDBIFactory;
import co.paralleluniverse.fibers.dropwizard.FiberDataSourceFactory;
import co.paralleluniverse.fibers.dropwizard.FiberHttpClientBuilder;
import com.example.helloworld.resources.HelloWorldResource;
import io.dropwizard.setup.Bootstrap;
Expand Down Expand Up @@ -32,15 +33,16 @@ public void fiberRun(HelloWorldConfiguration configuration,
using(configuration.getHttpClientConfiguration()).
build("FiberHttpClient");

final IDBI jdbi = new FiberDBIFactory().build(environment, configuration.getDatabase(), "postgresql");
final IDBI jdbi = new FiberDBIFactory().build(environment, configuration.getDatabase(), "jdbiDB");
final MyDAO dao = jdbi.onDemand(MyDAO.class);

final HelloWorldResource helloWorldResource = new HelloWorldResource(
configuration.getTemplate(),
configuration.getDefaultName(),
fhc,
jdbi,
dao
dao,
new FiberDataSourceFactory(configuration.getDatabase()).build(environment.metrics(), "jdbcDB")
);
environment.jersey().register(helloWorldResource);
}
Expand Down
Expand Up @@ -7,12 +7,18 @@
import com.example.helloworld.core.Saying;
import com.google.common.base.Optional;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicLong;
import javax.sql.DataSource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
Expand All @@ -24,21 +30,22 @@
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
final String ALREADY_EXISTS_ERROR = "X0Y32";
private final String template;
private final String defaultName;
private final AtomicLong counter;
private final HttpClient httpClient;
private final MyDAO dao;
private final IDBI jdbi;
private final DataSource ds;

public HelloWorldResource(String template, String defaultName, HttpClient httpClient, IDBI jdbi, MyDAO dao) {
public HelloWorldResource(String template, String defaultName, HttpClient httpClient, IDBI jdbi, MyDAO dao, DataSource jdbcDB) {
this.template = template;
this.defaultName = defaultName;
this.counter = new AtomicLong();
this.httpClient = httpClient;
this.dao = dao;
this.jdbi = jdbi;
this.ds = jdbcDB;
}

@GET
Expand Down Expand Up @@ -112,4 +119,21 @@ public String daoQuery(@QueryParam("id") Optional<Integer> id) throws Interrupte
String first = dao.findNameById(id.or(1));
return first != null ? first : null;
}

@GET
@Path("/db/jdbc")
@Timed
public String jdbcQuery(@QueryParam("id") Optional<Integer> id) throws InterruptedException, SuspendExecution, SQLException {
String res = null;
try (final Connection c = ds.getConnection()) {
try (final PreparedStatement ps = c.prepareStatement("select name from something where id = ?")) {
ps.setInt(1, id.or(1));
try (final ResultSet rs = ps.executeQuery()) {
if (rs.next())
res = rs.getString(1);
}
}
}
return res;
}
}

0 comments on commit b5921fb

Please sign in to comment.