Execution context
Execution context
Data Client Base library uses parallel and asynchronous processing wherever
possible to utilize resources best. It uses futures for internal handling (like
de/serialization, encoding, decoding). Futures are run in an execution context.
If your user application does not deal with dedicated execution contexts Data
Client Base library uses Scala's ExecutionContext.Implicit.global. For further
reading about this context please see
here. If your
application uses some custom execution context or you want to control the
execution context for some other reason, so you can. Please see the example
below how to pass custom execution context to BaseClient.
object WorkingWithBaseClientMainContext {
implicit val myCustomExecutionContext: ExecutionContextExecutor = ExecutionContext.global
def main(args: Array[String]): Unit = {
val client = BaseClient() // this takes the implicit exec parameter
val configApi = client.of[ConfigApi]
val result: CatalogsResultBase = configApi
.getCatalogs(verbose = Some(false))
.toEntity()
println(s"response: $result")
}
}public class JavaWorkingWithBaseClientMainContext {
private static ExecutorService myCustomExecutor = Executors.newFixedThreadPool(10);
public static void main(String[] args) {
BaseClient baseClient =
new BaseClientJava.Builder().withExecutorService(myCustomExecutor).build();
ConfigApi configApi = new ConfigApi(baseClient);
CatalogsListResult listResult =
(CatalogsListResult)
configApi.getCatalogs().withVerbose(Optional.of(true)).build().toEntity();
System.out.println(listResult);
}
}Data Client Base library uses OkHttp stack
for low-level HTTP processing. OkHttp is a Java-based library which uses its own
execution context for IO. If you do not have any demand to control the OkHttp
execution context it will map to Java's ForkJoinPool.commonPool(). If you want
to control OkHttp's execution context you can pass it as ioExecutorService to
the BaseClient. Please see the example below.
object WorkingWithBaseClientMainIoExec {
implicit val myCustomExecutionContext: ExecutionContextExecutor = ExecutionContext.global
implicit val myIOExecutorService: ExecutorService =
ExecutionContext.fromExecutorService(ForkJoinPool.commonPool())
def main(args: Array[String]): Unit = {
val client = BaseClient() // this takes the implicit ioExec parameter
val configApi = client.of[ConfigApi]
val result: CatalogsResultBase = configApi
.getCatalogs(verbose = Some(false))
.toEntity()
println(s"response: $result")
}
}public class JavaWorkingWithBaseClientMainIoExec {
private static ExecutorService myCustomIoExecutor = Executors.newFixedThreadPool(10);
public static void main(String[] args) {
BaseClient baseClient =
new BaseClientJava.Builder().withIOExecutorService(myCustomIoExecutor).build();
ConfigApi configApi = new ConfigApi(baseClient);
CatalogsListResult listResult =
(CatalogsListResult)
configApi.getCatalogs().withVerbose(Optional.of(true)).build().toEntity();
System.out.println(listResult);
}
}Of course you can combine both examples above and pass both, execution context
and ioExecutorService to the BaseClient to full control the usage of
execution contexts.
Updated 21 days ago