Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ratpack JVM_MX Meetup February 2016

1,010 views

Published on

Introduction to Ratpack

Published in: Technology
  • Be the first to comment

Ratpack JVM_MX Meetup February 2016

  1. 1. Ratpack @jvm_mx Meetup / CDMX / February 2016
  2. 2. Ratpack
  3. 3. Ratpack • Originally written in Groovy • Now a Java 8 code base project. • It uses Apache Netty • Apache 2.0 Licensed • Fully Groovy support!
  4. 4. demo.groovy @Grab('org.slf4j:slf4j-simple:1.7.16') @Grab('io.ratpack:ratpack-groovy:1.2.0') import static ratpack.groovy.Groovy.ratpack ratpack { handlers { get(":name") { render "Hello $pathTokens.name!" } get { render "Hello World!" } } }
  5. 5. Java project demo plugins { id 'io.ratpack.ratpack-java' version '1.2.0' } apply plugin: 'idea' repositories { jcenter() } dependencies { runtime 'org.slf4j:slf4j-simple:1.7.16' testCompile 'junit:junit:4.12' } mainClassName = 'org.groovyando.Main'
  6. 6. Ratpack 101 • Ratpack uses Apache Netty, an async event- driven network application framework. • Ratpack is not Servlet API-based, so don’t expect things HttpServletRequest or HttpSession.
  7. 7. Ratpack key features • Strongly typed • Non blocking
  8. 8. What is blocking?
  9. 9. Blocking • Any IO operation • InputStream • Lock.lock() • JDBC • 99% of all APIs out there!!
  10. 10. Contention = threads > cores
  11. 11. Blocking and threads Each concurrent piece of work requires a separate thread. 100 concurrent requests =~ 100 threads
  12. 12. C10K Problem? https://en.wikipedia.org/wiki/C10k_problem The C10k problem is the problem of optimising network sockets to handle a large number of clients at the same time. The name C10k is a numeronym for concurrently handling ten thousand connections. Note that concurrent connections are not the same as requests per second, though they are similar: handling many requests per second requires high throughput (processing them quickly), while high number of concurrent connections requires efficient scheduling of connections.
  13. 13. Ratpack components • Launch Configuration • Handlers • Registry
  14. 14. Handlers • Handlers as a mix of a Servlet and a Filter. • Each handler receives a Context object to interact with the request & response objects, the chain, etc.
  15. 15. Handlers • Handlers are functions composed in a handler chain. A handler can do one of the following: • Respond to the request. • Delegate to the next handler in the chain. • Insert more handlers in the chain and delegate to them.
  16. 16. Bad IO handling package org.groovyando; import ratpack.server.RatpackServer; import java.nio.file.Files; import java.nio.file.Paths; public class Main { public static void main(String... args) throws Exception { RatpackServer.start(s -> s .handlers(chain -> chain .get(ctx -> { String fileContent = new String(Files .readAllBytes(Paths.get("build.gradle"))); ctx.render(fileContent); })) ); } }
  17. 17. Better IO Handling package org.groovyando; import ratpack.exec.Blocking; import ratpack.server.RatpackServer; import java.nio.file.Files; import java.nio.file.Paths; public class Main { public static void main(String... args) throws Exception { RatpackServer.start(s -> s .handlers(chain -> chain .get(ctx -> { Blocking.get(() -> new String(Files .readAllBytes(Paths.get("build.gradle")))) .then(data -> ctx.render(data)); })) ); } }
  18. 18. Better usage of Promises! package org.groovyando; import ratpack.exec.Blocking; import ratpack.server.RatpackServer; import java.nio.file.Files; import java.nio.file.Paths; public class Main { public static void main(String... args) throws Exception { RatpackServer.start(s -> s .handlers(chain -> chain .get(ctx -> { ctx.render(Blocking.get(() -> new String(Files .readAllBytes(Paths.get("build.gradle"))))); })) ); } }
  19. 19. Promise Representation of a potential value.
  20. 20. Promise • Returns a simple value • Very different to RxJava
  21. 21. Using Promises Promise .<String>of(downstream -> { //puedes regresar un valor downstream.success("valor de retorno"); //o puedes fallar downstream.error(new RuntimeException()); }) .onError(throwable -> System.out.print("error por acá")) .then(System.out::println);
  22. 22. Promises in Action! Promise .<String>of(downstream -> { //puedes regresar un valor downstream.success("valor de retorno"); //o puedes fallar downstream.error(new RuntimeException()); downstream.complete(); }) .onError(throwable -> System.out.print("error por acá")) .map(s -> s.toLowerCase()) .flatMap(s -> Blocking.get(() -> "demo" + s).map(s1 -> s1.toLowerCase())) .cache() .then(System.out::println);
  23. 23. Promises are not a free lunch 1.You can not use the typical try/catch 2.What about ThreadLocal? 3.Most libs aren’t non blocking. 4.Debugging is a PITA (segmentation of stack traces)
  24. 24. Deal with the issues (1..3) • Use ratpack.exec.Execution
  25. 25. try/catch ratpack.exec.Execution.fork() .onError(throwable -> { //global errorHandler for this execution throwable.printStackTrace(); }) .start(execution -> { new String(Files.readAllBytes(Paths.get("build.gradle"))); });
  26. 26. Dealing with ThreadLocal ratpack.exec.Execution.fork() .start(execution -> { Execution.current().add(Integer.class, 0); Blocking .get(() -> Execution.current().get(Integer.class)) .then(System.out::println); });
  27. 27. Show me the code!
  28. 28. holagus.com

×