public class Exec extends Object implements Jooby.Module
Manage the life cycle of ExecutorService and build async apps, schedule tasks, etc...
...
import org.jooby.exec.Exec;
...
{
use(new Exec());
get("/", req -> {
ExecutorService executor = req.require(ExecutorService.class);
// work with executor
});
}
The default executor is a Executors.newFixedThreadPool(int) with threads defined by
Runtime.availableProcessors()
The default ExecutorService is nice and give you something that just works out of the
box. But, what if you need to control the number of threads?
Explicit control is provided via executors which allow the following syntax:
type (= int)? (, daemon (= boolean)? )? (, priority (= int)? )?
Let's see some examples:
# fixed thread pool with a max number of threads equals to the available runtime processors executors = "fixed"
# fixed thread pool with a max number of 10 threads executors = "fixed = 10"
# fixed thread pool with a max number of 10 threads executors = "fixed = 10"
# scheduled thread pool with a max number of 10 threads executors = "scheduled = 10"
# cached thread pool with daemon threads and max priority executors = "cached, daemon = true, priority = 10"
# forkjoin thread pool with asyncMode executors = "forkjoin, asyncMode = true"
Multiple executors are provided by expanding the executors properties, like:
executors {
pool1: fixed
jobs: forkjoin
}
Later, you can request your executor like:
{
get("/", req -> {
ExecutorService pool1 = req.require("pool1", ExecutorService.class);
ExecutorService jobs = req.require("jobs", ExecutorService.class);
});
}
Any ExecutorService created by this module will automatically shutdown on application
shutdown time.
| Constructor and Description |
|---|
Exec() |
| Modifier and Type | Method and Description |
|---|---|
com.typesafe.config.Config |
config() |
void |
configure(Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder) |
Exec |
daemon(boolean daemon)
Defined the default value for daemon.
|
Exec |
priority(int priority)
Defined the default value for priority.
|
public Exec daemon(boolean daemon)
truedaemon - True for default daemon.public Exec priority(int priority)
Thread.NORM_PRIORITY.priority - One of Thread.MIN_PRIORITY, Thread.NORM_PRIORITY or
Thread.MAX_PRIORITY.public com.typesafe.config.Config config()
config in interface Jooby.Modulepublic void configure(Env env, com.typesafe.config.Config conf, com.google.inject.Binder binder)
configure in interface Jooby.ModuleCopyright © 2017. All rights reserved.