execution.asciidoc 2.5 KB

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