有没有一种好的方法来检查Datastax Session.executeAsync()是否引发了异常? [英] Is there a good way to check whether a Datastax Session.executeAsync() has thrown an exception?

查看:107
本文介绍了有没有一种好的方法来检查Datastax Session.executeAsync()是否引发了异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图通过调用 session.executeAsync()而不是 session.execute()用于数据库写入。

I'm trying to speed up our code by calling session.executeAsync() instead of session.execute() for DB writes.

我们有一些使用情况,其中数据库连接可能已断开,当前是以前的 execute()会引发异常。我们可以捕获这些异常,然后在其他地方重试或保存数据,等等...

We have use cases where the DB connection might be down, currently the previous execute() throws an exception when the connection is lost (no hosts reachable in the cluster). We can catch these exceptions and retry or save the data somewhere else etc...

使用 executeAsync(),看起来没有任何方法可以满足该用例-需要访问返回的 ResultSetFuture 对象以检查结果,这将使使用 executeAsync()首先...

With executeAsync(), it doesn't look like there's any way to fulfill this use case - the returned ResultSetFuture object needs to be accessed to check the result, which would defeat the purpose of using the executeAsync() in the first place...

是否可以添加侦听器(或类似方法)

Is there any way to add a listener (or something similar) anywhere for the executeAsync() call that will asynchronously notify some other code that a DB write has failed?

是否与此相关?
Datastax 1.0.2
Java 1.7.40

Is this pertinent? Datastax 1.0.2 Java 1.7.40

推荐答案

ResultSetFuture 从Guava库中实现 ListenableFuture

You could try something like this since the ResultSetFuture implements ListenableFuture from the Guava library:

    ResultSetFuture resultSetFuture = session.executeAsync("SELECT * FROM test.t;");
    Futures.addCallback(resultSetFuture, new FutureCallback<ResultSet>() {
        @Override
        public void onSuccess(@Nullable com.datastax.driver.core.ResultSet resultSet) {
            // do nothing
        }

        @Override
        public void onFailure(Throwable throwable) {
            System.out.printf("Failed with: %s\n", throwable);
        }
    });

这种方法不会阻止您的应用程序。

This approach will not block your application.

这篇关于有没有一种好的方法来检查Datastax Session.executeAsync()是否引发了异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆