I went through many articles over the forum and configured couchbase by extending AbstractCouchbaseConfiguration.
Implementing buckets , template and overriding configureRepositoryOperationsMapping.
I have bucket1 and bucket2.Normal Repository. save and fetch is working as expected.
Entity I am using for second bucket is not saved in bucket2.Instead its getting saved in default bucket.
Change is I am extending ReactiveCrudRepository.And working entirely on reactive environment.
So do I need to change the implementation and extend AbstractReactiveCouchbaseConfiguration instead?
How to I connect bucket2 in reactive way.Is there any reference link for the sample implementation.
Below is my code.
@Repository
public interface SchedulesRepository extends ReactiveCrudRepository<Schedules,String> {
}
//Config class
@Configuration
@EnableReactiveCouchbaseRepositories(basePackages = "****.repository")
public class CouchbaseConfig extends AbstractCouchbaseConfiguration {
@Bean(name="binaryBucket")
public Bucket binaryBucket() throws Exception {
return couchbaseConfigurer().couchbaseCluster().openBucket(env.getProperty("spring.couchbase.bucket.schedule.name"),env.getProperty("spring.couchbase.bucket.schedule.password"));
}
@Bean
public CouchbaseTemplate scheduleTemplate() throws Exception {
CouchbaseTemplate template = new CouchbaseTemplate(
couchbaseClusterInfo(), binaryBucket(),
mappingCouchbaseConverter(), translationService());
template.setDefaultConsistency(getDefaultConsistency());
return template;
}
@Override
public void configureRepositoryOperationsMapping(
RepositoryOperationsMapping baseMapping) {
try {
baseMapping.mapEntity(Schedules.class, scheduleTemplate());
} catch (Exception e) {
}
}
When I am saving using repository
scheduleRepo.save(schedule);//Saved in Bucket1 only
I am not able to figure out what configuration is missing.
Implemented by extending AbstractReactiveCouchbaseConfiguration and below is the code.
Earlier was not passing the second bucket mapping in reactiveCouchbaseTemplate
Went through the constructor and figured out.
public RxJavaCouchbaseTemplate(final ClusterInfo clusterInfo, final Bucket client) {
this(clusterInfo, client, null, null);
}
So every time it was connecting to default bucket.
@Configuration
@EnableReactiveCouchbaseRepositories(basePackages = "******.repository")
public class CouchbaseConfig extends AbstractReactiveCouchbaseConfiguration {
//configuring default bucket here
@Autowired
private Environment env;
@Override
protected List<String> getBootstrapHosts() {
return Collections.singletonList(env.getProperty("spring.couchbase.bootstrap-hosts"));
}
@Override
protected String getBucketName() {
return env.getProperty("spring.couchbase.bucket.name");
}
@Override
protected String getBucketPassword() {
return env.getProperty("spring.couchbase.bucket.password");
}
//bucket other than defaut one
@Bean(name = "binaryBucket")
public Bucket binaryBucket() throws Exception {
return couchbaseConfigurer().couchbaseCluster().openBucket(
env.getProperty("spring.couchbase.bucket.schedule.name"),
env.getProperty("spring.couchbase.bucket.schedule.password"));
}
@Bean(name = BeanNames.RXJAVA1_COUCHBASE_TEMPLATE)
public RxJavaCouchbaseTemplate reactiveCouchbaseTemplate() throws Exception {
RxJavaCouchbaseTemplate template = new RxJavaCouchbaseTemplate(couchbaseConfigurer().couchbaseClusterInfo(),
binaryBucket(), mappingCouchbaseConverter(), translationService());
template.setDefaultConsistency(getDefaultConsistency());
return template;
}
@Bean(name = BeanNames.REACTIVE_COUCHBASE_OPERATIONS_MAPPING)
public ReactiveRepositoryOperationsMapping reactiveCouchbaseRepositoryOperationsMapping(
RxJavaCouchbaseTemplate reactiveCouchbaseTemplate) {
// create a base mapping that associates all repositories to the default
// template
ReactiveRepositoryOperationsMapping baseMapping = new ReactiveRepositoryOperationsMapping(
reactiveCouchbaseTemplate);
// let the user tune it
configureReactiveRepositoryOperationsMapping(baseMapping);
return baseMapping;
}
public void configureRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
baseMapping.mapEntity(Schedules.class, reactiveCouchbaseTemplate());
} catch (Exception e) {
// custom Exception handling
}
}
@Override
protected Consistency getDefaultConsistency() {
return Consistency.READ_YOUR_OWN_WRITES;
}