[英]How to enable infinispan cache in my WEB application?
这是问题所在:
我有一个Dynamic Web Application
,这里有一个sessions
列表作为一个静态字段,并且我现在正在研究将来可能出现的群集问题。
我想将我的静态HashMap
移到一个可以独立于服务器访问的地方,换句话说,一旦我有2台服务器,并且其中一台具有静态HashMap
的服务器消失,另一台服务器应该能够使用以下方法恢复HashMap
在infinispan
缓存中,不会因服务器故障而丢失。
因此,我尝试实现一些EmbededCacheManager
和一些CashContainers
,但是在大多数情况下,我遇到了一个问题,就是我无法将infinispan
jar添加到我的项目中,而无法使用infinispan
缓存。
我四处搜寻,但找不到将依赖项添加到我的WEB项目中的方法。 网上的所有教程(例如本教程): http : //infinispan.org/docs/stable/getting_started/getting_started.html ,都在使用Maven,但我没有。 我需要一个免费的Maven解决方案。
另外,我的代码:
static List<Session> sessions = new ArrayList<Session>();
我想工作的是:
@Resource(lookup = "java:jboss/infinispan/container/myCache")
public CacheContainer myCache;
但是我根本做不到。 我在网上搜索了一下,发现我需要将infinispan依赖项添加到MANIFEST.MF文件中的项目中,一旦完成,就需要这样:
Manifest-Version: 1.0
Dependencies: org.infinispan export
我将manifest
文件夹添加到src \\ META-INF文件夹中(我也创建了该文件夹,因为它不存在),现在可以导入infinispan.cache
但是然后,我无法构建我的整个项目,在我添加的部分中,它始终在我的standalone.xml
文件中显示一些错误。
这是代码:
<subsystem xmlns="urn:jboss:domain:infinispan:4.0">
<cache-container name="myCache" default-cache="default" module="org.wildfly.clustering.server">
<transport lock-timeout="60000"/>
<replicated-cache name="sessions" mode="SYNC">
</replicated-cache>
</cache-container>
...
<\subsystem>
在控制台中,WildFly 10 btw是一行写的,显示出现问题的行和列,问题出在第二行和第四列(我不知道第四列在哪里,因为standalone.xml的前几个字符是制表符...?)
希望您能解决我的问题,因为我不知道下一步该怎么做。 谢谢。
好的,我按照您的步骤进行了一些更改,
Java的:
@Resource(lookup = "java:jboss/infinispan/container/myCache")
public CacheContainer myCache;
Standalone.xml
<replicated-cache name="sessions" mode="SYNC">
<transaction mode="BATCH"/> //added this line
</replicated-cache>
另外,在同一个standalone.xml
文件中,由于缺少jgroup
子系统的依赖关系而出现错误
<subsystem xmlns="urn:jboss:domain:jgroups:4.0">
解决方案是,我在standalone-full-ha.xml中找到了与jgroup有关的所有依赖jgroups
,然后将它们全部复制到了standalone.xml
(我建议他为该任务建立Total Commander,工具比较两个文件)
您的MANIFEST.MF
文件是正确的,并且他在src / META-INF文件夹中的位置也是正确的。
我曾经在infinispan
遇到过类似的问题,所有问题都与Maven及其依赖项有关,但是有一个解决方法。
您需要进入wildfly
文件夹,在该文件夹中找到module \\ system \\ layers \\ base \\ org \\ infinispan \\ main文件夹,在其中找到以下文件:infinispan-core-8.2.4.Final(也许是其他版本),然后您必须转到:Wildfly \\ module \\ system \\ layers \\ base \\ org \\ infinispan \\ commons \\ main那里,您将找到此文件:infinispan-commons-8.2.4.Final(也许是其他版本)
这些文件是您的wildfly
使用的文件(显然是:)),它们具有所需的infinispan
函数的正确版本。
将两个文件都复制到WEB / WebContent / WEB-INF / lib(我确定您还有其他jar)。如果还有其他infinispan
jar,请将其删除,因为使用与服务器相同的版本很重要。
完成后,您可以执行以下操作:
Java的:
private List<Session> sessions() {
Cache<Integer, List<Session>> c = myCache.getCache("sessions");
// since you List is not a HashMap, you will need to make sure that
// you get this right
List<Session> l = c.get(1); // this returns the List, but with the key 1, read all the code, you will understand
if (l != null) { // if its ok, then return the list
return l;
} else { // you need to make sure the list exist in the cache, just for the first time, all the other times, l will be different then null
l = new ArrayList<Session>(); // make an empty list
c.put(1, l); //add it to the cache
return l; // use the list as you wish
}
}
这将允许您使用直接从缓存中获取的会话列表。
希望我能为您服务。 否则,祝您好运,您将需要它:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.