Browse Source

jooby: v0.16.0
* upgrade to latest version of jooby
* update/rewrite existing tests

Edgar Espina 9 years ago
parent
commit
98e45c8a21

+ 12 - 1
frameworks/Java/jooby/conf/application.conf

@@ -1,6 +1,17 @@
+# add or override properties
+# See https://github.com/typesafehub/config/blob/master/HOCON.md for more details
+
 application.env = prod
 application.env = prod
 
 
+## server threads
+server.threads.Min = ${runtime.processors}
+server.threads.Max = ${runtime.processors-x2}
+
+# default DBHOST
+DBHOST = localhost
+
+## database
 db.url = "jdbc:mysql://"${DBHOST}":3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true"
 db.url = "jdbc:mysql://"${DBHOST}":3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true"
 db.user = benchmarkdbuser
 db.user = benchmarkdbuser
 db.password = benchmarkdbpass
 db.password = benchmarkdbpass
-hikari.maximumPoolSize = 100
+hikari.maximumPoolSize = ${server.threads.Max}

+ 3 - 3
frameworks/Java/jooby/pom.xml

@@ -7,7 +7,7 @@
   <parent>
   <parent>
     <groupId>org.jooby</groupId>
     <groupId>org.jooby</groupId>
     <artifactId>jooby-project</artifactId>
     <artifactId>jooby-project</artifactId>
-    <version>0.14.0</version>
+    <version>0.16.0</version>
   </parent>
   </parent>
 
 
   <artifactId>jooby</artifactId>
   <artifactId>jooby</artifactId>
@@ -15,10 +15,9 @@
   <version>1.0</version>
   <version>1.0</version>
 
 
   <name>jooby</name>
   <name>jooby</name>
-  <description>generated by Jooby archetype</description>
 
 
   <properties>
   <properties>
-    <jooby.version>0.14.0</jooby.version>
+    <jooby.version>0.16.0</jooby.version>
 
 
     <!-- Startup class -->
     <!-- Startup class -->
     <application.class>com.techempower.App</application.class>
     <application.class>com.techempower.App</application.class>
@@ -37,6 +36,7 @@
       <artifactId>jooby-jackson</artifactId>
       <artifactId>jooby-jackson</artifactId>
     </dependency>
     </dependency>
 
 
+    <!-- jdbi -->
     <dependency>
     <dependency>
       <groupId>org.jooby</groupId>
       <groupId>org.jooby</groupId>
       <artifactId>jooby-jdbi</artifactId>
       <artifactId>jooby-jdbi</artifactId>

+ 38 - 50
frameworks/Java/jooby/src/main/java/com/techempower/App.java

@@ -1,20 +1,20 @@
 package com.techempower;
 package com.techempower;
 
 
-import org.jooby.Jooby;
-import org.jooby.MediaType;
-import org.jooby.Results;
-import org.jooby.Result;
-import org.jooby.json.Jackson;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.Locale;
-import java.nio.charset.StandardCharsets;
 import java.time.Instant;
 import java.time.Instant;
 import java.time.ZoneId;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeFormatter;
+import java.util.Locale;
+import java.util.concurrent.ThreadLocalRandom;
+
+import org.jooby.Jooby;
+import org.jooby.MediaType;
+import org.jooby.Result;
+import org.jooby.Results;
 import org.jooby.jdbi.Jdbi;
 import org.jooby.jdbi.Jdbi;
+import org.jooby.json.Jackson;
 import org.skife.jdbi.v2.Handle;
 import org.skife.jdbi.v2.Handle;
-import java.util.Random;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 
 /**
 /**
  * @author jooby generator
  * @author jooby generator
@@ -28,56 +28,44 @@ public class App extends Jooby {
   static final String H_SERVER = "Server";
   static final String H_SERVER = "Server";
   static final String SERVER = "Netty";
   static final String SERVER = "Netty";
 
 
-  static final String H_DATE= "Date";
-
-  static final String helloWorld = "Hello, World!";
+  static final String H_DATE = "Date";
 
 
   static final int DB_ROWS = 10000;
   static final int DB_ROWS = 10000;
 
 
+  static final String HELLO_WORLD = "Hello, World!";
+
   {
   {
-    use(new Jackson());
+    /** json via jackson .*/
+    ObjectMapper mapper = new ObjectMapper();
+    use(new Jackson(mapper));
+
+    /** database via jdbi .*/
     use(new Jdbi());
     use(new Jdbi());
 
 
-    get("*", (req, rsp) -> {
-      rsp.header(H_SERVER, SERVER)
-         .header(H_DATE, fmt.format(Instant.ofEpochMilli(System.currentTimeMillis())));
-    });
-
-    /**
-     * Plain text response. Please note all these lines can be just:
-     *
-     *  get("/plaintext", () -> "Hello, World!");
-     *
-     *  This way we will get just a few extra ms.
-     */
-    byte[] plaintextBytes = helloWorld.getBytes(StandardCharsets.UTF_8);
-    Result plaintext = Results
-        .with(plaintextBytes)
-        .type(MediaType.plain);
-
-    get("/plaintext", () -> plaintext);
-
-    /**
-     * json response.
-     */
-    Map<String, Object> hash = new HashMap<>();
-    hash.put("message", helloWorld);
-    Result json = Results
-        .with(hash)
-        .type(MediaType.json);
-
-    get("/json", () -> json);
+    get("/plaintext", () -> result(HELLO_WORLD, MediaType.text))
+        .renderer("text");
+
+    get("/json", () -> result(mapper.createObjectNode().put("message", HELLO_WORLD), MediaType.json))
+        .renderer("json");
 
 
     get("/db", req -> {
     get("/db", req -> {
       try (Handle handle = req.require(Handle.class)) {
       try (Handle handle = req.require(Handle.class)) {
-        Random rnd = new Random();
-        int id = rnd.nextInt(DB_ROWS);
-        return handle.createQuery("select * from World where id = :id")
-          .bind("id", id)
-          .map((idx, rs, ctx) -> new World(rs.getInt("id"), rs.getInt("randomNumber")))
-          .first();
+        int id = ThreadLocalRandom.current().nextInt(DB_ROWS + 1);
+        return result(
+            handle.createQuery("select * from World where id = :id")
+                .bind("id", id)
+                .map((idx, rs, ctx) -> new World(rs.getInt("id"), rs.getInt("randomNumber")))
+                .first(),
+            MediaType.json);
       }
       }
-  });
+    }).renderer("json");
+
+  }
+
+  private Result result(final Object value, final MediaType type) {
+    return Results.ok(value).type(type)
+        .header(H_SERVER, SERVER)
+        .header(H_DATE, fmt.format(Instant.ofEpochMilli(System.currentTimeMillis())));
   }
   }
 
 
   public static void main(final String[] args) throws Exception {
   public static void main(final String[] args) throws Exception {