実行コンテキスト

データ クライアント ベース ライブラリでは、可能な限り並列および非同期の処理を使用して、リソースを最適に利用します。 内部処理 ( デシリアライゼーション、エンコード、デコードなど ) には先物が使用されます。 先物は実行コンテキストで実行されます。 ユーザーアプリケーションが専用の実行コンテキストを処理しない場合、データ クライアント ベース ライブラリは Scala を使用 ExecutionContext.Implicit.globalします。 このコンテキストの詳細については 、 HEREを参照してください。 アプリケーションが何らかのカスタム実行コンテキストを使用している場合、または何らかの理由で実行コンテキストを制御したい場合は、次の操作を行うことができます。 カスタム実行コンテキストをに渡す方法については、次の例 BaseClientを参照してください。

Scala
Java
object WorkingWithBaseClientMainContext {

  implicit val myCustomExecutionContext = 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);
  }
}

データ クライアント ベース ライブラリは 、低レベルの HTTP 処理に OkHttp スタックを使用します。 OkHttp は Java ベースのライブラリで、 IO に独自の実行コンテキストを使用します。 OkHttp 実行コンテキストを制御する必要がない場合は、 Java にマッピング ForkJoinPool.commonPool()されます。 OkHttp の実行コンテキストを制御する場合は、BaseClientにそのコンテキストをioExecutorService渡すことができます。 以下の例を参照してください。

Scala
Java
object WorkingWithBaseClientMainIoExec {

  implicit val myCustomExecutionContext = 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);
  }
}

もちろん、上記の両方の例を結合して、実行コンテキストと ioExecutorService の両方をに渡し BaseClient 、実行コンテキストの使用を完全に制御できます。

」に一致する結果は 件です

    」に一致する結果はありません