本文介绍了Ehcache web CacheManager.replaceCacheWithDecoratedCache抛出NPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的设置:Struts2网络应用程序,添加到它Ehcache-Web并试图使其工作。

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

这是我的 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筛选器

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>

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

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)

按照Ehcache网站上的说明:

Followed these instructions from Ehcache website:

从其repo手动下载正确的eccache-web和ehcache-core源代码:

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

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

And checked the lines of code reported in the log:

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;

950)是:

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

但我不知道是什么。有人可能已经这样告诉我我做错了什么,或者我错过了什么。我只是想做一些简单的缓存我的HTML内容。谢谢!

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!

推荐答案

好吧,对我个人的尴尬我解决了。这里是简短的回答:

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

我的项目也使用Hibernate。 Hibernate自带了一个旧版本的Ehcache。在我的Maven设置发生了什么是旧版本的Ehcache(版本1.2.3),Hibernate附带的一个优先于我添加自己的(ehcache-web v 2.0.4& ehcache-core 2.4 .6)。因为旧的Hibernate版本的Ehcache不支持在 SimplePageCachingFilter 中命名缓存,所以我有错误。

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.

长答案:

为了解决这个问题,我在项目的pom.xml中从我的Hibernate依赖项中排除了Ehcache依赖,如下: / p>

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>

我发现这个问题是使用Maven依赖树命令,如下所示:

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

这向我展示了来自Hibernate的 net.sf.ehcache:ehcache:jar:1.2.3 依赖,它依次来自 g1 :modelApp:jar:2.0:compile 具有优先级。添加如下所示的exlude之后,重做依赖关系树命令会显示我:

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

事实上,使用这个设置,我可以使用 SimplePageCachingFilter 配置如我的问题所述,一切开始确定,没有更多的例外。 Ehcache日志还显示过滤器实际上是工作和做东西。

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.

我真的很感谢@jeha帮助我,把我放在右边方向。

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

这篇关于Ehcache web CacheManager.replaceCacheWithDecoratedCache抛出NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 20:45