Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make Java/RestExpress to conform to #4622 (#4632)
* Add check to distinguish between db and multiple queries

* Handle non-numeric parameter value

* Add separate implementations for db and query tests for MySQL

* Disable SSL for MySQL connections

* Add separate handlers for db and queries for MongoDB
  • Loading branch information
zloster authored and NateBrady23 committed Apr 9, 2019
1 parent 42873e9 commit 1d83a3a
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 223 deletions.
8 changes: 4 additions & 4 deletions frameworks/Java/restexpress/benchmark_config.json
Expand Up @@ -4,8 +4,8 @@
"default": {
"json_url": "/restexpress/json",
"plaintext_url": "/restexpress/plaintext",
"db_url": "/restexpress/mongodb",
"query_url": "/restexpress/mongodb?queries=",
"db_url": "/restexpress/mongo/db",
"query_url": "/restexpress/mongo/query?queries=",
"port": 8080,
"approach": "Realistic",
"classification": "Micro",
Expand All @@ -23,8 +23,8 @@
"versus": "netty"
},
"mysql-raw": {
"db_url": "/restexpress/mysql",
"query_url": "/restexpress/mysql?queries=",
"db_url": "/restexpress/mysql/db",
"query_url": "/restexpress/mysql/query?queries=",
"port": 8080,
"approach": "Realistic",
"classification": "Micro",
Expand Down
Expand Up @@ -6,7 +6,7 @@ executor.threadPool.size = 300
mongodb.uri = mongodb://tfb-database:27017/hello_world?maxPoolSize=300

# A MySQL URI/Connection string
mysql.uri = jdbc:mysql://tfb-database:3306/hello_world
mysql.uri = jdbc:mysql://tfb-database:3306/hello_world?useSSL=false

# MySQL useConfigs value, See section "21.3.5.1.1. Properties Files for the useConfigs Option" of:
# http://dev.mysql.com/doc/refman/5.6/en/connector-j-reference-configuration-properties.html
Expand Down
45 changes: 21 additions & 24 deletions frameworks/Java/restexpress/src/main/java/hello/Main.java
Expand Up @@ -14,44 +14,41 @@
import com.strategicgains.restexpress.RestExpress;
import com.strategicgains.restexpress.util.Environment;

public class Main
{
public static void main(String[] args) throws Exception
{
public class Main {
public static void main(String[] args) throws Exception {
Configuration config = loadEnvironment(args);
RestExpress server = new RestExpress()
.setName("RestExpress Benchmark")
.setExecutorThreadCount(config.getExecutorThreadPoolSize())
.alias("HelloWorld", HelloWorld.class);
RestExpress server = new RestExpress().setName("RestExpress Benchmark")
.setExecutorThreadCount(config.getExecutorThreadPoolSize())
.alias("HelloWorld", HelloWorld.class);

server.uri("/restexpress/json", config.getJsonController())
.action("helloWorld", HttpMethod.GET);
server.uri("/restexpress/json", config.getJsonController()).action("helloWorld",
HttpMethod.GET);

server.uri("/restexpress/plaintext", config.getPlaintextController())
.action("helloWorld", HttpMethod.GET)
.noSerialization();
server.uri("/restexpress/plaintext", config.getPlaintextController())
.action("helloWorld", HttpMethod.GET).noSerialization();

server.uri("/restexpress/mysql", config.getMysqlController())
.method(HttpMethod.GET);
server.uri("/restexpress/mysql/db", config.getDbMysqlController()).method(HttpMethod.GET);

server.uri("/restexpress/mongodb", config.getMongodbController())
.method(HttpMethod.GET);
server.uri("/restexpress/mysql/query", config.getQueriesMysqlController()).method(
HttpMethod.GET);

server.uri("/restexpress/mongo/db", config.getDbMongodbController()).method(HttpMethod.GET);

server.uri("/restexpress/mongo/query", config.getQueriesMongodbController()).method(HttpMethod.GET);

server.addPostprocessor((request, response) -> {
response.addHeader("Server", "RestExpress");
response.addHeader("Date", DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC)));
response.addHeader("Date",
DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC)));
});

server.bind(config.getPort());
server.awaitShutdown();
}

private static Configuration loadEnvironment(String[] args)
throws FileNotFoundException,
IOException
{
if (args.length > 0)
{
private static Configuration loadEnvironment(String[] args) throws FileNotFoundException,
IOException {
if (args.length > 0) {
return Environment.from(args[0], Configuration.class);
}

Expand Down
@@ -1,10 +1,12 @@
package hello.config;

import hello.controller.DbMongodbController;
import hello.controller.DbMysqlController;
import hello.controller.JsonController;
import hello.controller.MongodbController;
import hello.controller.MysqlController;
import hello.controller.persistence.WorldsMongodbRepository;
import hello.controller.PlaintextController;
import hello.controller.QueriesMongodbController;
import hello.controller.QueriesMysqlController;
import hello.controller.persistence.WorldsMongodbRepository;

import java.util.Properties;

Expand All @@ -13,9 +15,7 @@
import com.strategicgains.restexpress.Format;
import com.strategicgains.restexpress.util.Environment;

public class Configuration
extends Environment
{
public class Configuration extends Environment {
private static final String DEFAULT_EXECUTOR_THREAD_POOL_SIZE = "20";

private static final String PORT_PROPERTY = "port";
Expand All @@ -29,76 +29,78 @@ public class Configuration
private int executorThreadPoolSize;

private JsonController jsonController;
private MysqlController mysqlController;
private MongodbController mongodbController;
private PlaintextController plaintextController;
private DbMysqlController dbMysqlController;
private QueriesMysqlController queriesMysqlController;
private DbMongodbController dbMongodbController;
private QueriesMongodbController queriesMongodbController;
private PlaintextController plaintextController;

@Override
protected void fillValues(Properties p)
{
protected void fillValues(Properties p) {
this.port = Integer.parseInt(p.getProperty(PORT_PROPERTY, "8080"));
this.defaultFormat = p.getProperty(DEFAULT_FORMAT_PROPERTY, Format.JSON);
this.baseUrl = p.getProperty(BASE_URL_PROPERTY, "http://localhost:" + String.valueOf(port));
this.executorThreadPoolSize = Integer.parseInt(p.getProperty(EXECUTOR_THREAD_POOL_SIZE, DEFAULT_EXECUTOR_THREAD_POOL_SIZE));
this.executorThreadPoolSize = Integer.parseInt(p.getProperty(EXECUTOR_THREAD_POOL_SIZE,
DEFAULT_EXECUTOR_THREAD_POOL_SIZE));
MongoConfig mongoSettings = new MongoConfig(p);
MysqlConfig mysqlSettings = new MysqlConfig(p);
initialize(mysqlSettings, mongoSettings);
}

private void initialize(MysqlConfig mysqlSettings, MongoConfig mongo)
{
private void initialize(MysqlConfig mysqlSettings, MongoConfig mongo) {
jsonController = new JsonController();
plaintextController = new PlaintextController();
mysqlController = new MysqlController(mysqlSettings.getDataSource());
WorldsMongodbRepository worldMongodbRepository = new WorldsMongodbRepository(mongo.getClient(), mongo.getDbName());
worldMongodbRepository.setIdentifierAdapter(new IdentiferAdapter<Long>()
{
plaintextController = new PlaintextController();
dbMysqlController = new DbMysqlController(mysqlSettings.getDataSource());
queriesMysqlController = new QueriesMysqlController(mysqlSettings.getDataSource());
WorldsMongodbRepository worldMongodbRepository = new WorldsMongodbRepository(
mongo.getClient(), mongo.getDbName());
worldMongodbRepository.setIdentifierAdapter(new IdentiferAdapter<Long>() {
@Override
public Long convert(String id) throws InvalidObjectIdException
{
public Long convert(String id) throws InvalidObjectIdException {
return Long.valueOf(id);
}
});
mongodbController = new MongodbController(worldMongodbRepository);
dbMongodbController = new DbMongodbController(worldMongodbRepository);
queriesMongodbController = new QueriesMongodbController(worldMongodbRepository);
}

public String getDefaultFormat()
{
public String getDefaultFormat() {
return defaultFormat;
}

public int getPort()
{
public int getPort() {
return port;
}

public String getBaseUrl()
{

public String getBaseUrl() {
return baseUrl;
}

public int getExecutorThreadPoolSize()
{

public int getExecutorThreadPoolSize() {
return executorThreadPoolSize;
}

public JsonController getJsonController()
{
public JsonController getJsonController() {
return jsonController;
}

public MysqlController getMysqlController()
{
return mysqlController;
public DbMysqlController getDbMysqlController() {
return dbMysqlController;
}

public MongodbController getMongodbController()
{
return mongodbController;
public QueriesMysqlController getQueriesMysqlController() {
return queriesMysqlController;
}

public PlaintextController getPlaintextController()
{
return plaintextController;
}
public DbMongodbController getDbMongodbController() {
return dbMongodbController;
}

public QueriesMongodbController getQueriesMongodbController() {
return queriesMongodbController;
}

public PlaintextController getPlaintextController() {
return plaintextController;
}
}
@@ -0,0 +1,27 @@
package hello.controller;

import hello.controller.persistence.WorldsMongodbRepository;

import java.util.concurrent.ThreadLocalRandom;

import com.strategicgains.restexpress.Request;
import com.strategicgains.restexpress.Response;

public class DbMongodbController {
// Database details.
private static final int DB_ROWS = 10000;

private WorldsMongodbRepository worldRepo;

public DbMongodbController(WorldsMongodbRepository worldsRepository) {
super();
this.worldRepo = worldsRepository;
}

public Object read(Request request, Response response) {
// Fetch some rows from the database.
final int random = 1 + ThreadLocalRandom.current().nextInt(DB_ROWS);

return worldRepo.find(random);
}
}
@@ -0,0 +1,47 @@
package hello.controller;

import hello.domain.World;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ThreadLocalRandom;

import javax.sql.DataSource;

import com.strategicgains.restexpress.Request;
import com.strategicgains.restexpress.Response;

public class DbMysqlController {
// Database details.
private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
private static final int DB_ROWS = 10000;

private DataSource mysqlDataSource;

public DbMysqlController(DataSource dataSource) {
super();
this.mysqlDataSource = dataSource;
}

public Object read(Request request, Response response) throws SQLException {
final int random = 1 + ThreadLocalRandom.current().nextInt(DB_ROWS);

World world = null;

// Fetch some rows from the database.
try (Connection conn = mysqlDataSource.getConnection()) {
try (PreparedStatement statement = conn.prepareStatement(DB_QUERY,
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
statement.setInt(1, random);
try (ResultSet results = statement.executeQuery()) {
results.next(); // Here the expectation is ONLY one
// result row
world = new World(results.getLong("id"), results.getInt("randomNumber"));
}
}
}
return world;
}
}

This file was deleted.

0 comments on commit 1d83a3a

Please sign in to comment.