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:
<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>
...
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]
@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.
Infinispan provides two CacheManager implementations: EmbeddedCacheManager. Entry point for caches when running Infinispan inside the same Java Virtual Machine (JVM) as the client application. RemoteCacheManager.
Infinispan is an open-source in-memory data grid that offers flexible deployment options and robust capabilities for storing, managing, and processing data. Infinispan provides a key/value data store that can hold all types of data, from Java objects to plain text.
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
Instead of injecting CacheManager
you should inject each cache instance. While doing, keep in mind the following points.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With