Unable to see the 'get' method on the AsyncBucket - java-client-2.5.2

Hi all,
Using the java-client-2.5.2, I’m trying to do something which should be relatively simple but for some reason I am stuck. I’m creating a standard connection with the CouchbaseAsyncCluster class, getting an AsyncCluster from it, opening an AsyncBucket from that, and then trying to perform a “get” on that bucket. The problem I’m having is getting the “get” method to actually show up as an available method from the AsyncBucket.

To get started overall, I followed the general code example on the documentation for the CouchbaseAsyncCluster class and made the required modifications for the actual output of the AsyncCluster openBucket method call.

My Java class looks like this:

package com.sample;

import com.couchbase.client.java.AsyncBucket;
import com.couchbase.client.java.AsyncCluster;
import com.couchbase.client.java.CouchbaseAsyncCluster;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import rx.Observable;

public class CouchbaseUtil {
private static CouchbaseUtil instance = null;
private AsyncCluster asyncCluster;
private CouchbaseEnvironment couchbaseEnvironment;
private Observable asyncBucket;

private CouchbaseUtil() {
    try {
        this.couchbaseEnvironment = DefaultCouchbaseEnvironment.builder().kvTimeout(3000).build();
        this.asyncCluster = CouchbaseAsyncCluster.fromConnectionString(this.couchbaseEnvironment, "removed for this post");
        this.asyncBucket = this.asyncCluster.openBucket("generic", "generic");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static CouchbaseUtil getInstance() {
    if (instance == null) {
        instance = new CouchbaseUtil();
    }

    return instance;
}

public Observable<AsyncBucket> getAsyncBucket() {
    return this.asyncBucket;
}

public void disconnect() {
    this.asyncCluster.disconnect();
    this.couchbaseEnvironment.shutdownAsync().toBlocking().single();
}

}

The line after the "this.asyncBucket = … " in the private constructor is where I’ve tried to do “this.asyncBucket.get(“documentId”)”, but no dice. The AsyncCluster.openBucket method signatures all return Observable<AsyncBucket>, which is fine but I am unsure how to unwrap or access the AsyncBucket inside of the Observable to be able to access its “get” (and other) methods. I’ve done searches on the internet to look for examples but have been miraculously unable to find any.

Am I making this too difficult on myself in some way? Is there a more preferred approach to what I’m doing?

Thanks all.

Hi Michael,

That’s because your asyncBucket field is actually an RxJava Observable that produces an AsyncBucket when you subscribe to it. In this case there’s probably not much value in asynchronously opening the bucket, so I’d recommend changing this:

private Observable asyncBucket;

to this:

private AsyncBucket asyncBucket;

And changing this:

this.asyncBucket = this.asyncCluster.openBucket("generic", "generic");

to this:

this.asyncBucket = this.asyncCluster.openBucket("generic", "generic")
        .toBlocking().single();

That said, unless you’re already comfortable with RxJava, it’s probably better to start by using the blocking CouchbaseCluster instead of CouchbaseAsyncCluster. The Buckets returned by the blocking cluster can be converted to AsyncBuckets by calling bucket.async() if you want to do certain operations in async mode. That way you can use the easy, simple blocking API in most places, and only dip into async mode when it offers considerable benefits over the blocking mode.

By the way, that getInstance() method is not thread-safe. Here are some thread-safe singleton initialization strategies.

Cheers,
David