Question

I'm trying to create an application that can save parameters using Infinispan DefaultCacheManager. My target is that two processes will be able to see the cached parameters from each other.

I was able to do it by using a config.xml file, but I can't find a way to do it without the xml file.

When I am removing the xml file from the DefaultCacheManager constructor, the cached parameters is available only for the running process and not between the two of them.

Is it possible to do it without the xml file?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top