简体   繁体   English

如何在我的WEB应用程序中启用infinispan缓存?

[英]How to enable infinispan cache in my WEB application?

Here is the problem: 这是问题所在:

I have a Dynamic Web Application , there I have a list of sessions as a static field, and I am working now on a possible clustering problem in the future, that might come up. 我有一个Dynamic Web Application ,这里有一个sessions列表作为一个静态字段,并且我现在正在研究将来可能出现的群集问题。

I want to move my static HashMap to a place where it can be accessed independent of the server, in other words, once I have 2 servers, and the one with the static HashMap dies, the other server should be able to recover the HashMap using the infinispan cache, witch will not be lost due to the servers failure. 我想将我的静态HashMap移到一个可以独立于服务器访问的地方,换句话说,一旦我有2台服务器,并且其中一台具有静态HashMap的服务器消失,另一台服务器应该能够使用以下方法恢复HashMapinfinispan缓存中,不会因服务器故障而丢失。

So, what I have tried is to implement some EmbededCacheManager and some CashContainers , but for the most part, I got the problem that I simply could not add the infinispan jar to my project, and use the freaking cache. 因此,我尝试实现一些EmbededCacheManager和一些CashContainers ,但是在大多数情况下,我遇到了一个问题,就是我无法将infinispan jar添加到我的项目中,而无法使用infinispan缓存。

I searched around, and I could not find a way to add the dependency to my WEB project. 我四处搜寻,但找不到将依赖项添加到我的WEB项目中的方法。 All the tutorials on the net, such as this one: http://infinispan.org/docs/stable/getting_started/getting_started.html , are using Maven, but I don't. 网上的所有教程(例如本教程): http : //infinispan.org/docs/stable/getting_started/getting_started.html ,都在使用Maven,但我没有。 I need a Maven free solution to this. 我需要一个免费的Maven解决方案。

Also, my code: 另外,我的代码:

static List<Session> sessions = new ArrayList<Session>(); 

And what I want to work: 我想工作的是:

@Resource(lookup = "java:jboss/infinispan/container/myCache")
public CacheContainer myCache;

But I simply cannot make it right. 但是我根本做不到。 I searched around on the net, and found out that I need to add the infinispan dependency to my project in the MANIFEST.MF file, and once I did it like this: 我在网上搜索了一下,发现我需要将infinispan依赖项添加到MANIFEST.MF文件中的项目中,一旦完成,就需要这样:

Manifest-Version: 1.0
Dependencies: org.infinispan export

I added my manifest folder to the src\\META-INF folder (I created that folder too, because it was not there), and now I could import the infinispan.cache 我将manifest文件夹添加到src \\ META-INF文件夹中(我也创建了该文件夹,因为它不存在),现在可以导入infinispan.cache

But then, I could not build my entire project, it was always showing some errors in my standalone.xml file, on the part what I added. 但是然后,我无法构建我的整个项目,在我添加的部分中,它始终在我的standalone.xml文件中显示一些错误。

Here is the code: 这是代码:

<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>

In the console, WildFly 10 btw, was a line written showing the row and the column that are making problems, and the problem was at the 2nd line, and the 4th column (I have no idea where the 4th column is, since, in the standalone.xml the first few characters were tabs...?) 在控制台中,WildFly 10 btw是一行写的,显示出现问题的行和列,问题出在第二行和第四列(我不知道第四列在哪里,因为standalone.xml的前几个字符是制表符...?)

Hope you got my problem, because, I have no idea what to do next. 希望您能解决我的问题,因为我不知道下一步该怎么做。 Thank you. 谢谢。

Ok, I followed your steps, and made it work with a few changes, 好的,我按照您的步骤进行了一些更改,

Java: Java的:

@Resource(lookup = "java:jboss/infinispan/container/myCache")
public CacheContainer myCache;

Standalone.xml Standalone.xml

<replicated-cache name="sessions" mode="SYNC">
      <transaction mode="BATCH"/>   //added this line
</replicated-cache>

Also, in the same standalone.xml file, I got an error with the missing dependency of the jgroup subsystem 另外,在同一个standalone.xml文件中,由于缺少jgroup子系统的依赖关系而出现错误

<subsystem xmlns="urn:jboss:domain:jgroups:4.0">

The solution for this, I found in the standalone-full-ha.xml all the dependencies that you need regarding the jgroups , and simply copied them all over to the standalone.xml (I recommend the Total Commander for this task, he has built-in tool to compare two files) 解决方案是,我在standalone-full-ha.xml中找到了与jgroup有关的所有依赖jgroups ,然后将它们全部复制到了standalone.xml (我建议他为该任务建立Total Commander,工具比较两个文件)

Your MANIFEST.MF file is correct, and his position in the src/META-INF folder is also correct. 您的MANIFEST.MF文件是正确的,并且他在src / META-INF文件夹中的位置也是正确的。

I had a similar problem once with infinispan , all work with Maven and his dependencies, but there is a work around that. 我曾经在infinispan遇到过类似的问题,所有问题都与Maven及其依赖项有关,但是有一个解决方法。

What do you need is to go to the wildfly folder, there you will find the folder module\\system\\layers\\base\\org\\infinispan\\main there you will find this file: infinispan-core-8.2.4.Final (maybe an other version) and then you must go to: Wildfly\\module\\system\\layers\\base\\org\\infinispan\\commons\\main there you will find this file: infinispan-commons-8.2.4.Final (maybe an other version) 您需要进入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(也许是其他版本)

These files are the ones witch your wildfly uses (obviously :)), and they have the proper version of the infinispan functions that you need. 这些文件是您的wildfly使用的文件(显然是:)),它们具有所需的infinispan函数的正确版本。

Copy both files to the WEB/WebContent/WEB-INF/lib (I am sure you got other jars there) If there are other infinispan jars, remove them, because its important to use the same versions as you server. 将两个文件都复制到WEB / WebContent / WEB-INF / lib(我确定您还有其他jar)。如果还有其他infinispan jar,请将其删除,因为使用与服务器相同的版本很重要。

Once done, you can do something like this: 完成后,您可以执行以下操作:

Java: 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
    }
}

This will allow you to use the session list which is directly fetched form the cache. 这将允许您使用直接从缓存中获取的会话列表。

Hope I could help you. 希望我能为您服务。 Else, good luck, you will need it :) 否则,祝您好运,您将需要它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM