ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java-Ehcache网站CacheManager.replaceCacheWithDecoratedCache引发NPE

2019-11-02 02:00:15  阅读:207  来源: 互联网

标签:configuration ehcache java


我有一个非常基本的设置:Struts2 Web应用程序,将其添加到Ehcache-Web并尝试使其工作.

这是我的ehcache.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false"
         dynamicConfig="false"
         >
    <diskStore path="java.io.tmpdir"/>

    <defaultCache
       maxElementsInMemory="100"
       maxElementsOnDisk="1000" 
       eternal="false"
       overflowToDisk="true"
       diskPersistent="false"
       memoryStoreEvictionPolicy="LRU"
       timeToIdleSeconds="120000"
       timeToLiveSeconds="120000"
       />

    <cache name="searchDspCache"
       maxElementsInMemory="100"
       maxElementsOnDisk="1000" 
       eternal="false"
       overflowToDisk="true"
       diskPersistent="false"
       memoryStoreEvictionPolicy="LRU"
       timeToIdleSeconds="120000"
       timeToLiveSeconds="120000"    
    />   

</ehcache>

这是我配置Ehcache Web过滤器的方式

<filter>
        <filter-name>searchDspCachingFilter</filter-name>
        <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>
        <init-param>
            <param-name>suppressStackTrace</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>cacheName</param-name>
            <param-value>searchDspCache</param-value>
        </init-param>
    </filter>

当应用程序部署在Jetty或Glassfish中时,我会立即收到此错误:

20111118T115824,294 [sales-web]  FATAL [Timer-1] (net.sf.ehcache.constructs.web.filter.Filter:201) - Could not initialise servlet filter.
java.lang.NullPointerException
    at net.sf.ehcache.CacheManager.replaceCacheWithDecoratedCache(CacheManager.java:950)
    at net.sf.ehcache.constructs.web.filter.CachingFilter.doInit(CachingFilter.java:92)

遵循Ehcache网站上的这些说明:

http://ehcache.org/documentation/modules/web-caching

从其仓库中手动下载正确的eccache-web和ehcache-core来源:

https://oss.sonatype.org/content/groups/sourceforge/net/sf/ehcache/

并检查了日志中报告的代码行:

net.sf.ehcache.constructs.web.filter.CachingFilter.doInit(CachingFilter.java:92)是:

/**
 * The cache name can be set through init parameters. If it is set it is
 * stored here.
 */ <---- this is line 92
protected String cacheName;

在net.sf.ehcache.CacheManager.replaceCacheWithDecoratedCache(CacheManager.java:950)处是:

// NPE guard
if (cacheName == null || cacheName.length() == 0) {
    return;
} <-- this is line 950

我知道我严重错过了一些东西,但我不知道是什么.已经做完这件事的人可以告诉我我做错了什么,或者我错过了什么.我只想对HTML内容进行一些简单的缓存.谢谢!

解决方法:

} <-- this is line 950

我认为这不是有问题的代码行-您是否查看了正确的源代码?快速浏览一下,CacheManager.replaceCacheWithDecoratedCache of Ehcache version 1.5是一个更有希望的:

if (!ehcache.equals(decoratedCache)) { // line 950

解释NPE,这意味着ehcache为null.

在ehcache.xml中将缓存命名为SimplePageCachingFilter:

<cache name="SimplePageCachingFilter" ... />

它会工作.原因是SimplePageCachingFilter必须具有一个名为SimplePageCachingFilter的缓存!出于相同的原因,您可以从web.xml中省略cacheName的init参数.

标签:configuration,ehcache,java
来源: https://codeday.me/bug/20191102/1987947.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有