6

I would like to use a replicated Infinispan cache using two Wildfly standalone instances. I want to insert a value on one node and I should be able to read it on the other node.

Here's what I tried:

  • I unzipped the full WF10 distribution using two different virtual maschines running Debian Jessie.
  • I run both maschines with the standalone-full-ha.xml config.
  • I changed the binding from localhost to the IP adresses of the VMs - all ports are reachable from outside.
  • I added another cache by inserting the following code to the config:
<subsystem xmlns="urn:jboss:domain:infinispan:4.0">  
  <cache-container name="monitor" default-cache="default">  
    <transport lock-timeout="60000"/>  
    <replicated-cache name="default" mode="SYNC">  
      <transaction mode="BATCH"/>  
    </replicated-cache>  
  </cache-container>  
...  
  • The rest of the configuration is not modified.
  • On both nodes I get the following log entries (my interpretation is - both nodes see each other):
2016-03-13 11:19:43,160 INFO  [org.infinispan.remoting.transport.jgroups.JGroupsTransport] (MSC service thread 1-1) ISPN000094: Received new cluster view for channel monitor: [wf1|5] (2) [wf1, wf2]
  • On one node I created a cache writer. On the other node a cache reader is deployed:
@Singleton  
@Startup  
public class CacheWriter {  

    private final static Logger LOG = LoggerFactory.getLogger(CacheWriter.class);  

    @Resource(lookup = "java:jboss/infinispan/container/monitor")  
    private EmbeddedCacheManager cacheManager;  

    private Cache<String, String> cache;  

    @PostConstruct  
    public void init() {  
        cache = cacheManager.getCache();  
        LOG.info("Cache name: " + cache.getName());  
    }  

    @Schedule(hour = "*", minute = "*", second = "0", persistent = false)  
    public void createDateString() {  
        Long date = new Date().getTime();  
        updateCache("date", date.toString());  
    }  

    public void updateCache(String key, String value) {  
        if (cache.containsKey("date")) {  
            LOG.info("Update date value: " + value);  
            cache.put(key, value);  
        } else {  
            LOG.info("Create date value: " + value);  
            cache.put(key, value);  
        }  
    }  
}
@Singleton  
@Startup  
public class CacheReader {  

    private final static Logger LOG = LoggerFactory.getLogger(CacheReader.class);  

    @Resource(lookup = "java:jboss/infinispan/container/monitor")  
    private EmbeddedCacheManager cacheManager;  

    private Cache<String, String> cache;  

    @PostConstruct  
    public void init() {  
        cache = cacheManager.getCache();  
        LOG.info("Cache name: " + cache.getName());  
    }  

    @Schedule(hour = "*", minute = "*", second = "10", persistent = false)  
    public void readDateString() {  
        LOG.info("Cache size: " + cache.keySet().size());  
        if (cache.containsKey("date")) {  
            LOG.info("The date value is: " + cache.get("date"));  
        } else {  
            LOG.warn("No date value found");  
        }  
    }  
}  

The values on the writer are inserted but there are no cache modifications on the reader node and the cache size is always 0. I tried the TCP and the UDP stack. What am I missing? Can you help me.

Thanks in advance.

Dieter Scholz
  • 71
  • 1
  • 3
  • Does getCacheName return the same name? When I use cache manager I normally ask for an explicitly named cache – Will Tatam Mar 16 '16 at 09:37
  • You could also try using one of the existing cache managers, e.g ejb – Will Tatam Mar 16 '16 at 09:37
  • The cache name is the same on both nodes and changing the injected cache to ejb doesn't help either - so no success. – Dieter Scholz Mar 16 '16 at 18:50
  • You do have the wildfly nodes clustered right? It will only work in clustered mode. Just sanity checking here. – sagneta Mar 18 '16 at 18:00
  • Yes, I use the standalone-full-ha.xml config and I think because of the log message above, that the cluster connection is established. – Dieter Scholz Mar 18 '16 at 19:27
  • I can't see what is wrong right now, but I'd suggest you try either [this](https://github.com/infinispan/infinispan-quickstart/tree/master/jboss-as7) and see if that works. Alternatively, you could enable TRACE logging for org.infinispan package in the configuration file and follow the put operation... – Galder Zamarreño Mar 22 '16 at 10:26
  • I aslo have a similar problem. The same code works fine in WildFly9, but WildFly10 returns CacheManager without "named configurations". So every time you ask for a cache, you just receive a new default configuration. – Maxim Karavaev Apr 04 '16 at 08:45

2 Answers2

3

Try to directly inject a cache reference (not populating it through the CacheManager). As I understand, this is only way to compel infinispan container to start it in the new WildFly 10.

@Resource(lookup = "java:jboss/infinispan/cache/monitor/default")
private Cache<String, String> cache; 

By careful with the JNDI name (default one) or specify it explicitly in configuration

Maxim Karavaev
  • 186
  • 1
  • 9
1

Instead of injecting CacheManager you should inject each cache instance. While doing, keep in mind the following points.

  • Make sure to enter the correct JNDI name. To avoid any confusion you could explicitly mention the JNDI name in the configuration
  • Add the transport tag to the cache-container. This is needed for replicated or distributed mode.

Sample Configuration in standalone-full-ha.xml

<cache-container name="replicated_cache" default-cache="default" module="org.wildfly.clustering.server" jndi-name="infinispan/replicated_cache">
  <transport lock-timeout="60000"/>
  <replicated-cache name="customer" mode="SYNC" jndi-name="infinispan/replicated_cache/customer">
    <transaction locking="OPTIMISTIC" mode="FULL_XA"/>
    <eviction strategy="NONE"/>
  </replicated-cache>
</cache-container>

Inject the resource as follows

@Resource(lookup = "java:jboss/infinispan/replicated_cache/customer")
private Cache<String, Customer> customerCache;
Isuru
  • 700
  • 9
  • 22