|
@@ -13,7 +13,6 @@ import javax.annotation.Resource;
|
|
|
import org.redkale.net.http.*;
|
|
|
import org.redkale.service.AbstractService;
|
|
|
import org.redkale.source.*;
|
|
|
-import org.redkale.util.AnyValue;
|
|
|
|
|
|
/**
|
|
|
*
|
|
@@ -24,30 +23,19 @@ public class Service extends AbstractService {
|
|
|
|
|
|
private static final byte[] helloBytes = "Hello, world!".getBytes();
|
|
|
|
|
|
- private final Random random = new Random();
|
|
|
+ private final ThreadLocal<Random> localRandom = ThreadLocal.withInitial(Random::new);
|
|
|
|
|
|
@Resource
|
|
|
private DataSource source;
|
|
|
|
|
|
-
|
|
|
- @RestMapping(name = "json")
|
|
|
- public Message getHelloMessage() {
|
|
|
- return new Message("Hello, World!");
|
|
|
- }
|
|
|
-
|
|
|
@RestMapping(name = "plaintext")
|
|
|
public byte[] getHelloBytes() {
|
|
|
return helloBytes;
|
|
|
}
|
|
|
|
|
|
- @RestMapping(name = "cached-worlds")
|
|
|
- public CachedWorld[] cachedWorlds(int q) {
|
|
|
- final int size = Math.min(500, Math.max(1, q));
|
|
|
- final CachedWorld[] worlds = new CachedWorld[size];
|
|
|
- for (int i = 0; i < size; i++) {
|
|
|
- worlds[i] = source.find(CachedWorld.class, randomId());
|
|
|
- }
|
|
|
- return worlds;
|
|
|
+ @RestMapping(name = "json")
|
|
|
+ public Message getHelloMessage() {
|
|
|
+ return new Message("Hello, World!");
|
|
|
}
|
|
|
|
|
|
@RestMapping(name = "db")
|
|
@@ -55,23 +43,21 @@ public class Service extends AbstractService {
|
|
|
return source.findAsync(World.class, randomId());
|
|
|
}
|
|
|
|
|
|
- @RestMapping(name = "fortunes")
|
|
|
- public CompletableFuture<HttpResult<String>> queryFortunes() {
|
|
|
- return source.queryListAsync(Fortune.class).thenApply((fortunes) -> {
|
|
|
- fortunes.add(new Fortune(0, "Additional fortune added at request time."));
|
|
|
- Collections.sort(fortunes);
|
|
|
- String html = FortunesTemplate.template(fortunes).render().toString();
|
|
|
- return new HttpResult("text/html; charset=UTF-8", html);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
@RestMapping(name = "queries")
|
|
|
public CompletableFuture<World[]> queryWorldAsync(int q) {
|
|
|
final int size = Math.min(500, Math.max(1, q));
|
|
|
+ final Random random = localRandom.get();
|
|
|
final World[] worlds = new World[size];
|
|
|
+
|
|
|
+// final CompletableFuture[] futures = new CompletableFuture[size];
|
|
|
+// for (int i = 0; i < size; i++) {
|
|
|
+// final int index = i;
|
|
|
+// futures[i] = source.findAsync(World.class, randomId(random)).thenAccept(v -> worlds[index] = v);
|
|
|
+// }
|
|
|
+// return CompletableFuture.allOf(futures).thenApply(v -> worlds);
|
|
|
final AtomicInteger index = new AtomicInteger();
|
|
|
- final Function<?, CompletableFuture> func = f -> source.findAsync(World.class, randomId())
|
|
|
- .thenAccept((World v) -> worlds[index.getAndIncrement()] = v);
|
|
|
+ final Function<?, CompletableFuture> func = f -> source.findAsync(World.class, randomId(random))
|
|
|
+ .thenAccept(v -> worlds[index.getAndIncrement()] = v);
|
|
|
CompletableFuture future = func.apply(null);
|
|
|
for (int i = 1; i < size; i++) {
|
|
|
future = future.thenCompose(func);
|
|
@@ -82,25 +68,60 @@ public class Service extends AbstractService {
|
|
|
@RestMapping(name = "updates")
|
|
|
public CompletableFuture<World[]> updateWorldAsync(int q) {
|
|
|
final int size = Math.min(500, Math.max(1, q));
|
|
|
+ final Random random = localRandom.get();
|
|
|
final World[] worlds = new World[size];
|
|
|
+
|
|
|
+// final CompletableFuture[] futures = new CompletableFuture[size];
|
|
|
+// for (int i = 0; i < size; i++) {
|
|
|
+// final int index = i;
|
|
|
+// futures[i] = source.findAsync(World.class, randomId(random)).thenAccept(v -> worlds[index] = v.randomNumber(randomId(random)));
|
|
|
+// }
|
|
|
+// return CompletableFuture.allOf(futures).thenCompose(v -> source.updateAsync(sort(worlds))).thenApply(v -> worlds);
|
|
|
final AtomicInteger index = new AtomicInteger();
|
|
|
- final Function<?, CompletableFuture> func = f -> source.findAsync(World.class, randomId())
|
|
|
- .thenApply((World v) -> {
|
|
|
- worlds[index.getAndIncrement()] = v;
|
|
|
- v.setRandomNumber(randomId());
|
|
|
- return v;
|
|
|
- });
|
|
|
+ final Function<?, CompletableFuture> func = f -> source.findAsync(World.class, randomId(random))
|
|
|
+ .thenAccept(v -> worlds[index.getAndIncrement()] = v.randomNumber(randomId(random)));
|
|
|
CompletableFuture future = func.apply(null);
|
|
|
for (int i = 1; i < size; i++) {
|
|
|
future = future.thenCompose(func);
|
|
|
}
|
|
|
- return future.thenCompose(v -> {
|
|
|
- Arrays.sort(worlds);
|
|
|
- return source.updateAsync(worlds);
|
|
|
- }).thenApply(v -> worlds);
|
|
|
+ return future.thenCompose(v -> source.updateAsync(sort(worlds))).thenApply(v -> worlds);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RestMapping(name = "fortunes")
|
|
|
+ public CompletableFuture<HttpResult<String>> queryFortunes() {
|
|
|
+ return source.queryListAsync(Fortune.class).thenApply((fortunes) -> {
|
|
|
+ fortunes.add(new Fortune(0, "Additional fortune added at request time."));
|
|
|
+ String html = FortunesTemplate.template(sort(fortunes)).render().toString();
|
|
|
+ return new HttpResult("text/html; charset=utf-8", html);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @RestMapping(name = "cached-worlds")
|
|
|
+ public CachedWorld[] cachedWorlds(int q) {
|
|
|
+ final int size = Math.min(500, Math.max(1, q));
|
|
|
+ final Random random = localRandom.get();
|
|
|
+ final CachedWorld[] worlds = new CachedWorld[size];
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ worlds[i] = source.find(CachedWorld.class, randomId(random));
|
|
|
+ }
|
|
|
+ return worlds;
|
|
|
+ }
|
|
|
+
|
|
|
+ private World[] sort(World[] words) {
|
|
|
+ Arrays.sort(words);
|
|
|
+ return words;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Fortune> sort(List<Fortune> fortunes) {
|
|
|
+ Collections.sort(fortunes);
|
|
|
+ return fortunes;
|
|
|
}
|
|
|
|
|
|
private int randomId() {
|
|
|
+ return 1 + localRandom.get().nextInt(10000);
|
|
|
+ }
|
|
|
+
|
|
|
+ private int randomId(Random random) {
|
|
|
return 1 + random.nextInt(10000);
|
|
|
}
|
|
|
}
|