Kris
Kris

Reputation: 8873

EhCache: Simple Program not working

I'm new to EhCache, and on the way to implement as a distributed cache service. I was trying out simple programs, but not able to work out the error. I'm able to store data to cache, but not able to retrieve it.

These are two simple programs i wrote for testing.

    package com.db.tests;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class TestCachePut {

    public TestCachePut() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        CacheManager cache= CacheManager.create("E:/ehcache-2.6.2/ehcache.xml");

        Cache caches = cache.getCache("cache1");

        Element element= new Element("testKey", "inserted to cache");
        caches.put(element);

        System.out.println("Put in to cache");



    }

}

Program 2:

    package com.db.tests;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class TestCacheGet {

    public TestCacheGet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */

    public static void main(String[] args) {




        CacheManager caches =  CacheManager.getInstance();
        Element val = caches.getCache("cache1").get("testKey");
        System.out.println(" cache content: "+val.getValue());


    }

}

I have the ehcache.xml configured like <cache name="cache1" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="3000" timeToLiveSeconds="6000" memoryStoreEvictionPolicy="LFU" transactionalMode="off"> <persistence strategy="localTempSwap"/> </cache>

Teracotta server is started, and showing its running. When I run Program 1, it runs without any error. After which i ran second one , i got error as NPE,

   24 Dec, 2012 12:05:42 PM net.sf.ehcache.config.ConfigurationFactory parseConfiguration
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/E:/ehcache-2.6.2/lib/ehcache-core-2.6.2.jar!/ehcache-failsafe.xml
Exception in thread "main" java.lang.NullPointerException
    at com.db.tests.TestCacheGet.main(TestCacheGet.java:22)

if i do specify configuration xml in both cases , its generating again NPE with a warning :

4 Dec, 2012 12:22:34 PM net.sf.ehcache.DiskStorePathManager resolveAndLockIfNeeded
WARNING: diskStorePath 'E:\Users\SKRISH~1\AppData\Local\Temp' is already used by an existing CacheManager either in the same VM or in a different process.
The diskStore path for this CacheManager will be set to E:\Users\SKRISH~1\AppData\Local\Temp\ehcache_auto_created1315701513913502723diskstore.
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 i'm doing wrong? Some input please.

Thanks

Upvotes: 0

Views: 4484

Answers (2)

Jaza
Jaza

Reputation: 86

Looks like you want to distribute (and persist over JVM restarts) your cache via Terracotta. In that case, you have to make the cache distributed by adding a TerracottaConfig element pointing to your server inside your ehcache.xml (on same level as your caches)

<terracottaConfig url="localhost:9510" />

After this is in place, add the

<terracotta/> 

tag to your cache to make it distributed.

Hope that helps!

Upvotes: 1

user1885297
user1885297

Reputation: 586

Specify the configuration file in TestCacheGet also:

CacheManager cacheManager =  CacheManager.create("E:/ehcache-2.6.2/ehcache.xml");

[EDITED]

And, localTempSwap strategy is VM specific. Try localRestartable with a diskStore path specified. (Available with enterprise version only)

<ehcache>
<diskStore path="/Users/me/store/data"/>
<cache name="cache1" maxEntriesLocalHeap="10000" maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" timeToIdleSeconds="3000" timeToLiveSeconds="6000" memoryStoreEvictionPolicy="LFU" transactionalMode="off"> <persistence strategy="localRestartable" synchronousWrites="true"/> </cache>
</ehcache> 

Upvotes: 1

Related Questions