异常处理
更新时间:
TSDB-Client SDK 不强制处理异常,所有异常均以 RuntimeException 的形式抛出。用户可以根据需要捕捉,处理异常。
常见异常
| 异常类 | 描述 |
|---|---|
| HttpClientException | Client 异常 |
| HttpClientConnectionRefusedException | TSDB 服务器拒绝连接 |
| HttpClientInitException | Client 初始化异常 |
| HttpClientSocketTimeoutException | 请求超时 |
| HttpServerErrorException | TSDB 服务端返回 5xx 错误码 |
| HttpServerNotSupportException | TSDB 服务端返回 4xx 错误码 |
| HttpUnknowStatusException | TSDB 服务端的响应码未知 |
| BufferQueueFullException | 异步提交缓冲队列已满 |
异常处理
同步接口与异常处理
try {tsdb.ttl(10000);} catch(HttpServerNotSupportException e) {int status = e.getStatus();String message = e.getMessage();System.err.println(status + "," + message);} catch(HttpServerErrorException e) {int status = e.getStatus();String message = e.getMessage();System.err.println(status + "," + message);}
异步接口与异常处理
在需要处理异常的时候,重写 failed 方法。
QueryCallback cb = new QueryCallback() {@Overridepublic void response(Query input, List<QueryResult> result) {System.out.println("查询参数:" + input);System.out.println("返回结果:" + result);}// 在需要处理异常的时候,重写 failed 方法@Overridepublic void failed(Query request, Exception ex) {super.failed(request, ex);}};
BatchPutCallback cb = new BatchPutCallback() {@Overridepublic void response(List<Point> input, Result output) {long afterNum = num.addAndGet(input.size());System.out.println("成功处理" + input.size() + ",已处理" + afterNum);}@Overridepublic void failed(List<Point> input, Exception ex) {ex.printStackTrace();long afterNum = num.addAndGet(input.size());System.out.println("失败处理" + input.size() + ",已处理" + afterNum);}};
该文章对您有帮助吗?