在使用异步 couchdatabase java 驱动程序时,我遇到了 Play 框架 默认缓存 (EHCache) 的问题。热重载时 Play 崩溃并出现错误:
Error in custom provider, play.api.cache.EhCacheExistsException: An EhCache instance with name 'play' already exists.
我发现这不仅可能与couchdatabase驱动程序有关,而且在其他一些场景中也可能发生,例如https://groups.google.com/forum/#!topic/pac4j-dev/2_EUOCrov7M .
最佳答案
我找到了一个解决方案 - 在停止钩子(Hook)上强制关闭缓存。它可以在项目中的现有模块之一中完成,例如:
lifecycle.addStopHook(() -> {
...
CacheManager.getInstance().shutdown();
...
});
也可以创建特殊的“修复”模块:
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
关于playframework - 名称为 'play' 的 EhCache 实例已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43243293/