|
| 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 example; |
| 23 | + |
| 24 | +import com.influxdb.client.InfluxDBClient; |
| 25 | +import com.influxdb.client.InfluxDBClientFactory; |
| 26 | +import com.influxdb.client.WriteApi; |
| 27 | +import com.influxdb.client.WriteApiBlocking; |
| 28 | +import com.influxdb.client.domain.WritePrecision; |
| 29 | +import com.influxdb.client.write.events.WriteErrorEvent; |
| 30 | +import com.influxdb.exceptions.InfluxException; |
| 31 | + |
| 32 | +import javax.annotation.Nonnull; |
| 33 | +import java.time.Instant; |
| 34 | +import java.time.temporal.ChronoUnit; |
| 35 | +import java.util.List; |
| 36 | +import java.util.logging.Logger; |
| 37 | + |
| 38 | +public class WriteHttpExceptionHandled { |
| 39 | + |
| 40 | + static Logger Log = Logger.getLogger(WriteHttpExceptionHandled.class.getName()); |
| 41 | + |
| 42 | + public static String resolveProperty(final String property, final String fallback) { |
| 43 | + return System.getProperty(property, System.getenv(property)) == null |
| 44 | + ? fallback : System.getProperty(property, System.getenv(property)); |
| 45 | + } |
| 46 | + |
| 47 | + private static final String influxUrl = resolveProperty("INFLUX_URL", "http://localhost:8086"); |
| 48 | + private static final char[] token = resolveProperty("INFLUX_TOKEN","my-token").toCharArray(); |
| 49 | + private static final String org = resolveProperty("INFLUX_ORG","my-org"); |
| 50 | + private static final String bucket = resolveProperty("INFLUX_DATABASE","my-bucket"); |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + |
| 54 | + InfluxDBClient influxDBClient = InfluxDBClientFactory.create(influxUrl, token, org, bucket); |
| 55 | + |
| 56 | + WriteApiBlocking writeApiBlocking = influxDBClient.getWriteApiBlocking(); |
| 57 | + WriteApi writeApi = influxDBClient.makeWriteApi(); |
| 58 | + |
| 59 | + // InfluxExceptions in Rx streams can be handled in an EventListener |
| 60 | + writeApi.listenEvents(WriteErrorEvent.class, (error) -> { |
| 61 | + if (error.getThrowable() instanceof InfluxException ie) { |
| 62 | + Log.warning("\n*** Custom event handler\n******\n" |
| 63 | + + influxExceptionString(ie) |
| 64 | + + "******\n"); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + // the following call will cause an HTTP 400 error |
| 69 | + writeApi.writeRecords(WritePrecision.MS, List.of("invalid", "clumsy", "broken", "unusable")); |
| 70 | + writeApi.close(); |
| 71 | + |
| 72 | + |
| 73 | + Log.info("\nWriting invalid records to InfluxDB blocking - can handle caught InfluxException.\n"); |
| 74 | + try { |
| 75 | + writeApiBlocking.writeRecord(WritePrecision.MS, "asdf"); |
| 76 | + } catch (InfluxException e) { |
| 77 | + Log.info(influxExceptionString(e)); |
| 78 | + } |
| 79 | + |
| 80 | + // Note when writing batches with one bad record: |
| 81 | + // Cloud v3.x - The bad record is ignored. |
| 82 | + // OSS v2.x - returns exception |
| 83 | + Log.info("Writing Batch with 1 bad record."); |
| 84 | + Instant now = Instant.now(); |
| 85 | + |
| 86 | + List<String> lpData = List.of( |
| 87 | + String.format("temperature,location=north value=60.0 %d", now.toEpochMilli()), |
| 88 | + String.format("temperature,location=south value=65.0 %d", now.minus(1, ChronoUnit.SECONDS).toEpochMilli()), |
| 89 | + String.format("temperature,location=north value=59.8 %d", now.minus(2, ChronoUnit.SECONDS).toEpochMilli()), |
| 90 | + String.format("temperature,location=south value=64.8 %d", now.minus(3, ChronoUnit.SECONDS).toEpochMilli()), |
| 91 | + String.format("temperature,location=north value=59.7 %d", now.minus(4, ChronoUnit.SECONDS).toEpochMilli()), |
| 92 | + "asdf", |
| 93 | + String.format("temperature,location=north value=59.9 %d", now.minus(6, ChronoUnit.SECONDS).toEpochMilli()), |
| 94 | + String.format("temperature,location=south value=64.9 %d", now.minus(7, ChronoUnit.SECONDS).toEpochMilli()), |
| 95 | + String.format("temperature,location=north value=60.1 %d", now.minus(8, ChronoUnit.SECONDS).toEpochMilli()), |
| 96 | + String.format("temperature,location=south value=65.1 %d", now.minus(9, ChronoUnit.SECONDS).toEpochMilli()) |
| 97 | + ); |
| 98 | + |
| 99 | + try { |
| 100 | + writeApiBlocking.writeRecords(WritePrecision.MS, lpData); |
| 101 | + } catch (InfluxException e) { |
| 102 | + Log.info(influxExceptionString(e)); |
| 103 | + } |
| 104 | + |
| 105 | + try { |
| 106 | + writeApi.writeRecords(WritePrecision.MS, lpData); |
| 107 | + } catch (Exception exception) { |
| 108 | + if (exception instanceof InfluxException) { |
| 109 | + Log.info(influxExceptionString((InfluxException) exception)); |
| 110 | + } |
| 111 | + } |
| 112 | + Log.info("Done"); |
| 113 | + } |
| 114 | + |
| 115 | + private static String influxExceptionString(@Nonnull InfluxException e) { |
| 116 | + StringBuilder sBuilder = new StringBuilder().append("Handling InfluxException:\n"); |
| 117 | + sBuilder.append(" ").append(e.getMessage()); |
| 118 | + String headers = e.headers() |
| 119 | + .keySet() |
| 120 | + .stream() |
| 121 | + .reduce("\n", (set, key) -> set.concat( |
| 122 | + String.format(" %s: %s\n", key, e.headers().get(key))) |
| 123 | + ); |
| 124 | + sBuilder.append("\n HTTP Response Headers:"); |
| 125 | + sBuilder.append(headers); |
| 126 | + return sBuilder.toString(); |
| 127 | + } |
| 128 | +} |
0 commit comments