Skip to content

Commit c4c52ff

Browse files
committed
Add documentation.
1 parent 884f970 commit c4c52ff

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,23 @@
2121
import org.elasticsearch.action.ActionListener;
2222
import org.elasticsearch.action.LatchedActionListener;
2323
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
24+
import org.elasticsearch.client.Request;
2425
import org.elasticsearch.client.RequestOptions;
26+
import org.elasticsearch.client.Response;
2527
import org.elasticsearch.client.RestHighLevelClient;
28+
import org.elasticsearch.client.watcher.AckWatchRequest;
29+
import org.elasticsearch.client.watcher.AckWatchResponse;
30+
import org.elasticsearch.client.watcher.ActionStatus;
31+
import org.elasticsearch.client.watcher.ActionStatus.AckStatus;
32+
import org.elasticsearch.client.watcher.WatchStatus;
2633
import org.elasticsearch.common.bytes.BytesArray;
2734
import org.elasticsearch.common.bytes.BytesReference;
2835
import org.elasticsearch.common.xcontent.XContentType;
2936
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
3037
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse;
3138
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
3239
import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse;
40+
import org.elasticsearch.rest.RestStatus;
3341

3442
import java.util.concurrent.CountDownLatch;
3543
import java.util.concurrent.TimeUnit;
@@ -132,4 +140,67 @@ public void onFailure(Exception e) {
132140
}
133141
}
134142

143+
public void testAckWatch() throws Exception {
144+
RestHighLevelClient client = highLevelClient();
145+
146+
{
147+
BytesReference watch = new BytesArray("{ \n" +
148+
" \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" +
149+
" \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" +
150+
" \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" +
151+
"}");
152+
PutWatchRequest putWatchRequest = new PutWatchRequest("my_watch_id", watch, XContentType.JSON);
153+
client.watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT);
154+
155+
// TODO: use the high-level REST client here once it supports 'execute watch'.
156+
Request executeWatchRequest = new Request("POST", "_xpack/watcher/watch/my_watch_id/_execute");
157+
executeWatchRequest.setJsonEntity("{ \"record_execution\": true }");
158+
Response executeResponse = client().performRequest(executeWatchRequest);
159+
assertEquals(RestStatus.OK.getStatus(), executeResponse.getStatusLine().getStatusCode());
160+
}
161+
162+
{
163+
//tag::ack-watch-execute
164+
AckWatchRequest request = new AckWatchRequest("my_watch_id", // <1>
165+
"logme", "emailme"); // <2>
166+
AckWatchResponse response = client.watcher().ackWatch(request, RequestOptions.DEFAULT);
167+
//end::ack-watch-execute
168+
169+
//tag::ack-watch-response
170+
WatchStatus watchStatus = response.getStatus();
171+
ActionStatus actionStatus = watchStatus.actionStatus("logme"); // <1>
172+
AckStatus.State ackState = actionStatus.ackStatus().state(); // <2>
173+
//end::ack-watch-response
174+
175+
assertEquals(AckStatus.State.ACKED, ackState);
176+
}
177+
178+
{
179+
AckWatchRequest request = new AckWatchRequest("my_watch_id");
180+
// tag::ack-watch-execute-listener
181+
ActionListener<AckWatchResponse> listener = new ActionListener<AckWatchResponse>() {
182+
@Override
183+
public void onResponse(AckWatchResponse response) {
184+
// <1>
185+
}
186+
187+
@Override
188+
public void onFailure(Exception e) {
189+
// <2>
190+
}
191+
};
192+
// end::ack-watch-execute-listener
193+
194+
// For testing, replace the empty listener by a blocking listener.
195+
final CountDownLatch latch = new CountDownLatch(1);
196+
listener = new LatchedActionListener<>(listener, latch);
197+
198+
// tag::ack-watch-execute-async
199+
client.watcher().ackWatchAsync(request, RequestOptions.DEFAULT, listener); // <1>
200+
// end::ack-watch-execute-async
201+
202+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
203+
}
204+
}
205+
135206
}

docs/java-rest/high-level/supported-apis.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,11 @@ The Java High Level REST Client supports the following Watcher APIs:
298298

299299
* <<java-rest-high-x-pack-watcher-put-watch>>
300300
* <<java-rest-high-x-pack-watcher-delete-watch>>
301+
* <<java-rest-high-watcher-ack-watch>>
301302

302303
include::watcher/put-watch.asciidoc[]
303304
include::watcher/delete-watch.asciidoc[]
305+
include::watcher/ack-watch.asciidoc[]
304306

305307
== Graph APIs
306308

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[[java-rest-high-watcher-ack-watch]]
2+
=== Ack Watch API
3+
4+
[[java-rest-high-watcher-ack-watch-execution]]
5+
==== Execution
6+
7+
{xpack-ref}/actions.html#actions-ack-throttle[Acknowledging a watch] enables you
8+
to manually throttle execution of a watch's actions. A watch can be acknowledged
9+
through the following request:
10+
11+
["source","java",subs="attributes,callouts,macros"]
12+
--------------------------------------------------
13+
include-tagged::{doc-tests}/WatcherDocumentationIT.java[ack-watch-execute]
14+
--------------------------------------------------
15+
<1> The ID of the watch to ack.
16+
<2> An optional list of IDs representing the watch actions that should be acked.
17+
If no action IDs are provided, then all of the watch's actions will be acked.
18+
19+
[[java-rest-high-watcher-ack-watch-response]]
20+
==== Response
21+
22+
The returned `AckWatchResponse` contains the new status of the requested watch:
23+
24+
["source","java",subs="attributes,callouts,macros"]
25+
--------------------------------------------------
26+
include-tagged::{doc-tests}/WatcherDocumentationIT.java[ack-watch-response]
27+
--------------------------------------------------
28+
<1> The status of a specific action that was acked.
29+
<2> The acknowledgement state of the action. If the action was successfully
30+
acked, this state will be equal to `AckStatus.State.ACKED`.
31+
32+
[[java-rest-high-watcher-ack-watch-async]]
33+
==== Asynchronous Execution
34+
35+
This request can be executed asynchronously:
36+
37+
["source","java",subs="attributes,callouts,macros"]
38+
--------------------------------------------------
39+
include-tagged::{doc-tests}/WatcherDocumentationIT.java[ack-watch-execute-async]
40+
--------------------------------------------------
41+
<1> The `AckWatchRequest` to execute and the `ActionListener` to use when
42+
the execution completes.
43+
44+
The asynchronous method does not block and returns immediately. Once the request
45+
completes, the `ActionListener` is called back using the `onResponse` method
46+
if the execution successfully completed or using the `onFailure` method if
47+
it failed.
48+
49+
A listener for `AckWatchResponse` can be constructed as follows:
50+
51+
["source","java",subs="attributes,callouts,macros"]
52+
--------------------------------------------------
53+
include-tagged::{doc-tests}/WatcherDocumentationIT.java[ack-watch-execute-listener]
54+
--------------------------------------------------
55+
<1> Called when the execution is successfully completed. The response is
56+
provided as an argument.
57+
<2> Called in case of failure. The raised exception is provided as an argument.

0 commit comments

Comments
 (0)