Java blocking / non-blocking


The Talares library is created with asynchronous, non-blocking usage in mind. Therefore regular usage should result in a Promise of a result in stead of the concrete result. This will make sure that threads are not blocked while waiting for a response from the webservice. A user can then supply a Function that defines what should happen once the result is received and apply this to the Promise through the use of it's map() method. This will result in yet another Promise.

Users are encouraged to leverage this way of working as it consumes the least amount of system resources and will therefore be able to out perform blocking alternatives.

There is, however, nothing stopping users from writing traditional, imperative, blocking code using the Talares library. Every asynchronous method has a blocking alternative easily identified by the underscore suffix. If writing functional Java is not desired and the adopting project has a sufficiently large thread pool, users can simply leverage the blocking alternative for every method that returns a Promise.

Blocking methods will use a timeout in milliseconds within which the result should be present. This timeout can either be provided explicitly in the method call or be configured in the application.conf file. If none of these are supplied the default of 5000 milliseconds will be used.

Example usage

// Non blocking using Java 7
F.Promise<Page> pagePromise = talares.getPage(1, 2);
return pagePromise.map(new F.Function<Page, Integer>() {
  @Override
  public Integer apply(Page page) throws Throwable {
    if (page == null) return 0;
    else return page.getItemId();
  }
});

// Non blocking using Java 8
F.Promise<Page> pagePromise = talares.getPage(1, 2);
return pagePromise.map(Page::getItemId);

// Blocking with explicit timeout
try {
  Page page = talares.getPage_(1, 2, 1000)
  if (page == null) return 0;
  else return page.getItemId();
} catch (TalaresException te) {
  System.err.print(te.getMessage());
}

// Blocking using timeout specified in configuration file
try {
  Page page = talares.getPage_(1, 2)
  if (page == null) return 0;
  else return page.getItemId();
} catch (TalaresException te) {
  System.err.print(te.getMessage());
}

Talares uses the tools provided by Play Framework for functional Java code. More information can be found in their documentation.