名称为"play"的EhCache实例已存在 [英] EhCache instance with name 'play' already exists

查看:125
本文介绍了名称为"play"的EhCache实例已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用异步 couchdatabase Java驱动程序时,我遇到了 Play框架默认缓存( EHCache )的问题.播放在热重载时崩溃,并显示以下错误:

I faced the issue with the Play framework default cache (EHCache) when working with asynchronous couchdatabase java driver. Play crashes on the hot reload with the error:

Error in custom provider, play.api.cache.EhCacheExistsException: An EhCache instance with name 'play' already exists.

我发现,这不仅可以与 couchdatabase 驱动程序一起使用,而且还可以在其他一些情况下使用,例如

I found this could be not only with the couchdatabase driver but also in some other scenarios, like https://groups.google.com/forum/#!topic/pac4j-dev/2_EUOCrov7M.

推荐答案

我想出了一个解决方案-在停止挂钩上强制关闭缓存.可以在项目中现有的模块之一中完成该操作,例如:

I figure out a solution - force cache shutdown on the stop hook. It could be done in one of an existent module in your project like:

lifecycle.addStopHook(() -> {
  ...
  CacheManager.getInstance().shutdown();
  ...
});

也可以创建特殊的修复"模块:

Special "fix" module could be created as well:

package fixes;

import java.util.concurrent.CompletableFuture;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.google.inject.AbstractModule;

import net.sf.ehcache.CacheManager;
import play.Logger;
import play.Logger.ALogger;
import play.inject.ApplicationLifecycle;

/**
 * Fix for the hot reloading cache issue.
 * "Error in custom provider, play.api.cache.EhCacheExistsException: An EhCache instance with name 'play' already exists."
 *
 */
public class CacheFix  extends AbstractModule{  
    @Override
    protected void configure() {
        bind(CacheFixInstance.class).asEagerSingleton();        
    }
}


/**
 * Only stop hook
 */
@Singleton
class CacheFixInstance {    
    private static ALogger logger = Logger.of(CacheFixInstance.class);

    @Inject
    public CacheFixInstance(ApplicationLifecycle lifecycle) {
        lifecycle.addStopHook(() -> {

            // Force cache to stop.
            CacheManager.getInstance().shutdown();
            logger.debug("Cache has been shutdown");

            // Nothing to return.
            return CompletableFuture.completedFuture(null);
        });
    }
}

application.conf中:

enabled += fixes.CacheFix

这篇关于名称为"play"的EhCache实例已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆