I'm trying to use JBoss 7 Infinispan cache as a communication form (something more later) of two war-deployed spring-based apps. I'm having a problem with accessing the JBoss managed cache managers.
When I use
DefaultCacheManager cacheManager = new DefaultCacheManager();
cache = cacheManager.getCache();
on each of two applications, I get two separate caches. Is there any way to access the cache created by JBoss server without using the @ManagedBean annotation and Java EE standard at all ?
It's done. Thanks to Kazaag, I used JNDI.
JndiTemplate jndiTemplate = new JndiTemplate();
jndiTemplate.lookup("java:jboss/infinispan/container/cluster");
I had the well known problem with a DefaultEmbeddedCacheManager Class Cast Exception. I used reflections.
Map<Object, Object> cache;
JndiTemplate jndiTemplate = new JndiTemplate();
Object cacheManager;
try {
cacheManager = (Object) jndiTemplate.lookup("java:jboss/infinispan/container/cluster");
Method method = cacheManager.getClass().getMethod("getCache");
cache = (Map) method.invoke(cacheManager);
} catch (Exception e) {
e.printStackTrace();
return;
}
Moreover I had to mark container as started eagerly.
<cache-container name="cluster" aliases="ha-partition" default-cache="default">
<transport lock-timeout="60000"/>
<replicated-cache name="default" mode="SYNC" start="EAGER" batching="true">
<locking isolation="REPEATABLE_READ"/>
</replicated-cache>
</cache-container>
The cache is replicated although different class loaders.
If each application are using there own cache manager, they will have separated cached.
You can retrieve the cache container managed by the application server via JNDI support of Spring (The JNDI name is java:jboss/infinispan/my-container-name
). So Spring will be responsible to make sure every part are using the same container.
I am not 100% sure you will get the same cache, it may return you a application specific cache (the 2 applications data object are in fact coming from different class loader).
Embedded cache is probably not mean for inter application communication. You probably need to use the client/server paradigm.
A bit late in the day but the information on accessing the infinispance cache store via JNDI can be found here
With that a JNDI lookup I get the CacheContainer
<jee:jndi-lookup id="cache1"
jndi-name="java:jboss/infinispan/container/jbossas7-quickstart"
cache="true" resource-ref="false" lookup-on-startup="true" />
which I inject via a setter
public void setContainer(CacheContainer container) {
this.container = container;
}
and now I have access to the cachestore. Note the suggestions here
@Resource(lookup="java:jboss/infinispan/container/my-container-name")
@Resource(lookup="java:jboss/infinispan/cache/my-container-name/my-cache-name")
do not work within my Spring Bean
来源:https://stackoverflow.com/questions/12858624/spring-infinispan-and-jboss-7-integration