use of org.springframework.data.cassandra.core.cql.util.CassandraFutureAdapter in project spring-data-cassandra by spring-projects.
the class AsyncCqlTemplate method query.
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.cql.AsyncCqlOperations#query(org.springframework.data.cassandra.core.cql.AsyncPreparedStatementCreator, org.springframework.data.cassandra.core.cql.PreparedStatementBinder, org.springframework.data.cassandra.core.cql.AsyncResultSetExtractor)
*/
@Override
public <T> ListenableFuture<T> query(AsyncPreparedStatementCreator preparedStatementCreator, @Nullable PreparedStatementBinder psb, AsyncResultSetExtractor<T> resultSetExtractor) throws DataAccessException {
Assert.notNull(preparedStatementCreator, "AsyncPreparedStatementCreator must not be null");
Assert.notNull(resultSetExtractor, "AsyncResultSetExtractor object must not be null");
PersistenceExceptionTranslator exceptionTranslator = ex -> translateExceptionIfPossible("Query", toCql(preparedStatementCreator), ex);
try {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Preparing statement [%s] using %s", toCql(preparedStatementCreator), preparedStatementCreator));
}
CqlSession session = getCurrentSession();
ListenableFuture<Statement<?>> statementFuture = new MappingListenableFutureAdapter<>(preparedStatementCreator.createPreparedStatement(session), preparedStatement -> {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Executing prepared statement [%s]", toCql(preparedStatement)));
}
return applyStatementSettings(psb != null ? psb.bindValues(preparedStatement) : preparedStatement.bind());
});
CompletableFuture<T> result = //
statementFuture.completable().thenCompose(//
session::executeAsync).thenApply(//
resultSetExtractor::extractData).thenCompose(ListenableFuture::completable);
return new CassandraFutureAdapter<>(result, exceptionTranslator);
} catch (DriverException e) {
throw translateException(exceptionTranslator, e);
}
}
Aggregations