-
Notifications
You must be signed in to change notification settings - Fork 129
/
WriteHttpExceptionHandled.java
128 lines (110 loc) · 5.48 KB
/
WriteHttpExceptionHandled.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package example;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApi;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.events.WriteErrorEvent;
import com.influxdb.exceptions.InfluxException;
import javax.annotation.Nonnull;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.logging.Logger;
public class WriteHttpExceptionHandled {
static Logger Log = Logger.getLogger(WriteHttpExceptionHandled.class.getName());
public static String resolveProperty(final String property, final String fallback) {
return System.getProperty(property, System.getenv(property)) == null
? fallback : System.getProperty(property, System.getenv(property));
}
private static final String influxUrl = resolveProperty("INFLUX_URL", "http://localhost:8086");
private static final char[] token = resolveProperty("INFLUX_TOKEN","my-token").toCharArray();
private static final String org = resolveProperty("INFLUX_ORG","my-org");
private static final String bucket = resolveProperty("INFLUX_DATABASE","my-bucket");
public static void main(String[] args) {
InfluxDBClient influxDBClient = InfluxDBClientFactory.create(influxUrl, token, org, bucket);
WriteApiBlocking writeApiBlocking = influxDBClient.getWriteApiBlocking();
WriteApi writeApi = influxDBClient.makeWriteApi();
// InfluxExceptions in Rx streams can be handled in an EventListener
writeApi.listenEvents(WriteErrorEvent.class, (error) -> {
if (error.getThrowable() instanceof InfluxException ie) {
Log.warning("\n*** Custom event handler\n******\n"
+ influxExceptionString(ie)
+ "******\n");
}
});
// the following call will cause an HTTP 400 error
writeApi.writeRecords(WritePrecision.MS, List.of("invalid", "clumsy", "broken", "unusable"));
writeApi.close();
Log.info("\nWriting invalid records to InfluxDB blocking - can handle caught InfluxException.\n");
try {
writeApiBlocking.writeRecord(WritePrecision.MS, "asdf");
} catch (InfluxException e) {
Log.info(influxExceptionString(e));
}
// Note when writing batches with one bad record:
// Cloud v3.x - The bad record is ignored.
// OSS v2.x - returns exception
Log.info("Writing Batch with 1 bad record.");
Instant now = Instant.now();
List<String> lpData = List.of(
String.format("temperature,location=north value=60.0 %d", now.toEpochMilli()),
String.format("temperature,location=south value=65.0 %d", now.minus(1, ChronoUnit.SECONDS).toEpochMilli()),
String.format("temperature,location=north value=59.8 %d", now.minus(2, ChronoUnit.SECONDS).toEpochMilli()),
String.format("temperature,location=south value=64.8 %d", now.minus(3, ChronoUnit.SECONDS).toEpochMilli()),
String.format("temperature,location=north value=59.7 %d", now.minus(4, ChronoUnit.SECONDS).toEpochMilli()),
"asdf",
String.format("temperature,location=north value=59.9 %d", now.minus(6, ChronoUnit.SECONDS).toEpochMilli()),
String.format("temperature,location=south value=64.9 %d", now.minus(7, ChronoUnit.SECONDS).toEpochMilli()),
String.format("temperature,location=north value=60.1 %d", now.minus(8, ChronoUnit.SECONDS).toEpochMilli()),
String.format("temperature,location=south value=65.1 %d", now.minus(9, ChronoUnit.SECONDS).toEpochMilli())
);
try {
writeApiBlocking.writeRecords(WritePrecision.MS, lpData);
} catch (InfluxException e) {
Log.info(influxExceptionString(e));
}
try {
writeApi.writeRecords(WritePrecision.MS, lpData);
} catch (Exception exception) {
if (exception instanceof InfluxException) {
Log.info(influxExceptionString((InfluxException) exception));
}
}
Log.info("Done");
}
private static String influxExceptionString(@Nonnull InfluxException e) {
StringBuilder sBuilder = new StringBuilder().append("Handling InfluxException:\n");
sBuilder.append(" ").append(e.getMessage());
String headers = e.headers()
.keySet()
.stream()
.reduce("\n", (set, key) -> set.concat(
String.format(" %s: %s\n", key, e.headers().get(key)))
);
sBuilder.append("\n HTTP Response Headers:");
sBuilder.append(headers);
return sBuilder.toString();
}
}