Browse Source

Merge pull request #1569 from victorbriz/master

Java: Updating vert.x
Hamilton Turner 10 years ago
parent
commit
bb1589bcb1

+ 2 - 2
frameworks/Java/vertx/README.md

@@ -12,7 +12,7 @@ This is the vertx portion of a [benchmarking test suite](../) comparing a variet
 ## Versions
 ## Versions
 
 
 * [Java OpenJDK 1.7.0_09](http://openjdk.java.net/)
 * [Java OpenJDK 1.7.0_09](http://openjdk.java.net/)
-* [vertx 1.3.1](http://vertx.io/)
+* [vertx 2.1.5](http://vertx.io/)
 
 
 
 
 ## Test URLs
 ## Test URLs
@@ -23,4 +23,4 @@ This is the vertx portion of a [benchmarking test suite](../) comparing a variet
 
 
 ### Database Mapping Test
 ### Database Mapping Test
 
 
-    http://localhost:8080/db?queries=5
+    http://localhost:8080/db?queries=5

+ 54 - 31
frameworks/Java/vertx/WebServer.java

@@ -17,11 +17,34 @@ import java.util.concurrent.ThreadLocalRandom;
 
 
 public class WebServer extends Verticle implements Handler<HttpServerRequest> {
 public class WebServer extends Verticle implements Handler<HttpServerRequest> {
 
 
-  private Buffer helloWorldBuffer = new Buffer("Hello, World!");
-  private String helloWorldContentLength = String.valueOf(helloWorldBuffer.length());
-  private DateFormat DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyyy HH:mm:ss z");
+  private final Buffer helloWorldBuffer = new Buffer("Hello, World!");
+  private final String helloWorldContentLength = String.valueOf(helloWorldBuffer.length());
+  private final DateFormat DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyyy HH:mm:ss z");
   private String dateString;
   private String dateString;
 
 
+  private final String PATH_PLAINTEXT = "/plaintext";
+  private final String PATH_JSON = "/json";
+  private final String PATH_DB = "/db";
+  private final String PATH_QUERIES = "/queries";
+  private final String RESPONSE_TYPE_PLAIN = "text/plain";
+  private final String RESPONSE_TYPE_JSON = "application/json";
+  private final String HEADER_CONTENT_TYPE = "Content-Type";
+  private final String HEADER_CONTENT_LENGTH = "Content-Length";
+  private final String HEADER_SERVER = "Server";
+  private final String HEADER_SERVER_VERTX = "vert.x";
+  private final String HEADER_DATE = "Date";
+  private final String MONGO_ADDRESS = "hello.persistor";
+  private final String UNDERSCORE_ID = "_id";
+  private final String TEXT_ID = "id";
+  private final String TEXT_RESULT = "result";
+  private final String TEXT_QUERIES = "queries";
+  private final String TEXT_MESSAGE = "message";
+  private final String TEXT_ACTION = "action";
+  private final String TEXT_FINDONE = "findone";
+  private final String TEXT_COLLECTION = "collection";
+  private final String TEXT_WORLD = "World";
+  private final String TEXT_MATCHER = "matcher";
+
   @Override
   @Override
   public void start() {
   public void start() {
     vertx.createHttpServer().requestHandler(WebServer.this).listen(8080);
     vertx.createHttpServer().requestHandler(WebServer.this).listen(8080);
@@ -36,18 +59,17 @@ public class WebServer extends Verticle implements Handler<HttpServerRequest> {
 
 
   @Override
   @Override
   public void handle(HttpServerRequest req) {
   public void handle(HttpServerRequest req) {
-    String path = req.path();
-    switch (path) {
-      case "/plaintext":
+    switch (req.path()) {
+      case PATH_PLAINTEXT:
         handlePlainText(req);
         handlePlainText(req);
         break;
         break;
-      case "/json":
+      case PATH_JSON:
         handleJson(req);
         handleJson(req);
         break;
         break;
-      case "/db":
+      case PATH_DB:
         handleDbMongo(req);
         handleDbMongo(req);
         break;
         break;
-      case "/queries":
+      case PATH_QUERIES:
         handleQueriesMongo(req);
         handleQueriesMongo(req);
         break;
         break;
       default:
       default:
@@ -62,14 +84,14 @@ public class WebServer extends Verticle implements Handler<HttpServerRequest> {
 
 
   private void handlePlainText(HttpServerRequest req) {
   private void handlePlainText(HttpServerRequest req) {
     HttpServerResponse resp = req.response();
     HttpServerResponse resp = req.response();
-    setHeaders(resp, "text/plain", helloWorldContentLength);
+    setHeaders(resp, RESPONSE_TYPE_PLAIN, helloWorldContentLength);
     resp.end(helloWorldBuffer);
     resp.end(helloWorldBuffer);
   }
   }
 
 
   private void handleJson(HttpServerRequest req) {
   private void handleJson(HttpServerRequest req) {
-    Buffer buff = new Buffer(Json.encode(Collections.singletonMap("message", "Hello, world!")));
+    Buffer buff = new Buffer(Json.encode(Collections.singletonMap(TEXT_MESSAGE, "Hello, world!")));
     HttpServerResponse resp = req.response();
     HttpServerResponse resp = req.response();
-    setHeaders(resp, "application/json", String.valueOf(buff.length()));
+    setHeaders(resp, RESPONSE_TYPE_JSON, String.valueOf(buff.length()));
     resp.end(buff);
     resp.end(buff);
   }
   }
 
 
@@ -86,25 +108,26 @@ public class WebServer extends Verticle implements Handler<HttpServerRequest> {
 
 
   private JsonObject getResultFromReply(Message<JsonObject> reply) {
   private JsonObject getResultFromReply(Message<JsonObject> reply) {
     JsonObject body = reply.body();
     JsonObject body = reply.body();
-    JsonObject world = body.getObject("result");
-    Object id = world.removeField("_id");
-    world.putValue("id", id);
+    JsonObject world = body.getObject(TEXT_RESULT);
+    Object id = world.removeField(UNDERSCORE_ID);
+    if (id instanceof Double) {
+		world.putValue(TEXT_ID, Integer.valueOf(((Double)id).intValue()));
+	} else {
+		world.putValue(TEXT_ID, id);
+	}
     return world;
     return world;
   }
   }
 
 
   private void handleQueriesMongo(final HttpServerRequest req) {
   private void handleQueriesMongo(final HttpServerRequest req) {
     int queriesParam = 1;
     int queriesParam = 1;
     try {
     try {
-      queriesParam = Integer.parseInt(req.params().get("queries"));
+      queriesParam = Integer.parseInt(req.params().get(TEXT_QUERIES));
     } catch (NumberFormatException e) {
     } catch (NumberFormatException e) {
-      e.printStackTrace();
-    }
-    if (queriesParam < 1)
-    {
       queriesParam = 1;
       queriesParam = 1;
     }
     }
-    if (queriesParam > 500)
-    {
+    if (queriesParam < 1) {
+      queriesParam = 1;
+    } else if (queriesParam > 500) {
       queriesParam = 500;
       queriesParam = 500;
     }
     }
     final MongoHandler dbh = new MongoHandler(req, queriesParam);
     final MongoHandler dbh = new MongoHandler(req, queriesParam);
@@ -116,26 +139,26 @@ public class WebServer extends Verticle implements Handler<HttpServerRequest> {
 
 
   private void findRandom(Random random, Handler<Message<JsonObject>> handler) {
   private void findRandom(Random random, Handler<Message<JsonObject>> handler) {
     vertx.eventBus().send(
     vertx.eventBus().send(
-        "hello.persistor",
+        MONGO_ADDRESS,
         new JsonObject()
         new JsonObject()
-            .putString("action", "findone")
-            .putString("collection", "World")
-            .putObject("matcher", new JsonObject().putNumber("_id", (random.nextInt(10000) + 1))),
+            .putString(TEXT_ACTION, TEXT_FINDONE)
+            .putString(TEXT_COLLECTION, TEXT_WORLD)
+            .putObject(TEXT_MATCHER, new JsonObject().putNumber(UNDERSCORE_ID, (random.nextInt(10000) + 1))),
         handler);
         handler);
   }
   }
 
 
   private void sendResponse(HttpServerRequest req, String result) {
   private void sendResponse(HttpServerRequest req, String result) {
     Buffer buff = new Buffer(result);
     Buffer buff = new Buffer(result);
     HttpServerResponse resp = req.response();
     HttpServerResponse resp = req.response();
-    setHeaders(resp, "application/json", String.valueOf(buff.length()));
+    setHeaders(resp, RESPONSE_TYPE_JSON, String.valueOf(buff.length()));
     resp.end(buff);
     resp.end(buff);
   }
   }
 
 
   private void setHeaders(HttpServerResponse resp, String contentType, String contentLength) {
   private void setHeaders(HttpServerResponse resp, String contentType, String contentLength) {
-    resp.putHeader("Content-Type", contentType);
-    resp.putHeader("Content-Length", contentLength);
-    resp.putHeader("Server", "vert.x");
-    resp.putHeader("Date", dateString);
+    resp.putHeader(HEADER_CONTENT_TYPE, contentType);
+    resp.putHeader(HEADER_CONTENT_LENGTH, contentLength);
+    resp.putHeader(HEADER_SERVER, HEADER_SERVER_VERTX );
+    resp.putHeader(HEADER_DATE, dateString);
   }
   }
 
 
   private class MongoHandler implements Handler<Message<JsonObject>> {
   private class MongoHandler implements Handler<Message<JsonObject>> {

+ 2 - 1
frameworks/Java/vertx/app.js

@@ -3,7 +3,8 @@ var container = require('vertx/container')
 var persistorConf = {
 var persistorConf = {
   address: 'hello.persistor',
   address: 'hello.persistor',
   db_name: 'hello_world',
   db_name: 'hello_world',
-  host: 'localhost'
+  host: '127.0.0.1',
+  pool_size: 20
 }
 }
 
 
 container.deployModule('io.vertx~mod-mongo-persistor~2.1.1', persistorConf, function (err, dep_id) {
 container.deployModule('io.vertx~mod-mongo-persistor~2.1.1', persistorConf, function (err, dep_id) {

+ 9 - 1
frameworks/Java/vertx/install.sh

@@ -1,3 +1,11 @@
 #!/bin/bash
 #!/bin/bash
 
 
-fw_depends java7 vertx 
+fw_depends java7
+
+RETCODE=$(fw_exists ${IROOT}/vert.x-2.1.5.installed)
+[ ! "$RETCODE" == 0 ] || { return 0; }
+
+fw_get http://dl.bintray.com/vertx/downloads/vert.x-2.1.5.tar.gz?direct=true -O vert.x-2.1.5.tar.gz
+fw_untar vert.x-2.1.5.tar.gz
+
+touch ${IROOT}/vert.x-2.1.5.installed

+ 1 - 1
frameworks/Java/vertx/setup.sh

@@ -4,4 +4,4 @@ source $IROOT/java7.installed
 
 
 sed -i 's|host: \x27.*\x27|host: \x27'"${DBHOST}"'\x27|g' app.js
 sed -i 's|host: \x27.*\x27|host: \x27'"${DBHOST}"'\x27|g' app.js
 
 
-${IROOT}/vert.x-2.1.1/bin/vertx run app.js &
+${IROOT}/vert.x-2.1.5/bin/vertx run app.js &

+ 1 - 1
frameworks/Java/vertx/source_code

@@ -1,2 +1,2 @@
-./vertx/App.groovy
+./vertx/app.js
 ./vertx/WebServer.java
 ./vertx/WebServer.java

+ 4 - 4
toolset/setup/linux/frameworks/vertx.sh

@@ -1,9 +1,9 @@
 #!/bin/bash
 #!/bin/bash
 
 
-RETCODE=$(fw_exists ${IROOT}/vert.x-2.1.1.installed)
+RETCODE=$(fw_exists ${IROOT}/vert.x-2.1.5.installed)
 [ ! "$RETCODE" == 0 ] || { return 0; }
 [ ! "$RETCODE" == 0 ] || { return 0; }
 
 
-fw_get http://dl.bintray.com/vertx/downloads/vert.x-2.1.1.tar.gz?direct=true -O vert.x-2.1.1.tar.gz
-fw_untar vert.x-2.1.1.tar.gz
+fw_get http://dl.bintray.com/vertx/downloads/vert.x-2.1.5.tar.gz?direct=true -O vert.x-2.1.5.tar.gz
+fw_untar vert.x-2.1.5.tar.gz
 
 
-touch ${IROOT}/vert.x-2.1.1.installed
+touch ${IROOT}/vert.x-2.1.5.installed