我想在我的应用程序中使用dropwizard指标进行异步回调。我正在cassandra asychrounously中插入记录。
我有一个正在执行的查询(绑定状态)列表。我已经添加了一个回调来跟踪onSuccess()和onFailure()到我的异步执行。 (信号量也只是为了限制异步插入查询执行的数量)
现在,我想正确实施指标,以便我可以衡量或监控以下内容:
以下是我需要包含指标的代码段。请帮助。
final Semaphore queryPermits= new Semaphore(100);
public List<ListenableFuture<ResultSet>> sendAsyncQueries(List<BoundStatement> boundStatements) throws CqlException, AdapterException {
List<ListenableFuture<ResultSet>> futures = new ArrayList<>();
final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
for (BoundStatement boundStatement : boundStatements) {
try {
queryPermits.acquire();//Sempahore to throttle cassandra insert
} catch (InterruptedException e) {
LOG.error("InterruptedException while acquire permit for asynchronous insertion ",e);
}
try {
final ListenableFuture<ResultSet> resultSetFuture = executeAsynchronously(boundStatement, ConsistencyLevel.QUORUM);
Futures.addCallback(resultSetFuture,
new FutureCallback<ResultSet>() {
@Override
public void onSuccess(ResultSet result) {
LOG.debug("Inserted cassandra row ");
queryPermits.release();
}
@Override
public void onFailure(Throwable t) {
LOG.error("Error while inserting inCassandra ");
queryPermits.release();
}
},
executor
);
futures.add(resultSetFuture);
}catch(AdapterException e){
queryPermits.release();
throw e;
}
}
return futures;
}