Skip to content

Commit

Permalink
chore: add WriteApi EventListener to example.
Browse files Browse the repository at this point in the history
  • Loading branch information
karel-rehor committed Aug 5, 2024
1 parent e2c3060 commit 2529ea2
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions examples/src/main/java/example/WriteHttpExceptionHandled.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand All @@ -52,20 +53,28 @@ public static void main(String[] args) {

InfluxDBClient influxDBClient = InfluxDBClientFactory.create(influxUrl, token, org, bucket);

Log.info("\nWriting invalid records to InfluxDB reactively - check log SEVERE messages.\n");
WriteApiBlocking writeApiBlocking = influxDBClient.getWriteApiBlocking();
WriteApi writeApi = influxDBClient.makeWriteApi();

// the following call will cause an HTTP 400 error, which will
// include selected HTTP response headers in the error log
// 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");
WriteApiBlocking writeApiBlocking = influxDBClient.getWriteApiBlocking();
try {
writeApiBlocking.writeRecord(WritePrecision.MS, "asdf");
} catch (InfluxException e) {
logInfluxException(e);
Log.info(influxExceptionString(e));
}

// Note when writing batches with one bad record:
Expand All @@ -90,13 +99,20 @@ public static void main(String[] args) {
try {
writeApiBlocking.writeRecords(WritePrecision.MS, lpData);
} catch (InfluxException e) {
logInfluxException(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 void logInfluxException(@Nonnull InfluxException e) {
private static String influxExceptionString(@Nonnull InfluxException e) {
StringBuilder sBuilder = new StringBuilder().append("Handling InfluxException:\n");
sBuilder.append(" ").append(e.getMessage());
String headers = e.headers()
Expand All @@ -107,6 +123,6 @@ private static void logInfluxException(@Nonnull InfluxException e) {
);
sBuilder.append("\n HTTP Response Headers:");
sBuilder.append(headers);
Log.info(sBuilder.toString());
return sBuilder.toString();
}
}

0 comments on commit 2529ea2

Please sign in to comment.