Ehcache web CacheManager.replaceCacheWithDecoratedCache throws NPE

北慕城南 提交于 2019-12-12 13:15:41

问题


I have a very basic setup: Struts2 web-app, added to it Ehcache-Web and trying to make it work.

Here's my 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>

Here's how I configured the Ehcache web filter

<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>

When the app is deployed in either Jetty or Glassfish, I get this error right upfront:

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)

Followed these instructions from Ehcache website:

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

Manually downloaded the correct eccache-web and ehcache-core sources from their repo:

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

And checked the lines of code reported in the log:

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

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

at net.sf.ehcache.CacheManager.replaceCacheWithDecoratedCache(CacheManager.java:950) is:

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

I know I'm severely missing something but I don't know what. Can someone who's already done this tell me what I'm doing wrong, or what I'm missing. I just wanna do some simple caching of my HTML content. Thankx!


回答1:


} <-- this is line 950

I don't think this is the problematic line of code - did you look at right sources? From a quick look, CacheManager.replaceCacheWithDecoratedCache of Ehcache version 1.5, is a more promising:

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

which would mean, to explain the NPE, that ehcache is null.

Name your cache SimplePageCachingFilter in your ehcache.xml:

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

and it will work. The reason for that is SimplePageCachingFilter must have a cache named SimplePageCachingFilter! You can leave out the init-param for cacheName from web.xml for the same reason.




回答2:


Ok, well, to my personal embarrassment I've solved it. Here's the short answer:

My project also uses Hibernate. Hibernate comes with an old version of Ehcache. In my Maven setup what happened was that the old version of Ehcache (version 1.2.3), the one that comes with Hibernate was taking priority over the one I was adding myself (ehcache-web v 2.0.4 & ehcache-core 2.4.6). And since the old, Hibernate, version of Ehcache didn't support naming your cache in the SimplePageCachingFilter, I had the error.

The long answer:

To solve it, I've excluded the Ehcache dependency from my Hibernate dependency in the pom.xml of the project like so:

<dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-annotations</artifactId>
          <version>3.4.0.GA</version>
        </dependency>
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate</artifactId>
          <version>3.2.7.ga</version>
          <exclusions>
            <exclusion>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </exclusion>
          </exclusions>
        </dependency>

What I did to discover this issue is use the Maven dependency tree command like so:

C:\_andrei\me\myprojects2\dynamicFormGwtSpringWicket\springMvcApp>mvn dependency:tree -Dverbose -Dincludes=net.sf.ehcache
(...)
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ springMcvApp ---
[INFO] g1:springMcvApp:war:2.0
[INFO] +- g1:modelApp:jar:2.0:compile
[INFO] |  \- org.hibernate:hibernate:jar:3.2.7.ga:compile
[INFO] |     \- net.sf.ehcache:ehcache:jar:1.2.3:compile
[INFO] \- net.sf.ehcache:ehcache-web:jar:2.0.4:compile
[INFO]    \- net.sf.ehcache:ehcache-core:jar:2.4.6:compile

This shows me that the net.sf.ehcache:ehcache:jar:1.2.3 dependency which comes from Hibernate, which in turn comes from g1:modelApp:jar:2.0:compile has priority. After adding the exlude as shown below, redoing the dependency tree command shows me just:

C:\_andrei\me\myprojects2\dynamicFormGwtSpringWicket\springMvcApp>mvn dependency:tree -Dverbose -Dincludes=net.sf.ehcache
(...)
INFO] g1:springMcvApp:war:2.0
INFO] \- net.sf.ehcache:ehcache-web:jar:2.0.4:compile
INFO]    \- net.sf.ehcache:ehcache-core:jar:2.4.6:compile

And in fact with this setup, I can start my app with SimplePageCachingFilter configured as described in my question and everything starts ok, no more exceptions. The Ehcache log also shows that the filter is actually working and doing stuff.

I'd really like to thank @jeha for helping me with this and putting me on the right direction.



来源:https://stackoverflow.com/questions/8181382/ehcache-web-cachemanager-replacecachewithdecoratedcache-throws-npe

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