I'm not sure what you are doing wrong, but yes it's definitely possible. This should work:
public static void main(String[] args) {
Cache cacheA = startSomeNode("A").getCache("someCache");
Cache cacheB = startSomeNode("B").getCache("someCache");
cacheA.put("key", "Hello World!");
Object val = cacheB.get("key");
System.out.println("Retrieved value is: " +val);
}
static DefaultCacheManager startSomeNode(String nodeName) {
GlobalConfiguration glb = new GlobalConfigurationBuilder()
.transport()
.nodeName(nodeName)
.defaultTransport()
.globalJmxStatistics()
.jmxDomain(nodeName)
.build();
ConfigurationBuilder builder = new ConfigurationBuilder();
builder
.clustering()
.cacheMode(CacheMode.REPL_SYNC);
Configuration cacheConfiguration = builder.build();
return new DefaultCacheManager(glb, cacheConfiguration);
}
Make sure you bind it to a loopback interface so that your two processes actually can reach each other even when running in the same machine, or as in the above example in the same process:
-Djgroups.bind_addr=127.0.0.1
If you're running on Linux, make sure you also avoid IPv6 as it's otherwise the default on that platform:
-Djava.net.preferIPv4Stack=true
Infinispan and JGroups support IPv6 too, but sadly it seems common for people to not have IPv6 configured correctly on their host, so I'd advise to start from the simpler IPv4 setup.
Note that I overly simplified the code; when you're done with playing you would probably want to make sure that
- you don't start two CacheManager instances in the same JVM
- you make sure you stop the CacheManager instances