Infinispan Jgroups Crashes after war deploying

隐身守侯 提交于 2019-11-29 18:08:07

There's nothing wrong with using the caches from WildFly's Infinispan subsystem, even via JNDI, so long as you are aware of the lifecycle requirements/constraints of server managed Infinispan resources. In WildFly, all Infinispan resources are created/started on demand, including cache managers, cache configurations, and caches. If no service requires a given Infinispan resource, it is not started (nor is it bound to JNDI). Likewise, when no service any longer requires a given Infinispan resource, it is stopped (and its JNDI binding removed). Thus, in order to lookup an Infinispan resource via JNDI, you must first force it to start. The easiest way to do this is to create a resource reference (i.e. a resource-ref or resource-env-ref). e.g.

<resource-ref>
    <res-ref-name>infinispan/mycontainer</res-ref-name>
    <lookup-name>java:jboss/infinispan/container/mycontainer</lookup-name>
</resource-ref>

You can now lookup the cache manager in your application jndi namespace. e.g.

Context ctx = new InitialContext();
EmbeddedCacheManager manager = (EmbeddedCacheManager) ctx.lookup("java:comp/env/infinispan/mycontainer");

The cache manager will already be started. Also, you should never attempt to stop a server managed cache manager. Additionally, you cannot guarantee that any of the cache configurations that are defined within the Infinispan subsystem for this container are installed. Thus, the use of getCache("...") methods are not a reliable way of obtaining a reference to a server managed cache. If you want to depend on a specific cache as defined in the subsystem, you would create a resource reference for the cache itself. e.g.

<resource-ref>
    <res-ref-name>infinispan/mycache</res-ref-name>
    <lookup-name>java:jboss/infinispan/cache/mycontainer/mycache</lookup-name>
</resource-ref>

You can now lookup the cache directly.

Cache<?, ?> cache = (Cache) ctx.lookup("java:comp/env/infinispan/mycache");

The cache will already be started. Likewise, you should not attempt to stop a server managed cache. It will stop automatically when you application is undeployed or the server is shutdown.

You are not supposed to use the Infinispan/JGroups libraries provided by Wildfly, and JNDI is not really the recommended way to share Cache/CacheManager instances.

Instead, you should deploy your own Infinispan/JGroups version, and then use things like CDI to inject the CacheManager where you need it. This quickstart shows you how you can do that using JBoss Data Grid, which is the supported version of Infinispan.

The repository contains other quickstarts such as this one centered on CDI injection of Infinispan Cache and JSR-107 Cache instances.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!