Last active
September 25, 2015 05:21
-
-
Save jamisonhyatt/f105fd8e91ae0c76a607 to your computer and use it in GitHub Desktop.
dropwizard-comsat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template: Hello, %s! | |
defaultName: Stranger | |
server: | |
maxThreads: 200 | |
minThreads: 200 | |
maxQueuedRequests: 9999 | |
requestLog: | |
appenders: [] | |
httpClient: | |
maxConnectionsPerRoute: 9999 | |
maxConnections: 9999 | |
database: | |
driverClass: | |
co.paralleluniverse.fibers.jdbc.FiberDriver | |
url: | |
jdbc:fiber:derby:build/mydb;user=root;password=root;create=true | |
# for mysql use url such as this | |
# jdbc:fiber:mysql://localhost:3306/testjdbi?zeroDateTimeBehavior=convertToNull | |
properties: | |
charSet: UTF-8 | |
# the maximum amount of time to wait on an empty pool before throwing an exception | |
maxWaitForConnection: 1s | |
user: root | |
password: root | |
# the SQL query to run when validating a connection's liveness | |
validationQuery: | |
"/* MyService Health Check */ VALUES 1" | |
# for mysql use validationQuery such as this | |
# "/* MyService Health Check */ SELECT 1" | |
# the minimum number of connections to keep open | |
minSize: 8 | |
# the maximum number of connections to keep open | |
maxSize: 32 | |
# whether or not idle connections should be validated | |
checkConnectionWhileIdle: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>hello-world</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>io.dropwizard</groupId> | |
<artifactId>dropwizard-core</artifactId> | |
<version>0.8.2</version> | |
</dependency> | |
<dependency> | |
<groupId>co.paralleluniverse</groupId> | |
<artifactId>quasar-core</artifactId> | |
<version>0.7.3</version> | |
</dependency> | |
<dependency> | |
<groupId>co.paralleluniverse</groupId> | |
<artifactId>comsat-dropwizard</artifactId> | |
<version>0.5.0</version> | |
</dependency> | |
<dependency> | |
<groupId>co.paralleluniverse</groupId> | |
<artifactId>comsat-jdbc</artifactId> | |
<version>0.5.0</version> | |
</dependency> | |
<dependency> | |
<groupId>co.paralleluniverse</groupId> | |
<artifactId>comsat-httpclient</artifactId> | |
<version>0.5.0</version> | |
</dependency> | |
</dependencies> | |
<properties> | |
<!-- use UTF-8 for everything --> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
</properties> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.1</version> | |
<!-- compile for Java 1.8 --> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
<encoding>UTF-8</encoding> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-shade-plugin</artifactId> | |
<version>2.3</version> | |
<configuration> | |
<createDependencyReducedPom>true</createDependencyReducedPom> | |
<filters> | |
<filter> | |
<artifact>*:*</artifact> | |
<excludes> | |
<exclude>META-INF/*.SF</exclude> | |
<exclude>META-INF/*.DSA</exclude> | |
<exclude>META-INF/*.RSA</exclude> | |
</excludes> | |
</filter> | |
</filters> | |
</configuration> | |
<executions> | |
<execution> | |
<phase>package</phase> | |
<goals> | |
<goal>shade</goal> | |
</goals> | |
<configuration> | |
<transformers> | |
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> | |
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | |
<mainClass>com.example.helloworld.HelloWorldApplication</mainClass> | |
</transformer> | |
</transformers> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-jar-plugin</artifactId> | |
<version>2.4</version> | |
<configuration> | |
<archive> | |
<manifest> | |
<addDefaultImplementationEntries>true</addDefaultImplementationEntries> | |
</manifest> | |
</archive> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.helloworld.core; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import org.hibernate.validator.constraints.Length; | |
public class Saying { | |
private long id; | |
@Length(max = 3) | |
private String content; | |
public Saying() { | |
// Jackson deserialization | |
} | |
public Saying(long id, String content) { | |
this.id = id; | |
this.content = content; | |
} | |
@JsonProperty | |
public long getId() { | |
return id; | |
} | |
@JsonProperty | |
public String getContent() { | |
return content; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.helloworld.health; | |
import com.codahale.metrics.health.HealthCheck; | |
public class TemplateHealthCheck extends HealthCheck { | |
private final String template; | |
public TemplateHealthCheck(String template) { | |
this.template = template; | |
} | |
@Override | |
protected Result check() throws Exception { | |
final String saying = String.format(template, "TEST"); | |
if (!saying.contains("TEST")) { | |
return Result.unhealthy("template doesn't include a name"); | |
} | |
return Result.healthy(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.helloworld; | |
import co.paralleluniverse.fibers.dropwizard.FiberApplication; | |
import co.paralleluniverse.fibers.dropwizard.FiberDBIFactory; | |
import co.paralleluniverse.fibers.dropwizard.FiberHttpClientBuilder; | |
import com.example.helloworld.resources.HelloWorldResource; | |
import io.dropwizard.setup.Bootstrap; | |
import io.dropwizard.setup.Environment; | |
import java.util.Arrays; | |
import org.apache.http.client.HttpClient; | |
import org.skife.jdbi.v2.IDBI; | |
public class HelloWorldApplication extends FiberApplication<HelloWorldConfiguration> { | |
public static void main(String[] args) throws Exception { | |
new HelloWorldApplication().run(args); | |
} | |
@Override | |
public String getName() { | |
return "hello-world"; | |
} | |
@Override | |
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) { | |
} | |
@Override | |
public void fiberRun(HelloWorldConfiguration configuration, | |
final Environment environment) throws ClassNotFoundException { | |
final HttpClient fhc = new FiberHttpClientBuilder(environment). | |
using(configuration.getHttpClientConfiguration()). | |
build("FiberHttpClient"); | |
final IDBI jdbi = new FiberDBIFactory().build(environment, configuration.getDatabase(), "postgresql"); | |
final MyDAO dao = jdbi.onDemand(MyDAO.class); | |
final HelloWorldResource helloWorldResource = new HelloWorldResource( | |
configuration.getTemplate(), | |
configuration.getDefaultName(), | |
fhc, | |
jdbi, | |
dao | |
); | |
environment.jersey().register(helloWorldResource); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.helloworld; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import io.dropwizard.Configuration; | |
import io.dropwizard.client.HttpClientConfiguration; | |
import io.dropwizard.db.DataSourceFactory; | |
import javax.validation.Valid; | |
import javax.validation.constraints.NotNull; | |
import org.hibernate.validator.constraints.NotEmpty; | |
public class HelloWorldConfiguration extends Configuration { | |
@NotEmpty | |
private String template; | |
@NotEmpty | |
private String defaultName = "Stranger"; | |
@Valid | |
@NotNull | |
@JsonProperty | |
private final HttpClientConfiguration httpClient = new HttpClientConfiguration(); | |
@JsonProperty | |
public HttpClientConfiguration getHttpClientConfiguration() { | |
return httpClient; | |
} | |
@Valid | |
@NotNull | |
@JsonProperty | |
private final DataSourceFactory database = new DataSourceFactory(); | |
public DataSourceFactory getDatabase() { | |
return database; | |
} | |
@JsonProperty | |
public String getTemplate() { | |
return template; | |
} | |
@JsonProperty | |
public void setTemplate(String template) { | |
this.template = template; | |
} | |
@JsonProperty | |
public String getDefaultName() { | |
return defaultName; | |
} | |
@JsonProperty | |
public void setDefaultName(String name) { | |
this.defaultName = name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.helloworld; | |
import co.paralleluniverse.fibers.Suspendable; | |
import org.skife.jdbi.v2.sqlobject.Bind; | |
import org.skife.jdbi.v2.sqlobject.SqlQuery; | |
import org.skife.jdbi.v2.sqlobject.SqlUpdate; | |
@Suspendable | |
public interface MyDAO { | |
@SqlUpdate("create table something (id int primary key, name varchar(100))") | |
void createSomethingTable(); | |
@SqlUpdate("insert into something (id, name) values (:id, :name)") | |
void insert(@Bind("id") int id, @Bind("name") String name); | |
@SqlQuery("select name from something where id = :id") | |
String findNameById(@Bind("id") int id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.helloworld.resources; | |
import co.paralleluniverse.fibers.Fiber; | |
import co.paralleluniverse.fibers.SuspendExecution; | |
import com.codahale.metrics.annotation.Timed; | |
import com.example.helloworld.MyDAO; | |
import com.example.helloworld.core.Saying; | |
import com.google.common.base.Optional; | |
import java.io.IOException; | |
import java.util.concurrent.atomic.AtomicLong; | |
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; | |
import org.skife.jdbi.v2.Handle; | |
import org.skife.jdbi.v2.IDBI; | |
import org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException; | |
import org.skife.jdbi.v2.util.StringMapper; | |
@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; | |
public HelloWorldResource(String template, String defaultName, HttpClient httpClient, IDBI jdbi, MyDAO dao) { | |
this.template = template; | |
this.defaultName = defaultName; | |
this.counter = new AtomicLong(); | |
this.httpClient = httpClient; | |
this.dao = dao; | |
this.jdbi = jdbi; | |
} | |
@GET | |
@Timed | |
public Saying sayHello(@QueryParam("name") Optional<String> name, | |
@QueryParam("sleep") Optional<Integer> sleepParameter) throws InterruptedException, SuspendExecution { | |
final String value = String.format(template, name.or(defaultName)); | |
Fiber.sleep(sleepParameter.or(1000)); | |
return new Saying(counter.incrementAndGet(), value); | |
} | |
@GET | |
@Path("/http") | |
@Timed | |
public String sayGoodye(@QueryParam("name") Optional<String> name) throws InterruptedException, SuspendExecution { | |
final String value = String.format(template, name.or(defaultName)); | |
String resp; | |
try { | |
resp = EntityUtils.toString(httpClient.execute(new HttpGet("http://localhost:8080/hello-world?sleep=100")).getEntity()); | |
} catch (IOException ex) { | |
resp = ex.toString(); | |
} | |
return resp; | |
} | |
@GET | |
@Path("/db/create") | |
@Timed | |
public String createdb(@QueryParam("name") Optional<String> name) throws InterruptedException, SuspendExecution { | |
try (Handle h = jdbi.open()) { | |
try { | |
h.execute("create table something (id int primary key, name varchar(100))"); | |
return "table created..."; | |
} catch (UnableToExecuteStatementException t) { | |
h.execute("truncate table something"); // in case the table exists | |
return "table truncated..."; | |
} | |
} | |
} | |
@GET | |
@Path("/db/insert") | |
@Timed | |
public String insertdb(@QueryParam("name") Optional<String> name) throws InterruptedException, SuspendExecution { | |
try (Handle h = jdbi.open()) { | |
for (int i = 0; i < 100; i++) | |
h.execute("insert into something (id, name) values (?, ?)", i, name.or("stranger ") + i); | |
return "inserted"; | |
} catch (UnableToExecuteStatementException t) { | |
return "ignored"; | |
} | |
} | |
@GET | |
@Path("/db/query") | |
@Timed | |
public String query(@QueryParam("id") Optional<Integer> id) throws InterruptedException, SuspendExecution { | |
try (Handle h = jdbi.open()) { | |
String first = h.createQuery("select name from something where id = :id") | |
.bind("id", id) | |
.map(StringMapper.FIRST) | |
.first(); | |
return first != null ? first : null; | |
} | |
} | |
@GET | |
@Path("/db/dao") | |
@Timed | |
public String daoQuery(@QueryParam("id") Optional<Integer> id) throws InterruptedException, SuspendExecution { | |
String first = dao.findNameById(id.or(1)); | |
return first != null ? first : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment