[[java-rest-high-x-pack-ml-forecast-job]]
=== Forecast Job API

The Forecast Job API provides the ability to forecast a {ml} job's behavior based
on historical data.
It accepts a `ForecastJobRequest` object and responds
with a `ForecastJobResponse` object.

[[java-rest-high-x-pack-ml-forecast-job-request]]
==== Forecast Job Request

A `ForecastJobRequest` object gets created with an existing non-null `jobId`.
All other fields are optional for the request.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-forecast-job-request]
--------------------------------------------------
<1> Constructing a new request referencing an existing `jobId`

==== Optional Arguments

The following arguments are optional.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-forecast-job-request-options]
--------------------------------------------------
<1> Set when the forecast for the job should expire
<2> Set how far into the future should the forecast predict

[[java-rest-high-x-pack-ml-forecast-job-execution]]
==== Execution

The request can be executed through the `MachineLearningClient` contained
in the `RestHighLevelClient` object, accessed via the `machineLearningClient()` method.

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-forecast-job-execute]
--------------------------------------------------

[[java-rest-high-x-pack-ml-forecast-job-execution-async]]
==== Asynchronous Execution

The request can also be executed asynchronously:

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-forecast-job-execute-async]
--------------------------------------------------
<1> The `ForecastJobRequest` to execute and the `ActionListener` to use when
the execution completes

The method does not block and returns immediately. The passed `ActionListener` is used
to notify the caller of completion. A typical `ActionListener` for `ForecastJobResponse` may
look like

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-forecast-job-listener]
--------------------------------------------------
<1> `onResponse` is called back when the action is completed successfully
<2> `onFailure` is called back when some unexpected error occurs

[[java-rest-high-x-pack-ml-forecast-job-response]]
==== Forecast Job Response

A `ForecastJobResponse` contains an acknowledgement and the forecast ID

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-forecast-job-response]
--------------------------------------------------
<1> `isAcknowledged()` indicates if the forecast was successful
<2> `getForecastId()` provides the ID of the forecast that was created