|
| 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 | +package org.elasticsearch.client.documentation; |
| 20 | + |
| 21 | +import org.elasticsearch.action.ActionListener; |
| 22 | +import org.elasticsearch.action.LatchedActionListener; |
| 23 | +import org.elasticsearch.client.ESRestHighLevelClientTestCase; |
| 24 | +import org.elasticsearch.client.RequestOptions; |
| 25 | +import org.elasticsearch.client.RestHighLevelClient; |
| 26 | +import org.elasticsearch.common.bytes.BytesArray; |
| 27 | +import org.elasticsearch.common.bytes.BytesReference; |
| 28 | +import org.elasticsearch.common.xcontent.XContentType; |
| 29 | +import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; |
| 30 | +import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; |
| 31 | + |
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +public class WatcherDocumentationIT extends ESRestHighLevelClientTestCase { |
| 36 | + |
| 37 | + public void testPutWatch() throws Exception { |
| 38 | + RestHighLevelClient client = highLevelClient(); |
| 39 | + |
| 40 | + { |
| 41 | + //tag::x-pack-put-watch-execute |
| 42 | + // you can also use the WatchSourceBuilder from org.elasticsearch.plugin:x-pack-core to create a watch programmatically |
| 43 | + BytesReference watch = new BytesArray("{ \n" + |
| 44 | + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + |
| 45 | + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + |
| 46 | + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + |
| 47 | + "}"); |
| 48 | + PutWatchRequest request = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); |
| 49 | + request.setActive(false); // <1> |
| 50 | + PutWatchResponse response = client.xpack().watcher().putWatch(request, RequestOptions.DEFAULT); |
| 51 | + //end::x-pack-put-watch-execute |
| 52 | + |
| 53 | + //tag::x-pack-put-watch-response |
| 54 | + String watchId = response.getId(); // <1> |
| 55 | + boolean isCreated = response.isCreated(); // <2> |
| 56 | + long version = response.getVersion(); // <3> |
| 57 | + //end::x-pack-put-watch-response |
| 58 | + } |
| 59 | + |
| 60 | + { |
| 61 | + BytesReference watch = new BytesArray("{ \n" + |
| 62 | + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + |
| 63 | + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + |
| 64 | + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + |
| 65 | + "}"); |
| 66 | + PutWatchRequest request = new PutWatchRequest("my_other_watch_id", watch, XContentType.JSON); |
| 67 | + // tag::x-pack-put-watch-execute-listener |
| 68 | + ActionListener<PutWatchResponse> listener = new ActionListener<PutWatchResponse>() { |
| 69 | + @Override |
| 70 | + public void onResponse(PutWatchResponse response) { |
| 71 | + // <1> |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onFailure(Exception e) { |
| 76 | + // <2> |
| 77 | + } |
| 78 | + }; |
| 79 | + // end::x-pack-put-watch-execute-listener |
| 80 | + |
| 81 | + // Replace the empty listener by a blocking listener in test |
| 82 | + final CountDownLatch latch = new CountDownLatch(1); |
| 83 | + listener = new LatchedActionListener<>(listener, latch); |
| 84 | + |
| 85 | + // tag::x-pack-put-watch-execute-async |
| 86 | + client.xpack().watcher().putWatchAsync(request, RequestOptions.DEFAULT, listener); // <1> |
| 87 | + // end::x-pack-put-watch-execute-async |
| 88 | + |
| 89 | + assertTrue(latch.await(30L, TimeUnit.SECONDS)); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments