What is the level of asynchronism on Play! framework

Play! touts is its asynchronous HTTP processing function, although it’s not very clear to me what is really asynchronous (without blocking and without switching threads.) In the asynchronous examples I read, like the game below! Framework cookbook:

public static void generateInvoice(Long orderId) { Order order = Order.findById(orderId); // #a InputStream is = await(new OrderAsPdfJob(order).now()); // #b renderBinary(is); } 

They focus on the long / expensive “business logic” step on #b, but I'm worried about DB calls on #a. In fact, most controller methods in many applications will simply try to do some CRUD for DB, for example:

 public static void generateInvoice(Long orderId) { Order order = Order.findById(orderId); // #a render(order); } 

I am particularly concerned about the requirement to use a "small number of threads" when serving a database access pattern.

So the questions are:

  • Will play! will block JDBC calls?
  • If we complete such calls in the future / promise / expect, this will cause thread switching (besides the inconvenience due to the ubiquity of database calls), right?
  • In light of this, how is its asynchronism compared to a servlet server with an NIO connector (for example, Tomcat + NIO connector, but without using a new event handler) in servicing this database access pattern?
  • Is there any plan to support the asynchronous DB driver, for example http://code.google.com/p/adbcj/ ?
+4
source share
1 answer
  • Play will be blocked when calling JDBC - there is no magic to prevent this.
  • To wrap jucFuture in F.Promise for Play, you need a loop. This can lead to many context switches.
  • Servlet containers can use NIO, for example. so that connections open between requests without thread binding for inactive connections. But a JDBC call in the request processing code will block and bind the stream in exactly the same way.
  • ADBCJ implements jucFuture but also supports callbacks that can be attached to F.Promise, see https://groups.google.com/d/topic/play-framework/c4DOOtGF50c/discussion .

I'm not sure if the play async function is worth it, considering how much this complicates the code and testing. Perhaps if you need to process thousands of requests per second on one machine when calling slow services.

+1
source

Source: https://habr.com/ru/post/1393434/


All Articles