Skip to content

Commit b2a7e57

Browse files
committed
[HLRC] Add Start/Stop Watch Service APIs. (#34317)
1 parent 38a76b9 commit b2a7e57

File tree

10 files changed

+323
-3
lines changed

10 files changed

+323
-3
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
package org.elasticsearch.client;
2020

2121
import org.elasticsearch.action.ActionListener;
22+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2223
import org.elasticsearch.client.watcher.ActivateWatchRequest;
2324
import org.elasticsearch.client.watcher.ActivateWatchResponse;
2425
import org.elasticsearch.client.watcher.AckWatchRequest;
2526
import org.elasticsearch.client.watcher.AckWatchResponse;
27+
import org.elasticsearch.client.watcher.StartWatchServiceRequest;
28+
import org.elasticsearch.client.watcher.StopWatchServiceRequest;
2629
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
2730
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse;
2831
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
@@ -41,6 +44,59 @@ public final class WatcherClient {
4144
this.restHighLevelClient = restHighLevelClient;
4245
}
4346

47+
/**
48+
* Start the watch service
49+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-start.html">
50+
* the docs</a> for more.
51+
* @param request the request
52+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
53+
* @return the response
54+
* @throws IOException in case there is a problem sending the request or parsing back the response
55+
*/
56+
public AcknowledgedResponse startWatchService(StartWatchServiceRequest request, RequestOptions options) throws IOException {
57+
return restHighLevelClient.performRequestAndParseEntity(
58+
request, WatcherRequestConverters::startWatchService, options, AcknowledgedResponse::fromXContent, emptySet());
59+
}
60+
61+
/**
62+
* Asynchronously start the watch service
63+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-start.html">
64+
* the docs</a> for more.
65+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
66+
*/
67+
public void startWatchServiceAsync(StartWatchServiceRequest request, RequestOptions options,
68+
ActionListener<AcknowledgedResponse> listener) {
69+
restHighLevelClient.performRequestAsyncAndParseEntity(
70+
request, WatcherRequestConverters::startWatchService, options, AcknowledgedResponse::fromXContent, listener, emptySet());
71+
}
72+
73+
/**
74+
* Stop the watch service
75+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-start.html">
76+
* the docs</a> for more.
77+
* @param request the request
78+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
79+
* @return the response
80+
* @throws IOException in case there is a problem sending the request or parsing back the response
81+
*/
82+
public AcknowledgedResponse stopWatchService(StopWatchServiceRequest request, RequestOptions options) throws IOException {
83+
return restHighLevelClient.performRequestAndParseEntity(
84+
request, WatcherRequestConverters::stopWatchService, options, AcknowledgedResponse::fromXContent, emptySet());
85+
}
86+
87+
/**
88+
* Asynchronously stop the watch service
89+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-start.html">
90+
* the docs</a> for more.
91+
* @param request the request
92+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
93+
*/
94+
public void stopWatchServiceAsync(StopWatchServiceRequest request, RequestOptions options,
95+
ActionListener<AcknowledgedResponse> listener) {
96+
restHighLevelClient.performRequestAsyncAndParseEntity(
97+
request, WatcherRequestConverters::stopWatchService, options, AcknowledgedResponse::fromXContent, listener, emptySet());
98+
}
99+
44100
/**
45101
* Put a watch into the cluster
46102
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-put-watch.html">

client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,40 @@
2020
package org.elasticsearch.client;
2121

2222
import org.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpPost;
2324
import org.apache.http.client.methods.HttpPut;
2425
import org.apache.http.entity.ByteArrayEntity;
2526
import org.apache.http.entity.ContentType;
2627
import org.elasticsearch.client.watcher.ActivateWatchRequest;
2728
import org.elasticsearch.client.watcher.AckWatchRequest;
29+
import org.elasticsearch.client.watcher.StartWatchServiceRequest;
30+
import org.elasticsearch.client.watcher.StopWatchServiceRequest;
2831
import org.elasticsearch.common.bytes.BytesReference;
2932
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
3033
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
3134

3235
public class WatcherRequestConverters {
3336

37+
static Request startWatchService(StartWatchServiceRequest startWatchServiceRequest) {
38+
String endpoint = new RequestConverters.EndpointBuilder()
39+
.addPathPartAsIs("_xpack")
40+
.addPathPartAsIs("watcher")
41+
.addPathPartAsIs("_start")
42+
.build();
43+
44+
return new Request(HttpPost.METHOD_NAME, endpoint);
45+
}
46+
47+
static Request stopWatchService(StopWatchServiceRequest stopWatchServiceRequest) {
48+
String endpoint = new RequestConverters.EndpointBuilder()
49+
.addPathPartAsIs("_xpack")
50+
.addPathPartAsIs("watcher")
51+
.addPathPartAsIs("_stop")
52+
.build();
53+
54+
return new Request(HttpPost.METHOD_NAME, endpoint);
55+
}
56+
3457
static Request putWatch(PutWatchRequest putWatchRequest) {
3558
String endpoint = new RequestConverters.EndpointBuilder()
3659
.addPathPartAsIs("_xpack")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.watcher;
21+
22+
import org.elasticsearch.client.Validatable;
23+
24+
public class StartWatchServiceRequest implements Validatable {
25+
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.watcher;
21+
22+
import org.elasticsearch.client.Validatable;
23+
24+
public class StopWatchServiceRequest implements Validatable {
25+
26+
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
package org.elasticsearch.client;
2020

2121
import org.elasticsearch.ElasticsearchStatusException;
22-
import org.elasticsearch.client.watcher.ActivateWatchRequest;
23-
import org.elasticsearch.client.watcher.ActivateWatchResponse;
22+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2423
import org.elasticsearch.client.watcher.AckWatchRequest;
2524
import org.elasticsearch.client.watcher.AckWatchResponse;
2625
import org.elasticsearch.client.watcher.ActionStatus;
2726
import org.elasticsearch.client.watcher.ActionStatus.AckStatus;
27+
import org.elasticsearch.client.watcher.ActivateWatchRequest;
28+
import org.elasticsearch.client.watcher.ActivateWatchResponse;
29+
import org.elasticsearch.client.watcher.StartWatchServiceRequest;
30+
import org.elasticsearch.client.watcher.StopWatchServiceRequest;
2831
import org.elasticsearch.common.bytes.BytesArray;
2932
import org.elasticsearch.common.bytes.BytesReference;
3033
import org.elasticsearch.common.xcontent.XContentType;
@@ -39,6 +42,18 @@
3942

4043
public class WatcherIT extends ESRestHighLevelClientTestCase {
4144

45+
public void testStartWatchService() throws Exception {
46+
AcknowledgedResponse response =
47+
highLevelClient().watcher().startWatchService(new StartWatchServiceRequest(), RequestOptions.DEFAULT);
48+
assertTrue(response.isAcknowledged());
49+
}
50+
51+
public void testStopWatchService() throws Exception {
52+
AcknowledgedResponse response =
53+
highLevelClient().watcher().stopWatchService(new StopWatchServiceRequest(), RequestOptions.DEFAULT);
54+
assertTrue(response.isAcknowledged());
55+
}
56+
4257
public void testPutWatch() throws Exception {
4358
String watchId = randomAlphaOfLength(10);
4459
PutWatchResponse putWatchResponse = createWatch(watchId);

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
package org.elasticsearch.client;
2121

2222
import org.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpPost;
2324
import org.apache.http.client.methods.HttpPut;
2425
import org.elasticsearch.client.watcher.ActivateWatchRequest;
2526
import org.elasticsearch.client.watcher.AckWatchRequest;
27+
import org.elasticsearch.client.watcher.StartWatchServiceRequest;
28+
import org.elasticsearch.client.watcher.StopWatchServiceRequest;
2629
import org.elasticsearch.common.bytes.BytesArray;
2730
import org.elasticsearch.common.xcontent.XContentType;
2831
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
@@ -39,7 +42,19 @@
3942

4043
public class WatcherRequestConvertersTests extends ESTestCase {
4144

42-
public void testXPackPutWatch() throws Exception {
45+
public void testStartWatchService() {
46+
Request request = WatcherRequestConverters.startWatchService(new StartWatchServiceRequest());
47+
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
48+
assertEquals("/_xpack/watcher/_start", request.getEndpoint());
49+
}
50+
51+
public void testStopWatchService() {
52+
Request request = WatcherRequestConverters.stopWatchService(new StopWatchServiceRequest());
53+
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
54+
assertEquals("/_xpack/watcher/_stop", request.getEndpoint());
55+
}
56+
57+
public void testPutWatch() throws Exception {
4358
PutWatchRequest putWatchRequest = new PutWatchRequest();
4459
String watchId = randomAlphaOfLength(10);
4560
putWatchRequest.setId(watchId);

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.elasticsearch.action.ActionListener;
2222
import org.elasticsearch.action.LatchedActionListener;
23+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2324
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
2425
import org.elasticsearch.client.Request;
2526
import org.elasticsearch.client.RequestOptions;
@@ -31,6 +32,8 @@
3132
import org.elasticsearch.client.watcher.AckWatchResponse;
3233
import org.elasticsearch.client.watcher.ActionStatus;
3334
import org.elasticsearch.client.watcher.ActionStatus.AckStatus;
35+
import org.elasticsearch.client.watcher.StartWatchServiceRequest;
36+
import org.elasticsearch.client.watcher.StopWatchServiceRequest;
3437
import org.elasticsearch.client.watcher.WatchStatus;
3538
import org.elasticsearch.common.bytes.BytesArray;
3639
import org.elasticsearch.common.bytes.BytesReference;
@@ -46,6 +49,92 @@
4649

4750
public class WatcherDocumentationIT extends ESRestHighLevelClientTestCase {
4851

52+
public void testStartStopWatchService() throws Exception {
53+
RestHighLevelClient client = highLevelClient();
54+
55+
{
56+
//tag::start-watch-service-request
57+
StartWatchServiceRequest request = new StartWatchServiceRequest();
58+
//end::start-watch-service-request
59+
60+
//tag::start-watch-service-execute
61+
AcknowledgedResponse response = client.watcher().startWatchService(request, RequestOptions.DEFAULT);
62+
//end::start-watch-service-execute
63+
64+
//tag::start-watch-service-response
65+
boolean isAcknowledged = response.isAcknowledged(); // <1>
66+
//end::start-watch-service-response
67+
}
68+
69+
{
70+
//tag::stop-watch-service-request
71+
StopWatchServiceRequest request = new StopWatchServiceRequest();
72+
//end::stop-watch-service-request
73+
74+
//tag::stop-watch-service-execute
75+
AcknowledgedResponse response = client.watcher().stopWatchService(request, RequestOptions.DEFAULT);
76+
//end::stop-watch-service-execute
77+
78+
//tag::stop-watch-service-response
79+
boolean isAcknowledged = response.isAcknowledged(); // <1>
80+
//end::stop-watch-service-response
81+
}
82+
83+
{
84+
StartWatchServiceRequest request = new StartWatchServiceRequest();
85+
86+
// tag::start-watch-service-execute-listener
87+
ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {
88+
@Override
89+
public void onResponse(AcknowledgedResponse response) {
90+
// <1>
91+
}
92+
93+
@Override
94+
public void onFailure(Exception e) {
95+
// <2>
96+
}
97+
};
98+
// end::start-watch-service-execute-listener
99+
100+
CountDownLatch latch = new CountDownLatch(1);
101+
listener = new LatchedActionListener<>(listener, latch);
102+
103+
// tag::start-watch-service-execute-async
104+
client.watcher().startWatchServiceAsync(request, RequestOptions.DEFAULT, listener); // <1>
105+
// end::start-watch-service-execute-async
106+
107+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
108+
}
109+
110+
{
111+
StopWatchServiceRequest request = new StopWatchServiceRequest();
112+
113+
// tag::stop-watch-service-execute-listener
114+
ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {
115+
@Override
116+
public void onResponse(AcknowledgedResponse response) {
117+
// <1>
118+
}
119+
120+
@Override
121+
public void onFailure(Exception e) {
122+
// <2>
123+
}
124+
};
125+
// end::stop-watch-service-execute-listener
126+
127+
CountDownLatch latch = new CountDownLatch(1);
128+
listener = new LatchedActionListener<>(listener, latch);
129+
130+
// tag::stop-watch-service-execute-async
131+
client.watcher().stopWatchServiceAsync(request, RequestOptions.DEFAULT, listener); // <1>
132+
// end::stop-watch-service-execute-async
133+
134+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
135+
}
136+
}
137+
49138
public void testWatcher() throws Exception {
50139
RestHighLevelClient client = highLevelClient();
51140

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,15 @@ include::security/delete-role-mapping.asciidoc[]
328328

329329
The Java High Level REST Client supports the following Watcher APIs:
330330

331+
* <<java-rest-high-watcher-start-watch-service>>
332+
* <<java-rest-high-watcher-stop-watch-service>>
331333
* <<java-rest-high-x-pack-watcher-put-watch>>
332334
* <<java-rest-high-x-pack-watcher-delete-watch>>
333335
* <<{upid}-ack-watch>>
334336
* <<{upid}-activate-watch>>
335337

338+
include::watcher/start-watch-service.asciidoc[]
339+
include::watcher/stop-watch-service.asciidoc[]
336340
include::watcher/put-watch.asciidoc[]
337341
include::watcher/delete-watch.asciidoc[]
338342
include::watcher/ack-watch.asciidoc[]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--
2+
:api: start-watch-service
3+
:request: StartWatchServiceRequest
4+
:response: StartWatchServiceResponse
5+
--
6+
[id="{upid}-{api}"]
7+
=== Start Watch Service API
8+
9+
[id="{upid}-{api}-request"]
10+
==== Execution
11+
12+
{xpack-ref}/watcher-api-start.html[Start watcher] enables you
13+
to manually start the watch service. Submit the following request
14+
to start the watch service:
15+
16+
["source","java",subs="attributes,callouts,macros"]
17+
--------------------------------------------------
18+
include-tagged::{doc-tests-file}[{api}-request]
19+
--------------------------------------------------
20+
21+
[id="{upid}-{api}-response"]
22+
==== Response
23+
24+
The returned `AcknowledgeResponse` contains a value on whether or not the request
25+
was received:
26+
27+
["source","java",subs="attributes,callouts,macros"]
28+
--------------------------------------------------
29+
include-tagged::{doc-tests-file}[{api}-response]
30+
--------------------------------------------------
31+
<1> A boolean value of `true` if successfully received, `false` otherwise.
32+
33+
include::../execution.asciidoc[]

0 commit comments

Comments
 (0)