我有一个使用
influxdb-client-java
版本7.0.0
的项目。我正在使用 WriteApi
编写点,它启动后台线程将数据发送到 influxdb 实例。
我想抓住
UnauthorizedException
并对其采取行动。
我可以在某处注册一个异常处理程序吗?或者我怎样才能实现这一点?
我在网上搜索过,但找不到任何有用的文档。
我找到了实现它的方法。
WriteApi
有一个 listenEvents
方法可供您使用。一个例子:
final var writeApi = client.makeWriteApi(writeOptions);
writeApi.listenEvents(WriteErrorEvent.class, event -> {
if (event.getThrowable() instanceof UnauthorizedException) {
log.error("Could not send data to influx. The provided influx token is either wrong or does not has the write permission granted.");
System.exit(-1);
}
});