0

I would like to configure a distributed cache with Apache Ignite using the JCache API (JSR107, javax.cache). Is this possible?

The examples I have found either create a local cache with the JCache API or create a distributed cache (or datagrid) using the Apache Ignite API.

PeakCode
  • 130
  • 7

2 Answers2

4

JCache allows to provide provider-specific configuration when creating a cache. I.e., you can do this:

// Get or create a cache manager.
CacheManager cacheMgr = Caching.getCachingProvider().getCacheManager();

// This is an Ignite configuration object (org.apache.ignite.configuration.CacheConfiguration).
CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>();

// Specify cache mode and/or any other Ignite-specific configuration properties.
cfg.setCacheMode(CacheMode.PARTITIONED);

// Create a cache based on configuration create above.
Cache<Integer, String> cache = cacheMgr.createCache("a", cfg);

Also note that partitioned mode is actually the default one in Ignite, so you are not required to specify it explicitly.

UPD. In addition, CachingProvider.getCacheManager(..) method accepts a provider-specific URI that in case of Ignite should point to XML configuration file. Discovery, communication and other parameters can be provided there.

Valentin Kulichenko
  • 8,365
  • 1
  • 16
  • 12
  • I'm aware of that you can use Ignite-specific configuration when creating a cache through JCache, but I can't see how you can configure relevant stuff like the grid and the discovery SPI. In short I haven't managed to create a JCache cache that exists in more than one node. – PeakCode Jan 27 '16 at 09:18
  • @ValentinKulichenko is there a way to start this ignite in CLIENT mode ? so that all these web app threads can connect to another ignite node that i run as server elsewhere ? – kommradHomer Oct 22 '18 at 13:41
1

Please note that JCache specification does not specify all the configurations that apply to individual cache providers in terms of configuring via CacheManager for creating a Grid. The requirement for creating a CacheManager is standard but not everything relevant to how the manager itself is configured.

Following code will demonstrate how to create a grid using Apache Ignite in SpringBoot

@Bean
@SuppressWarnings("unchecked")
public org.apache.ignite.cache.spring.SpringCacheManager cacheManager() {
    IgniteConfiguration igniteConfiguration = new IgniteConfiguration();
    igniteConfiguration.setGridName("petclinic-ignite-grid");
    //igniteConfiguration.setClassLoader(dynamicClassLoaderWrapper());

    igniteConfiguration.setCacheConfiguration(this.createDefaultCache("petclinic"),
            this.createDefaultCache("org.hibernate.cache.spi.UpdateTimestampsCache"),
            this.createDefaultCache("org.hibernate.cache.internal.StandardQueryCache"));

    SpringCacheManager springCacheManager = new SpringCacheManager();
    springCacheManager.setConfiguration(igniteConfiguration);
    springCacheManager.setDynamicCacheConfiguration(this.createDefaultCache(null));
    return springCacheManager;
}

private org.apache.ignite.configuration.CacheConfiguration createDefaultCache(String name) {

    org.apache.ignite.configuration.CacheConfiguration cacheConfiguration = new org.apache.ignite.configuration.CacheConfiguration();
    cacheConfiguration.setName(name);
    cacheConfiguration.setCacheMode(CacheMode.PARTITIONED);
    cacheConfiguration.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    cacheConfiguration.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
    cacheConfiguration.setStatisticsEnabled(true);
    cacheConfiguration.setEvictSynchronized(true);
    return cacheConfiguration;
 }
}

If we were to create another instance of this service and have it register to the same grid as igniteConfiguration.setGridName("petclinic-ignite-grid"), an IMDG will be created. Please note that the 2 service instances with this version of partitioned, embedded distributed cache should be able to talk to each other via required PORTS. Please refer to Apache Ignite - Data Grid for more details.

Hope this helps.

sg4j
  • 39
  • 5