文档

异常处理

更新时间:
一键部署

TSDB-Client SDK 不强制处理异常,所有异常均以 RuntimeException 的形式抛出。用户可以根据需要捕捉,处理异常。

常见异常

异常类描述
HttpClientExceptionClient 异常
HttpClientConnectionRefusedExceptionTSDB 服务器拒绝连接
HttpClientInitExceptionClient 初始化异常
HttpClientSocketTimeoutException请求超时
HttpServerErrorExceptionTSDB 服务端返回 5xx 错误码
HttpServerNotSupportExceptionTSDB 服务端返回 4xx 错误码
HttpUnknowStatusExceptionTSDB 服务端的响应码未知
BufferQueueFullException异步提交缓冲队列已满

异常处理

同步接口与异常处理

  1. try {
  2. tsdb.ttl(10000);
  3. } catch(HttpServerNotSupportException e) {
  4. int status = e.getStatus();
  5. String message = e.getMessage();
  6. System.err.println(status + "," + message);
  7. } catch(HttpServerErrorException e) {
  8. int status = e.getStatus();
  9. String message = e.getMessage();
  10. System.err.println(status + "," + message);
  11. }

异步接口与异常处理

在需要处理异常的时候,重写 failed 方法。

  1. QueryCallback cb = new QueryCallback() {
  2. @Override
  3. public void response(Query input, List<QueryResult> result) {
  4. System.out.println("查询参数:" + input);
  5. System.out.println("返回结果:" + result);
  6. }
  7. // 在需要处理异常的时候,重写 failed 方法
  8. @Override
  9. public void failed(Query request, Exception ex) {
  10. super.failed(request, ex);
  11. }
  12. };
  1. BatchPutCallback cb = new BatchPutCallback() {
  2. @Override
  3. public void response(List<Point> input, Result output) {
  4. long afterNum = num.addAndGet(input.size());
  5. System.out.println("成功处理" + input.size() + ",已处理" + afterNum);
  6. }
  7. @Override
  8. public void failed(List<Point> input, Exception ex) {
  9. ex.printStackTrace();
  10. long afterNum = num.addAndGet(input.size());
  11. System.out.println("失败处理" + input.size() + ",已处理" + afterNum);
  12. }
  13. };
  • 本页导读 (1)
文档反馈