views:

96

answers:

1

Env: Seam 2.2, ehcache-core 2.1.0

I tried injecting the CacheProvider using the following call in my bean scoped for session

    @In CacheProvider cacheProvider;

    WEB-INF\components.xml contains the following line to enable the cache provider
    <cache:eh-cache-provider/>

The above configuration seems to return a null value for the cache provider


Using the cache provider like this         
CacheProvider cacheProvider = CacheProvider.instance();
throws the following warning

    15:29:27,586 WARN [CacheManager] Creating a new instance of CacheManager using
    the diskStorePath "C:\DOCUME~1\user5\LOCALS~1\Temp\" which is already used by an
    existing CacheManager.
    The source of the configuration was net.sf.ehcache.config.generator.Configuratio
    nSource$DefaultConfigurationSource@15ed0f9.
    The diskStore path for this CacheManager will be set to C:\DOCUME~1\user5\LOCALS
    ~1\Temp\\ehcache_auto_created_1276682367586.
    To avoid this warning consider using the CacheManager factory methods to create
    a singleton CacheManager or specifying a separate ehcache configuration (ehcache
    .xml) for each CacheManager instance.

What am I missing here?

+1  A: 

Keep in mind net.sf.ehcache.Cache needs to be on the classpath (I am not sure but I Think ehcache-core.jar contains this class) if you want to use EhCahceProvider. Here goes its signature

@Name("org.jboss.seam.cache.cacheProvider")
@Scope(APPLICATION)
@BypassInterceptors
@Install(value = false, precedence=BUILT_IN, classDependencies="net.sf.ehcache.Cache")
@AutoCreate
public class EhCacheProvider extends CacheProvider<CacheManager> {

Notice classDependencies attribute. Its documentation is clear

Indicates that the component should not be installed unless the the given class definitions are available on the classpath

So if your classpath contains net.sf.ehcache.Cache you do not need to declare

<cache:eh-cache-provider/>

And as it is Application scoped, you can retrieve, besides @In-jection, by using

ApplicationContext.getContext().get("cacheProvider");

UPDATE

First of all

  • remove <cache:eh-cache-provider/> declaration. I said you why (see above)

Second of all

  • Although i am pretty sure CacheProvider can not be null because @In required attribute is, by default, true, which cannot be null. Inside your business method, Make sure your CacheProvider is not null

    assert cacheProvider != null

Third of all

  • I Think you do not need to call cacheProvider.instance() method. If its default scope is Application. Why do you want to retrieve another CacheProvider ??? It does not make sense.

Fourth of all

  • It is not an exception. Its is just a warning message because you is trying to use more than one cache provider where both use the same space in memory
Arthur Ronald F D Garcia
net.sf.ehcache.Cache is part of ehcache-core.jar and is present in ear\lib folder.I have tried the following methods and none seem to work // CacheProvider cacheProvider = CacheProvider.instance();or CacheProvider cacheProvider = (CacheProvider) Contexts .getApplicationContext().get("cacheProvider");or@In CacheProvider cacheProvider;
Joshua
@Joshua Make sure war lib folder does not contain ehcache-core.jar once it is stored in ear lib folder
Arthur Ronald F D Garcia
the jar is present only in ear\lib folder
Joshua
@Joshua See update
Arthur Ronald F D Garcia
I did remove the <cache:eh-cache-provider/> declaration and it still comes up with a NullPointer Exception.
Joshua
@Joshua. I believe you. On weekend, i will Try it by myself what you are trying to do. But let me know: which App server do you use ??? EAR or WAR ???
Arthur Ronald F D Garcia
JBoss 5.1.0, JDK 6, Seam 2.2.0, ehcache-core 2.0.1, standard EAR file containing JPA entity classes in a ejb module and a set of xhtml files. Thanks for all the support you have given so far.
Joshua
@Joshua Which Tutorial do you follow ??? Do you use Ajax ??? Do you use JBoss connection pool ??? plain JPA or Hibernate ???
Arthur Ronald F D Garcia