Search code examples
javaspring-bootspring-datacouchbaseignite

Apache Ignite with Couchbase as Persistence


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.


Solution

  • 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);
    }