1

I am trying to use Apache Ignite with Couchbase at the backend as persistence layer. I am doing this as a data grid so that any changes made to ignite in-memory cache gets written to couchbase eventually. I am implementing this with spring-boot and spring-data. The igniteConfiguration bean looks like this

@Bean(name = "igniteConfiguration")
    public IgniteConfiguration igniteConfiguration() {
        IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
        CacheConfiguration cache = new CacheConfiguration("sample");
        cache.setAtomicityMode(CacheAtomicityMode.ATOMIC);
        //The below line  where I am confused
        cache.setCacheStoreFactory(FactoryBuilder.factoryOf(CacheStoreImplementationWithCouchbaseRepositoryBean.class);
        cache.setCacheStoreSessionListenerFactories()
        cache.setReadThrough(true);
        cache.setWriteThrough(true);
        
        igniteConfiguration.setCacheConfiguration(cache);

        return igniteConfiguration;
    }

I need to provide one implementation of cacheStore interface of Ignite in order to connect couchbase as backend data store. I configured Couchbase Repository class and created a bean of it in the CacheStoreImplementationWithCouchbaseRepositoryBean.java. But this repository bean is not getting initiated because CacheStoreImplementation class is not in spring context and getting null always.

As I used spring-data. Now I have

  1. Spring data repository for Ignite
  2. Spring data repository for couchbase
  3. One implementation of cacheStore interface of ignite

But not sure how to send the couchbase repository bean to the cacheStore implementation in a way so that it uses this repository class to execute crud operation in couchbase internally.

dariosicily
  • 4,239
  • 2
  • 11
  • 17
bivrantoshakil
  • 421
  • 1
  • 5
  • 10

4 Answers4

1

bivrantoshakil -

"I need to provide one implementation of cacheStore interface of Ignite in order to connect couchbase as backend data store. I configured Couchbase Repository class and created a bean of it in the CacheStoreImplementationWithCouchbaseRepositoryBean.java"

What documentation/tutorial are you following? Please zip up and post your project and I will investigate.

I have a few tangential thoughts

  1. you may which to just use Couchbase without Ignite, as the key-value api is really fast, with persistence being asynchronous and configurable.

  2. There is a tutorial of spring data caching with couchbase at https://blog.couchbase.com/couchbase-spring-cache/ You can skip to "Adding Spring Boot Caching Capabilites".

  3. There is tutorial Apache Ignite here - https://www.baeldung.com/apache-ignite-spring-data

Michael Reiche
  • 375
  • 1
  • 7
1

Take a look here to see how store factories are configured: https://github.com/apache/ignite/tree/master/examples/src/main/java/org/apache/ignite/examples/datagrid/store

I recommend decoupling your project from CouchBase and/or spring-data to make it simpler, and then adding the appropriate components, one by one, to see where the failure is.

If you need one bean to be initialized before another bean consider using the appropriate annotations.

Alex K
  • 841
  • 4
  • 5
0

I found one work around for the issue I was having above. First I created one class to get the Spring Application Context

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextProvider implements ApplicationContextAware {

private static ApplicationContext context;

/**
 * Returns the Spring managed bean instance of the given class type if it
 * exists. Returns null otherwise.
 * 
 * @param beanClass
 * @return
 */
public static <T extends Object> T getBean(Class<T> beanClass) {
    return context.getBean(beanClass);
}

@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {

    // store ApplicationContext reference to access required beans later on
    ApplicationContextProvider.context = context;
}
}

and then in the CacheStoreImplementationWithCouchbaseRepositoryBean.class I did this -

//Repository bean
private CouchbaseRepository repository;
@Override
public Employee load(Long key) {
    repository = ApplicationContextProvider.getBean(CouchbaseRepository.class);
    return repository.findById(key).orElse(null);
}
bivrantoshakil
  • 421
  • 1
  • 5
  • 10
0

There are examples of using couchbase cache in

git clone git@github.com:spring-projects/spring-data-couchbase.git
cd spring-data-couchbase
./java/org/springframework/data/couchbase/cache/CouchbaseCacheCollectionIntegrationTests.java
./java/org/springframework/data/couchbase/cache/CouchbaseCacheIntegrationTests.java
Michael Reiche
  • 375
  • 1
  • 7