You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-users@jakarta.apache.org by Craig Johannsen <cr...@ludicorp.com> on 2003/09/10 21:19:33 UTC

Re: Simple Example of Accessing JCS Cache

/*
* JCSTest.java
*
* Created on Sept 10, 2003, 12:21 PM
*
* $Log$
*/

//==============================================================================
// Imports for JCS.
//==============================================================================
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.*;
//==============================================================================
// Imports for java collections.
//==============================================================================
import java.util.*;

/**
* Tests JCS object cache functionality. Uses "cache.ccf" configuration file.
* Not a very realistic example, but shows how to put into and get from a 
cache.
* Also illustrates some properties of the key class, such as what 
functions it
* should implement.
*
* @author Craig Johannsen
*/
public class JCSTest {
/** Primary keys for Dog instances. */
private static final String DOG_ID_01 = "Goldie";
private static final String DOG_ID_02 = "Bowser";
/** Primary keys for Cat instances. */
private static final String CAT_ID_01 = "Fluffy";
private static final String CAT_ID_02 = "Blackie";
private static JCS dfltCache;
private static JCS dogCache;
private static JCS catCache;
private static Hashtable hashTable = new Hashtable();

private class Key {
public String keyValue;
public Key(String val) { keyValue = val; }
public String getKeyValue() { return keyValue; }
public boolean equals(Object obj) {
System.out.println(">>>called equals");
if (obj instanceof Key)
return keyValue.equals(((Key) obj).getKeyValue());
else if (obj instanceof String)
return keyValue.equals(obj);
else
return false;
}
public int hashCode() {
System.out.println(">>>called hashCode");
return keyValue.hashCode();
}
public String toString() { return keyValue; }
}

/** Note that cached items must be Serializable. */
private interface AnimalIF extends java.io.Serializable {
void speak();
String getName();
String toString();
boolean equals(AnimalIF animal);
}

private class Dog implements AnimalIF {
private String name;
public Dog(String name) { this.name = name; }
public String getName() { return name; }
public String toString() { return "A dog named " + name; }
public void speak() { System.out.println(name + " says 'Woof'"); }
public boolean equals(AnimalIF animal) { return 
name.equals(animal.getName()); }
}

private class Cat implements AnimalIF {
private String name;
public Cat(String name) { this.name = name; }
public String getName() { return name; }
public String toString() { return "A cat named " + name; }
public void speak() { System.out.println(name + " says 'Meow'"); }
public boolean equals(AnimalIF animal) { return 
name.equals(animal.getName()); }
}

/** Creates a new instance of TestJCS */
public JCSTest() {
try {
dfltCache = JCS.getInstance("dflt");
String dogCacheName = getCacheName(Dog.class);
System.out.println("dogCache=" + dogCacheName);
dogCache = JCS.getInstance(dogCacheName);
String catCacheName = getCacheName(Cat.class);
System.out.println("catCache=" + catCacheName);
catCache = JCS.getInstance(catCacheName);
} catch (CacheException e) {
System.out.println("Error instantiating cache: " + e);
System.exit(100);
}
}

public String getCacheName(Class objClass) {
return objClass.getName();
}

public void doTest() {
// Testing some hypotheses about Hashtable key comparisons.
System.out.println(">>>Starting Hashtable Key test");
Key key1 = new Key("key1");
hashTable.put(key1, "key one");
Key key2 = new Key("key2");
hashTable.put(key2, "key two");
String val1 = (String) hashTable.get(key1);
System.out.println("key1 value=" + val1);
String val2 = (String) hashTable.get(key2);
System.out.println("key2 value=" + val2);

// Testing some hypotheses about JCS key comparisons.
System.out.println(">>>Starting JCS Key test");
try {
dfltCache.put(key1.toString(), "key one");
dfltCache.put(key2.toString(), "key two");
val1 = (String) hashTable.get(key1.toString());
System.out.println("key1 value=" + val1);
val2 = (String) hashTable.get(key2.toString());
System.out.println("key2 value=" + val2);
} catch (Exception e) {
System.out.println("Exception during KeyTest: " + e);
e.printStackTrace();
};
System.out.println(">>>End of Key tests");

// Create some Dog instances
Dog dog1 = new Dog(DOG_ID_01);
Dog dog2 = new Dog(DOG_ID_02);
System.out.println("dog1=" + dog1);
System.out.println("dog2=" + dog2);

// Create some Cat instances
AnimalIF cat1 = new Cat(CAT_ID_01);
AnimalIF cat2 = new Cat(CAT_ID_02);
System.out.println("cat1=" + cat1);
System.out.println("cat2=" + cat2);

// Add each Dog and Cat instance to the dogCache or catCache.
try {
dogCache.put(dog1.getName(), dog1);
dogCache.put(dog2.getName(), dog2);
catCache.put(cat1.getName(), cat1);
catCache.put(cat2.getName(), cat2);
} catch (CacheException e) {
System.out.println("Error putting animal object into cache: " + e);
System.exit(100);
}

// Ensure that we get the correct reg objects from the reg cache.
Dog dog1a = (Dog)dogCache.get(DOG_ID_01);
System.out.println("get dog1a=" + dog1a);
if (dog1a == null)
System.out.println(">>>ERROR: dog1a is null");
else if (! dog1a.equals(dog1))
System.out.println(">>>ERROR: dog1a does not match dog1");
else {
System.out.println("dog1a is OK, obj=" + dog1a);
dog1a.speak();
}

Dog dog2a = (Dog)dogCache.get(DOG_ID_02);
System.out.println("get dog2a=" + dog2a);
if (dog2a == null)
System.out.println(">>>ERROR: dog2a is null");
else if (! dog2a.equals(dog2))
System.out.println(">>>ERROR: dog2a does not match dog2");
else {
System.out.println("dog2a is OK, obj=" + dog2a);
dog2a.speak();
}

Cat cat1a = (Cat)catCache.get(CAT_ID_01);
System.out.println("get cat1a=" + cat1a);
if (cat1a == null)
System.out.println(">>>ERROR: cat1a is null");
else if (! cat1a.equals(cat1))
System.out.println(">>>ERROR: cat1a does not match cat1");
else {
System.out.println("cat1a is OK, obj=" + cat1a);
cat1a.speak();
}

Cat cat2a = (Cat)catCache.get(CAT_ID_02);
System.out.println("get cat2a=" + cat2a);
if (cat2a == null)
System.out.println(">>>ERROR: cat2a is null");
else if (! cat2a.equals(cat2))
System.out.println(">>>ERROR: cat2a does not match cat2");
else {
System.out.println("cat2a is OK, obj=" + cat2a);
cat2a.speak();
}

// Test removal from the cache.
try {
dogCache.remove(DOG_ID_02);
Dog dogX = (Dog) dogCache.get(DOG_ID_02);
if (dogX != null)
System.out.println(">>>ERROR: dogX is NOT null, obj=" + dogX);
else
System.out.println("dogX is OK (should be null), obj=" + dogX);
} catch (CacheException e) {
System.out.println(">>>Error removing dog2 from location cache: " + e);
System.exit(100);
}
}

/**
* The main program.
*/
public static void main(String[] args) {
try {
JCSTest test = new JCSTest();
test.doTest();
} catch(Exception e) {
System.out.println("Uncaught error: " + e);
e.printStackTrace(); }
System.exit(0);
}

}

====================================================================
File build.sh -- build script for Linux/Unix
CP=".:your_lib/jcs.jar:your_lib/commons-lang.jar"
javac -classpath $CP JCSTest.java

====================================================================
File run.sh -- run script for Linux/Unix (remember to "chmod +x run.sh")
CP=".:/home/craigj/lib/jcs.jar:/home/craigj/lib/commons-lang.jar":/home/craigj/lib/commons-logging.jar
java -classpath $CP JCSTest

====================================================================
File cache.ccf -- JCS configuration file
# DEFAULT CACHE REGION
jcs.default=DC
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=2000
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.default.elementattributes.IsEternal=false
jcs.default.elementattributes.MaxLifeSeconds=2400
jcs.default.elementattributes.IdleTime=1800
jcs.default.elementattributes.IsSpool=false
jcs.default.elementattributes.IsRemote=false
jcs.default.elementattributes.IsLateral=false

#Dog Cache
jcs.region.JCSTest$Dog=
jcs.region.JCSTest$Dog.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.JCSTest$Dog.cacheattributes.MaxObjects=4000
jcs.region.JCSTest$Dog.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.JCSTest$Dog.cacheattributes.UseMemoryShrinker=true
jcs.region.JCSTest$Dog.cacheattributes.MaxMemoryIdleTimeSeconds=3600
jcs.region.JCSTest$Dog.cacheattributes.ShrinkerIntervalSeconds=600
jcs.region.JCSTest$Dog.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.JCSTest$Dog.elementattributes.IsEternal=false
jcs.region.JCSTest$Dog.elementattributes.MaxLifeSeconds=2400
jcs.region.JCSTest$Dog.elementattributes.IdleTime=180
jcs.region.JCSTest$Dog.elementattributes.IsSpool=false
jcs.region.JCSTest$Dog.elementattributes.IsRemote=false
jcs.region.JCSTest$Dog.elementattributes.IsLateral=false

#Cat Cache
jcs.region.JCSTest$Cat=
jcs.region.JCSTest$Cat.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.region.JCSTest$Cat.cacheattributes.MaxObjects=4000
jcs.region.JCSTest$Cat.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.region.JCSTest$Cat.cacheattributes.UseMemoryShrinker=true
jcs.region.JCSTest$Cat.cacheattributes.MaxMemoryIdleTimeSeconds=3600
jcs.region.JCSTest$Cat.cacheattributes.ShrinkerIntervalSeconds=600
jcs.region.JCSTest$Cat.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.region.JCSTest$Cat.elementattributes.IsEternal=false
jcs.region.JCSTest$Cat.elementattributes.MaxLifeSeconds=2400
jcs.region.JCSTest$Cat.elementattributes.IdleTime=180
jcs.region.JCSTest$Cat.elementattributes.IsSpool=false
jcs.region.JCSTest$Cat.elementattributes.IsRemote=false
jcs.region.JCSTest$Cat.elementattributes.IsLateral=false



---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-user-help@jakarta.apache.org


Re: Remote Cache Server usage

Posted by Geoff Waggott <ge...@webartjapan.com>.
Thanks Christian.

It turns out that, although you can specify the config file on the 
command line to RemoteCacheServerFactory, it is the path of the file 
within the class path.

Cheers,
Geoff

Christian Kreutzfeldt wrote:

>  
>
>Maybe. Do you have the properties file within your class path? It must be on
>top-level, because it
>gets read with the reader associated with the implementing class.
>
>
>  
>




---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-user-help@jakarta.apache.org


Re: Remote Cache Server usage

Posted by Christian Kreutzfeldt <kr...@subshell.com>.
Hi,

> So now when trying to start the cache server passing the absolute path
> of a cache.ccf file as it's only parameter I get the following exception:
>
> Exception in thread "main" java.lang.NullPointerException
>         at java.io.Reader.<init>(Reader.java:59)
>         at java.io.InputStreamReader.<init>(InputStreamReader.java:84)
>         at java.io.InputStreamReader.<init>(InputStreamReader.java:73)
>         at java.util.Properties.load(Properties.java:184)
>         at
>
org.apache.jcs.engine.control.CompositeCacheManager.configure(CompositeCache
Manager.java:203)
>         at
>
org.apache.jcs.auxiliary.remote.server.RemoteCacheServer.createCacheManager(
RemoteCacheServer.java:186)
>         at
>
org.apache.jcs.auxiliary.remote.server.RemoteCacheServer.init(RemoteCacheSer
ver.java:158)
>         at
>
org.apache.jcs.auxiliary.remote.server.RemoteCacheServer.<init>(RemoteCacheS
erver.java:145)
>         at
>
org.apache.jcs.auxiliary.remote.server.RemoteCacheServerFactory.startup(Remo
teCacheServerFactory.java:152)
>         at
>
org.apache.jcs.auxiliary.remote.server.RemoteCacheServerFactory.main(RemoteC
acheServerFactory.java:283)
>
> Is this because it can't find the config file I specified on the command
> line or some other properties file?
Maybe. Do you have the properties file within your class path? It must be on
top-level, because it
gets read with the reader associated with the implementing class.

With regards,
  Christian


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-user-help@jakarta.apache.org


Remote Cache Server usage

Posted by Geoff Waggott <ge...@webartjapan.com>.
Hi,

I'm looking at using remote caching but currently can't get the server 
to run.

I checked the JCS project out of CVS and built it using maven.

The src/scripts/startRemoteCache.bat sscript is for windows so I rewrote 
it for linux. It didn't run because there was no 
org.apache.jcs.auxiliary.remote.server.group.RemoteGroupCacheServerFactory 
class in the maven built jcs-1.0-dev.jar, so I tried 
org.apache.jcs.auxiliary.remote.server.RemoteCacheServerFactory instead. 
That didn't run because there was no RMI stub class for 
RemoteCacheServer. Not knowing how to modify the maven project to run 
rmic I wrote an Ant script to do it.

the getGroupKeys method declared in IRemoteCacheService wasn't declared 
properly. After changing to have IOException in its throws clause rmic 
successfully generated the stub.

So now when trying to start the cache server passing the absolute path 
of a cache.ccf file as it's only parameter I get the following exception:

Exception in thread "main" java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:59)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:84)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:73)
        at java.util.Properties.load(Properties.java:184)
        at 
org.apache.jcs.engine.control.CompositeCacheManager.configure(CompositeCacheManager.java:203)
        at 
org.apache.jcs.auxiliary.remote.server.RemoteCacheServer.createCacheManager(RemoteCacheServer.java:186)
        at 
org.apache.jcs.auxiliary.remote.server.RemoteCacheServer.init(RemoteCacheServer.java:158)
        at 
org.apache.jcs.auxiliary.remote.server.RemoteCacheServer.<init>(RemoteCacheServer.java:145)
        at 
org.apache.jcs.auxiliary.remote.server.RemoteCacheServerFactory.startup(RemoteCacheServerFactory.java:152)
        at 
org.apache.jcs.auxiliary.remote.server.RemoteCacheServerFactory.main(RemoteCacheServerFactory.java:283)

Is this because it can't find the config file I specified on the command 
line or some other properties file?

Any help would be greatly appreciated.

Thanks,
Geoff



---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-user-help@jakarta.apache.org