1
0

execution-no-req.asciidoc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. ////
  2. This file is included by high level rest client API documentation pages
  3. where the client method does not use a request object.
  4. For methods with requests, see execution.asciidoc
  5. ////
  6. [id="{upid}-{api}-sync"]
  7. ==== Synchronous execution
  8. When executing the +{api}+ API in the following manner, the client waits
  9. for the +{response}+ to be returned before continuing with code execution:
  10. ["source","java",subs="attributes,callouts,macros"]
  11. --------------------------------------------------
  12. include-tagged::{doc-tests-file}[{api}-execute]
  13. --------------------------------------------------
  14. Synchronous calls may throw an `IOException` in case of either failing to
  15. parse the REST response in the high-level REST client, the request times out
  16. or similar cases where there is no response coming back from the server.
  17. In cases where the server returns a `4xx` or `5xx` error code, the high-level
  18. client tries to parse the response body error details instead and then throws
  19. a generic `ElasticsearchException` and adds the original `ResponseException` as a
  20. suppressed exception to it.
  21. [id="{upid}-{api}-async"]
  22. ==== Asynchronous execution
  23. The +{api}+ API can also be called in an asynchronous fashion so that
  24. the client can return directly. Users need to specify how the response or
  25. potential failures will be handled by passing a listener to the
  26. asynchronous {api} method:
  27. ["source","java",subs="attributes,callouts,macros"]
  28. --------------------------------------------------
  29. include-tagged::{doc-tests-file}[{api}-execute-async]
  30. --------------------------------------------------
  31. <1> The `RequestOptions` and `ActionListener` to use when the execution
  32. completes
  33. The asynchronous method does not block and returns immediately. Once it is
  34. completed the `ActionListener` is called back using the `onResponse` method
  35. if the execution successfully completed or using the `onFailure` method if
  36. it failed. Failure scenarios and expected exceptions are the same as in the
  37. synchronous execution case.
  38. A typical listener for +{api}+ looks like:
  39. ["source","java",subs="attributes,callouts,macros"]
  40. --------------------------------------------------
  41. include-tagged::{doc-tests-file}[{api}-execute-listener]
  42. --------------------------------------------------
  43. <1> Called when the execution is successfully completed.
  44. <2> Called when the +{api}+ call fails.