HelloHandler.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package hello.home.handler;
  2. import hello.*;
  3. import hello.home.entity.*;
  4. import java.util.*;
  5. import java.util.concurrent.*;
  6. import com.techempower.cache.*;
  7. import com.techempower.gemini.*;
  8. import com.techempower.gemini.path.*;
  9. import com.techempower.gemini.path.annotation.*;
  10. /**
  11. * Responds to the framework benchmarking requests for "hello, world" and
  12. * simple database queries.
  13. */
  14. public class HelloHandler
  15. extends BasicPathHandler<GhContext>
  16. {
  17. private static final int DB_ROWS = 10000;
  18. private final EntityStore store;
  19. /**
  20. * Constructor.
  21. */
  22. public HelloHandler(GeminiApplication app)
  23. {
  24. super(app, "hllo");
  25. this.store = app.getStore();
  26. }
  27. /**
  28. * Return "hello world" as a JSON-encoded message.
  29. */
  30. @PathDefault
  31. public boolean helloworld()
  32. {
  33. return message("Hello, World!");
  34. }
  35. /**
  36. * Return a list of World objects as JSON, selected randomly from the World
  37. * table. For consistency, we have assumed the table has 10,000 rows.
  38. */
  39. @PathSegment
  40. public boolean db()
  41. {
  42. final Random random = ThreadLocalRandom.current();
  43. final int queries = context().getInt("queries", 1, 1, 500);
  44. final World[] worlds = new World[queries];
  45. for (int i = 0; i < queries; i++)
  46. {
  47. worlds[i] = store.get(World.class, random.nextInt(DB_ROWS) + 1);
  48. }
  49. return json(worlds);
  50. }
  51. /**
  52. * Fetch the full list of Fortunes from the database, sort them by the
  53. * fortune message text, and then render the results to simple HTML using a
  54. * server-side template.
  55. */
  56. @PathSegment
  57. public boolean fortunes()
  58. {
  59. final List<Fortune> fortunes = store.list(Fortune.class);
  60. fortunes.add(new Fortune().setMessage("Additional fortune added at request time."));
  61. Collections.sort(fortunes);
  62. return mustache("fortunes", fortunes);
  63. }
  64. }