我想使用两个Wildfly独立实例来复制Infinispan缓存。我想在一个节点上插入一个值,我应该能够在另一个节点上读取它。
这是我尝试的内容:
<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");
}
}
}
插入了编写器上的值,但读取器节点上没有缓存修改,缓存大小始终为0.我尝试了TCP和UDP堆栈。我错过了什么?你能帮帮我吗?
提前致谢。
答案 0 :(得分:2)
尝试直接注入缓存引用(不通过CacheManager填充)。据我了解,这只是迫使infinispan容器在新的WildFly 10中启动它的方法。
@Resource(lookup = "java:jboss/infinispan/cache/monitor/default")
private Cache<String, String> cache;
小心使用JNDI名称(默认值为1)或在配置中明确指定
答案 1 :(得分:0)
您应该注入每个缓存实例,而不是注入CacheManager
。在做的时候,请记住以下几点。
transport
标记添加到cache-container
。复制或分布式模式需要这样做。 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>
按如下方式注入资源
@Resource(lookup = "java:jboss/infinispan/replicated_cache/customer")
private Cache<String, Customer> customerCache;