Search code examples
javacassandradatastaxfuturetask

Modify collection with ResultSetFuture


I tried implementing async query using java-driver-async-queries. I am modifying a List within the FutureCallback but seems its not working -

List<Product> products = new ArrayList<Product>();

for (// iterating over a Map) {
    key = entry.getKey();
    String query = "SELECT id,desc,category FROM products where id=?";
    ResultSetFuture future = session.executeAsync(query, key);
    Futures.addCallback(future,
        new FutureCallback<ResultSet>() {
            @Override public void onSuccess(ResultSet result) {
                Row row = result.one();
                if (row != null) {
                    Product product = new Product();
                    product.setId(row.getString("id"));
                    product.setDesc(row.getString("desc"));
                    product.setCategory(row.getString("category"));

                    products.add(product);
                }
            }

            @Override public void onFailure(Throwable t) {
                // log error
            }
        },
        MoreExecutors.sameThreadExecutor()
    );
}

System.out.println("Product List : " + products); // Not printing correct values. Sometimes print blank

Is there any other way?

Based on Mikhail Baksheev answer I implemented and now getting proper result. Just a twist. There is some extra logic i need to implement. I am wondering if I can use List<MyClass> instead of List<ResultSetFuture> and MyClass as -

public class MyClass {

    private Integer         productCount;
    private Integer         stockCount;
    private ResultSetFuture result;
}

Then while iterating set FutureList as -

ResultSetFuture result = session.executeAsync(query, key.get());
MyClass allResult = new MyClass();
allResult.setInCount(inCount);
allResult.setResult(result);
allResult.setSohCount(values.size() - inCount);

futuresList.add(allResult);

Solution

  • As @RussS mentioned, the code is not waiting all futures are completed.

    There are many ways to synchronize async code. For example, using CountDownLatch:

    EDIT: Also please use separte thread for callbacks and use concurrent collection for products.

    ConcurrentLinkedQueue<Product> products = new ConcurrentLinkedQueue<Product>();
    final Executor callbackExecutor = Executors.newSingleThreadExecutor();
    final CountDownLatch doneSignal = new CountDownLatch(/*the Map size*/);
    for (// iterating over a Map) {
        key = entry.getKey();
        String query = "SELECT id,desc,category FROM products where id=?";
        ResultSetFuture future = session.executeAsync(query, key);
        Futures.addCallback(future,
            new FutureCallback<ResultSet>() {
                @Override public void onSuccess(ResultSet result) {
                    Row row = result.one();
                    if (row != null) {
                        Product product = new Product();
                        product.setId(row.getString("id"));
                        product.setDesc(row.getString("desc"));
                        product.setCategory(row.getString("category"));
    
                        products.add(product);
                    }
                    doneSignal.countDown();
    
                }
    
                @Override public void onFailure(Throwable t) {
                    // log error
                    doneSignal.countDown();
                }
            },
            callbackExecutor
        );
    }
    
    doneSignal.await();           // wait for all async requests to finish
    System.out.println("Product List : " + products); 
    

    Another way is to collect all futures in a list and wait all results as a single future with guava's Futures.allAsList, e.g:

    List<ResultSetFuture> futuresList = new ArrayList<>( /*Map size*/);
            for (/* iterating over a Map*/) {
                key = entry.getKey();
                String query = "SELECT id,desc,category FROM products where id=?";
                futuresList.add( session.executeAsync( query, key ) );
            }
    
            ListenableFuture<List<ResultSet>> allFuturesResult = Futures.allAsList( futuresList );
            List<Product> products = new ArrayList<>();
            try {
                final List<ResultSet> resultSets = allFuturesResult.get();
                for ( ResultSet rs : resultSets ) {
                    if ( null != rs ) {
                        Row row = rs.one();
                        if (row != null) {
                            Product product = new Product();
                            product.setId(row.getString("id"));
                            product.setDesc(row.getString("desc"));
                            product.setCategory(row.getString("category"));
    
                            products.add(product);
                        }
                    }
                }
            } catch ( InterruptedException | ExecutionException e ) {
                System.out.println(e);
            }
            System.out.println("Product List : " + products);
    

    EDIT 2

    I am wondering if I can use List instead of List and MyClass as

    Technically yes, but you can't pass List<MyClass> in Futures.allAsList in this case or MyClass should implement ListenableFuture interface