Apache Shiro EhCache initialization exception: Another unnamed CacheManager already exists in the same VM

时光总嘲笑我的痴心妄想 提交于 2019-12-06 02:38:22

问题


I am trying to get EhCache configured to handle authorization caching in my Apache Shiro enabled web service. Currently I am getting the following exception:

org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.

My shiro.ini looks like:
[main]
...
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager cacheManager.cacheManagerConfigFile = classpath:ehcache.xml
securityManager.cacheManager = $cacheManager

From this StackOverflow post it looks like people using Spring have gotten around this issue by forcing the CacheManager to be a singleton: Another unnamed CacheManager already exists in the same VM (ehCache 2.5).

Is anybody aware of work-arounds not using Spring initialization (I'm working within the dropwizard framework and have no need to pull in Spring)? Is there some manner of enforcing singleton configuration from the shiro.ini?

Thank you in advance!


回答1:


Create a custom class that extends EhCacheManager and set your cacheManager in the constructor. This (net.sf.ehcache.CacheManager.create()) allows you to reuse an already existing cachemanager.

package lekkie.omotayo

public class MyShiroCacheManager extends EhCacheManager
{

    public CacheManager()
    {
        setCacheManager(net.sf.ehcache.CacheManager.create());
    }
}

Then you can do this:

cacheManager = lekkie.omotayo.MyShiroCacheManager
cacheManager.cacheManagerConfigFile = classpath:ehcache.xml
securityManager.cacheManager = $cacheManager


来源:https://stackoverflow.com/questions/11139653/apache-shiro-ehcache-initialization-exception-another-unnamed-cachemanager-alre

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