简体   繁体   English

Spring,Infinispan和JBoss 7集成

[英]Spring, Infinispan and JBoss 7 integration

I'm trying to use JBoss 7 Infinispan cache as a communication form (something more later) of two war-deployed spring-based apps. 我正在尝试使用JBoss 7 Infinispan缓存作为两个战争部署的基于Spring的应用程序的通信形式(更晚一些)。 I'm having a problem with accessing the JBoss managed cache managers. 我在访问JBoss托管缓存管理器时遇到问题。

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 ? 有没有办法在不使用@ManagedBean注释和Java EE标准的情况下访问JBoss服务器创建的缓存?


It's done. 完成。 Thanks to Kazaag, I used JNDI. 感谢Kazaag,我使用了JNDI。

JndiTemplate jndiTemplate = new JndiTemplate();
jndiTemplate.lookup("java:jboss/infinispan/container/cluster");

I had the well known problem with a DefaultEmbeddedCacheManager Class Cast Exception. 我有一个众所周知的DefaultEmbeddedCacheManager类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 ). 您可以通过Spring的JNDI支持检索应用程序服务器管理的缓存容器(JNDI名称为java:jboss/infinispan/my-container-name )。 So Spring will be responsible to make sure every part are using the same container. 所以Spring将负责确保每个部件都使用相同的容器。

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). 我不是100%确定你会获得相同的缓存,它可能会返回一个特定于应用程序的缓存(2个应用程序数据对象实际上来自不同的类加载器)。

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 有一天晚些时候,但可以在这里找到有关通过JNDI访问infinispance缓存存储的信息

With that a JNDI lookup I get the CacheContainer 通过JNDI查找,我获得了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 不在我的Spring Bean中工作

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

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