|
@@ -3,7 +3,12 @@ package hello;
|
|
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
|
|
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
|
|
import hello.domain.World;
|
|
import hello.domain.World;
|
|
|
|
|
|
|
|
+import java.util.Map;
|
|
import java.util.Random;
|
|
import java.util.Random;
|
|
|
|
+import java.util.concurrent.Callable;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
|
+import java.util.concurrent.Future;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
import javax.ws.rs.DefaultValue;
|
|
import javax.ws.rs.DefaultValue;
|
|
@@ -29,16 +34,30 @@ public class DbResource {
|
|
|
|
|
|
@GET
|
|
@GET
|
|
@Produces(APPLICATION_JSON + "; charset=utf-8")
|
|
@Produces(APPLICATION_JSON + "; charset=utf-8")
|
|
- public Object db(@QueryParam("queries") @DefaultValue("1") final int queries) {
|
|
|
|
|
|
+ public Object db(@QueryParam("queries") @DefaultValue("1") final int queries)
|
|
|
|
+ throws ExecutionException, InterruptedException {
|
|
|
|
+
|
|
final World[] worlds = new World[queries];
|
|
final World[] worlds = new World[queries];
|
|
final Random random = ThreadLocalRandom.current();
|
|
final Random random = ThreadLocalRandom.current();
|
|
final Session session = sessionFactory.openSession();
|
|
final Session session = sessionFactory.openSession();
|
|
-
|
|
|
|
|
|
+ session.setDefaultReadOnly(true);
|
|
|
|
+
|
|
|
|
+ Map<Integer, Future<World>> futureWorlds = new ConcurrentHashMap<>();
|
|
|
|
+ for (int i = 0; i < queries; i++) {
|
|
|
|
+ futureWorlds.put(i, Common.EXECUTOR.submit(
|
|
|
|
+ new Callable<World>() {
|
|
|
|
+ @Override
|
|
|
|
+ public World call() throws Exception {
|
|
|
|
+ return (World) session.byId(World.class).load(random.nextInt(DB_ROWS) + 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ));
|
|
|
|
+ }
|
|
|
|
+
|
|
for (int i = 0; i < queries; i++) {
|
|
for (int i = 0; i < queries; i++) {
|
|
- worlds[i] = (World) session.byId(World.class).load(random.nextInt(DB_ROWS) + 1);
|
|
|
|
|
|
+ worlds[i] = futureWorlds.get(i).get();
|
|
}
|
|
}
|
|
-
|
|
|
|
- session.close();
|
|
|
|
|
|
+
|
|
return queries == 1 ? worlds[0] : worlds;
|
|
return queries == 1 ? worlds[0] : worlds;
|
|
}
|
|
}
|
|
|
|
|