|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | +package com.influxdb.v3; |
| 23 | + |
| 24 | +import java.time.Duration; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +import com.influxdb.v3.client.InfluxDBClient; |
| 29 | +import com.influxdb.v3.client.PointValues; |
| 30 | +import com.influxdb.v3.client.config.ClientConfig; |
| 31 | + |
| 32 | +/** |
| 33 | + * This example shows how to set universal timeouts for writes and queries. |
| 34 | + * <p> |
| 35 | + * The example depends on the "influxdb3-java" module and this module should be built first |
| 36 | + * by running "mvn install" in the root directory. |
| 37 | + */ |
| 38 | +public final class TimeoutsExample { |
| 39 | + |
| 40 | + public static String resolveProperty(final String property, final String fallback) { |
| 41 | + return System.getProperty(property, System.getenv(property)) == null |
| 42 | + ? fallback : System.getProperty(property, System.getenv(property)); |
| 43 | + } |
| 44 | + |
| 45 | + private TimeoutsExample() { } |
| 46 | + |
| 47 | + public static void main(final String[] args) { |
| 48 | + // timeout to use for writes. Experiment with lower values to see timeout exceptions. |
| 49 | + Duration writeTimeout = Duration.ofMillis(5000L); |
| 50 | + // timeout to use for queries. Experiment with lower values to see timeout exceptions. |
| 51 | + Duration queryTimeout = Duration.ofMillis(5000L); |
| 52 | + |
| 53 | + String host = resolveProperty("INFLUX_HOST", "http://localhost:8181"); |
| 54 | + String token = resolveProperty("INFLUX_TOKEN", "my-token"); |
| 55 | + String database = resolveProperty("INFLUX_DATABASE", "my-database"); |
| 56 | + |
| 57 | + String measurement = "timeout_example"; |
| 58 | + |
| 59 | + ClientConfig config = new ClientConfig.Builder() |
| 60 | + .host(host) |
| 61 | + .token(token.toCharArray()) |
| 62 | + .database(database) |
| 63 | + .writeTimeout(writeTimeout) // set timeout to be used with the Write API |
| 64 | + .queryTimeout(queryTimeout) // set timeout to be used with the Query API |
| 65 | + .build(); |
| 66 | + |
| 67 | + try (InfluxDBClient client = InfluxDBClient.getInstance(config)) { |
| 68 | + client.writeRecord(String.format("%s,id=0001 temp=30.14,ticks=42i", measurement)); |
| 69 | + |
| 70 | + TimeUnit.SECONDS.sleep(1); |
| 71 | + String sql = String.format("SELECT * FROM %s ORDER BY time DESC", measurement); |
| 72 | + try (Stream<PointValues> values = client.queryPoints(sql)) { |
| 73 | + values.forEach(pv -> { |
| 74 | + String sv = measurement + "," |
| 75 | + + " id: " + pv.getTag("id") + "," |
| 76 | + + " fVal: " + pv.getFloatField("temp") + "," |
| 77 | + + " iVal: " + pv.getIntegerField("ticks") + "," |
| 78 | + + " " + pv.getTimestamp(); |
| 79 | + System.out.println(sv); |
| 80 | + }); |
| 81 | + } |
| 82 | + } catch (Exception e) { |
| 83 | + throw new RuntimeException(e); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments