这些天,我在使用Cassandra和反射机制异步执行查询时遇到了操作超时异常(由于某些原因,这里不允许直接涉及Cassandra驱动程序核心jar文件) 而且我只想执行查询以删除或创建键空间,就我而言,返回结果集不是必需的。
/**
* Executes the provided query asynchronously.
*
* @param query The query statement
* @param timeout The timeout of query
* @param timeunit The unit of timeout
*/
public void executeAsync(String query, long timeout, TimeUnit timeunit) {
Object resultSetFuture;
if (session == null) {
throw new RuntimeException(String.format("No configured session."));
}
try {
Method executeAsync = session.getClass().getMethod("executeAsync", new Class[] {String.class});
resultSetFuture = executeAsync.invoke(session, query);
Method getUninterruptibly = resultSetFuture.getClass().getMethod("getUninterruptibly", new Class[] {long.class, TimeUnit.class});
getUninterruptibly.setAccessible(true);
getUninterruptibly.invoke(resultSetFuture, timeout, timeunit);
} catch (Exception e) {
throw Err.wrap(e);
}
}
调用者非常简单。
cqlUtil.executeAsync(String.format("DROP KEYSPACE IF EXISTS %s;", keyspaceName), args.getTimeout());
环境:带Cassandra驱动程序核心3.0.1的Cassandra 3.11.2阴影