Creating caches programmatically using Infinispan 4.2.0.Final
<?xml version="1.0" encoding="UTF-8"?>
<infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:infinispan:config:4.0">
<global>
<globalJmxStatistics enabled="true" jmxDomain="infinispan" allowDuplicateDomains="true"/>
<transport clusterName="local-infinispan-cluster" />
</global>
<default>
<jmxStatistics enabled="true" />
<clustering mode="replication">
<sync replTimeout="20000" />
</clustering>
</default>
</infinispan>
package org.modeshape.connector.infinispan;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.apache.log4j.Logger;
import org.infinispan.Cache;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.junit.Test;
public class RawInfinispanClusterTest {
@Test
public void shouldWorkAndDoes() throws Exception {
EmbeddedCacheManager manager1 = new DefaultCacheManager("infinispan_clustered_config.xml");
Cache<String, String> cache1 = manager1.getCache("cache");
EmbeddedCacheManager manager2 = new DefaultCacheManager("infinispan_clustered_config.xml");
Cache<String, String> cache2 = manager2.getCache("cache");
cache1.put("key1", "value1");
cache1.put("key2", "value2");
// Make sure the keys are accessible from cache2 ...
String v1 = cache2.get("key1");
String v2 = cache2.get("key2");
assertThat(v1, is("value1"));
assertThat(v2, is("value2"));
Logger.getLogger(getClass()).info("*** Getting ready to create 'new-cache' programmatically");
// now create a new cache, but have to do this on both managers! ...
manager1.defineConfiguration("new-cache", manager1.getDefaultConfiguration());
Cache<String, String> cache1b = manager1.getCache("new-cache");
manager2.defineConfiguration("new-cache", manager2.getDefaultConfiguration());
Cache<String, String> cache2b = manager2.getCache("new-cache");
Logger.getLogger(getClass()).info("*** Getting ready to insert entry into 'new-cache'");
cache1b.put("key1b", "value1b");
Logger.getLogger(getClass()).info("*** Getting ready to find 'new-cache' from second manager");
String v1b = cache2b.get("key1b");
assertThat(v1b, is("value1b"));
}
@Test
public void shouldWorkButDoesNot() throws Exception {
EmbeddedCacheManager manager1 = new DefaultCacheManager("infinispan_clustered_config.xml");
Cache<String, String> cache1 = manager1.getCache("cache");
EmbeddedCacheManager manager2 = new DefaultCacheManager("infinispan_clustered_config.xml");
Cache<String, String> cache2 = manager2.getCache("cache");
cache1.put("key1", "value1");
cache1.put("key2", "value2");
// Make sure the keys are accessible from cache2 ...
String v1 = cache2.get("key1");
String v2 = cache2.get("key2");
assertThat(v1, is("value1"));
assertThat(v2, is("value2"));
Logger.getLogger(getClass()).info("*** Getting ready to create 'new-cache' programmatically");
// now create a new cache ...
manager1.defineConfiguration("new-cache", manager1.getDefaultConfiguration());
Cache<String, String> cache1b = manager1.getCache("new-cache");
cache1b.start();
Logger.getLogger(getClass()).info("*** Getting ready to insert entry into 'new-cache'");
cache1b.put("key1b", "value1b");
Logger.getLogger(getClass()).info("*** Getting ready to find 'new-cache' from second manager");
manager2.defineConfiguration("new-cache", manager2.getDefaultConfiguration());
Cache<String, String> cache2b = manager2.getCache("new-cache");
cache1b.start();
String v1b = cache2b.get("key1b");
assertThat(v1b, is("value1b"));
}
}