Reputation:
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.
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.
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.
I searched around, and I could not find a way to add the dependency to my WEB project. 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. I need a Maven free solution to this.
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:
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
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.
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...?)
Hope you got my problem, because, I have no idea what to do next. Thank you.
Upvotes: 0
Views: 849
Reputation: 2066
Ok, I followed your steps, and made it work with a few changes,
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>
Also, in the same standalone.xml
file, I got an error with the missing dependency of the jgroup
subsystem
<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)
Your MANIFEST.MF
file is correct, and his position in the src/META-INF folder is also correct.
I had a similar problem once with infinispan
, all work with Maven and his dependencies, but there is a work around that.
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)
These files are the ones witch your wildfly
uses (obviously :)), and they have the proper version of the infinispan
functions that you need.
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.
Once done, you can do something like this:
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 :)
Upvotes: 1