JBOSS Cache Notes:
http://www.redhat.com/docs/en-US/JBoss_Enterprise_Application_Platform/4.3.0.cp04/html/Tree_Cache_Guide/index.html
http://www.redhat.com/docs/en-US/JBoss_Enterprise_Application_Platform/4.3.0.cp04/pdf/Cache_Frequently_Asked_Questions/JBoss_Cache_Frequently_Asked_Questions_CP04.pdf
TreeCache Example:
TreeCache tree = new TreeCache();
tree.setClusterName("demo-cluster");
tree.setClusterProperties("default.xml"); // uses defaults if not provided
tree.setCacheMode(TreeCache.REPL_SYNC);
tree.createService(); // not necessary, but is same as MBean lifecycle
tree.startService(); // kick start tree cache
tree.put("/a/b/c", "name", "Ben");
tree.put("/a/b/c/d", "uid", new Integer(322649));
Integer tmp = (Integer) tree.get("/a/b/c/d", "uid");
tree.remove("/a/b");
tree.stopService();
tree.destroyService(); // not necessary, but is same as MBean lifecycle
TreeCache requires that all keys and node be Serializable. Where this is not a requirement with a PojoCache.
PojoCache extends the functionality of the TreeCache.
JBOSS Cache Websites:
- http://www.jboss.org/jbosscache/
- http://www.jboss.org/wiki/Wiki.jsp?page=CVSRepository
JBoss Cache Version Check:
- java -jar jboss-cache.jar org.jboss.cache.Version
JBoos cache uses JGroups under the covers.
JBossCache has five different cache modes,
- LOCAL - single instance, it will not attempt to replicate anything.
- REPL_SYNC - replicate synchronous between the different cache instances.
- REPL_AYSNC - asynchronous replication betweencache instances.
In the event of a network crash REPL_SYNC will return an error, where REPL_ASYNC will continue to work (However the cache will be out of sync.)
Asynchronous replication is faster (no caller blocking)
ClusterConfig is the attribute where the cluster name is defined. Once a server joins the cluster every change is replicated.
Buddy Replication allows each node to pick one or more 'buddies' in the cluster and only replicate to its buddies. (As of, JBoss Cache 1.4.0)
JBoss Cache is Thread safe.
JBoss Cache allows for presistance and passivation/overflow to a data store.
- Cache Passivation is the process of removing an object from in-memory cache and writing it to a secondary data store (e.g., file system, database) on eviction.
IsolationLevel controls the cache locking.
- NONE
- READ_UNCOMMITTED
- READ_COMMITTED
- REPEATABLE_READ
- SERIALIZABLE
JBoss TreeCache allows for event notification pre and post.
POJOCache uses twice as much memory. Not a good option if running a local cache or not persisting.
Concurrent Access:
JBoss Cache uses a pessimistic locking scheme by default to prevent concurrent access to the same data. Optimistic locking may alternatively be used.
example of how to use JBoss Cache in a standalone (i.e. outside an application server) fashion with dummy transactions:
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
User Transaction tx=(UserTransaction)new InitialContext(prop).lookup("UserTransaction");
TreeCache tree = new TreeCache();
PropertyConfigurator config = new PropertyConfigurator();
config.configure(tree, "META-INF/replSync-service.xml");
tree.createService(); // not necessary
tree.startService(); // kick start tree cache
try {
tx.begin();
tree.put("/classes/cs-101", "description", "the basics");
tree.put("/classes/cs-101", "teacher", "Ben");
tx.commit();
}
catch(Throwable ex) {
try { tx.rollback(); } catch(Throwable t) {}
}
Eviction Policies:
- TreeCache
- org.jboss.cache.eviction.LFUPolicy - Least Frequently Used removed
- org.jboss.cache.eviction.FIFOPolicy - First in First out removed
- org.jboss.cache.eviction.MRUPolicy - Most recently used removed (Why would anyone use this?)
- POJOCache
- org.jboss.cache.aop.eviction.AopLRUPolicy
- ...
Eviction policies can be global or per region.
OOM (OutOfMemoryException) this can be adjusted by modifying the EvictionPolicyConfig attributes.
- wakeUpIntervalInSeconds
- timeToLiveInSeconds
- maxNodes
- region
CacheLoaders are available to store and retrieve data.
- Used to preload a cache.
- Custom CacheLoaders are created by implementing the org.jboss.cache.loader.CacheLoader class.
- There are a few predefined classloaders
JBoss Cache is managed and monitored through the jconsole.
Wednesday, March 25, 2009
Thursday, March 27, 2008
Callable Future - Some code! It's what I do for a living.
package mytest;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor;
public class CallableTest implements Callable {
private String myName = "Unknown";
private Random rand = new Random();
public CallableTest(String name) {
// Wahoo I was given a birth name.
myName = name;
}
public String call() throws Exception {
for(int i = 0; i < 100; i++) {
System.out.println("CallableTest - Name [" + myName + "] Count [" + i + "]");
// Lets snooze the thread for a bit so they get out of sequence.
int value = rand.nextInt(101);
/* Execute Brittany Spears Exception when the value is 42 (The answer to all questions)
if(value == 42) {
String oops = "Ooops I did it again!";
System.out.println(oops);
throw new Exception(oops);
}
*/
Thread.sleep(value * 10);
}
return myName;
}
public String getName() {
return myName;
}
public static void main(String[] args) {
int total = 5;
ExecutorService executor = new ScheduledThreadPoolExecutor(total);
Future[] futures = new Future[total];
for(int i = 0; i < total; i++) {
futures[i] = executor.submit(new CallableTest("Test [" + i + "]"));
}
/*
// Bah, we really didn't want to execute these threads. So lets see what
// happens below.
try {
for(int i = 0; i < total; i++) {
boolean flag = futures[i].cancel(true);
if(flag) {
System.out.println("Cancelled");
}
}
} catch (Exception e) {
System.out.println("Exception caught [" + e + "]");
}
*/
/*
// Even though we have multiple threads this logic lines the threads
// back up in the order of execution (submitting).
try {
for(int i = 0; i < total; i++) {
String name = futures[i].get();
System.out.println("Thread Complete [" + name + "]");
}
} catch (ExecutionException e) {
System.out.println("ExecutionException caught [" + e + "]");
} catch (CancellationException e) {
System.out.println("CancellationException caught [" + e + "]");
} catch (Exception e) {
System.out.println("Exception caught [" + e + "]");
}
*/
// Lets try to find the first completed thread.
try{
// Need to create a bucket mechanism so we know which thread completed.
// As a thread completes we empty the bucket. When the bucket is empty
// everything is done.
Vector bucketOhThreads = new Vector();
for(int i = 0; i < total; i++) {
bucketOhThreads.add(futures[i]);
}
while(bucketOhThreads.size() > 0) {
for(int i = 0; i < bucketOhThreads.size(); i++) {
try {
if (((Future)bucketOhThreads.elementAt(i)).isCancelled() ) {
System.out.println("Thread Cancelled - ThreadCount[" + (bucketOhThreads.size() - 1) + "]");
bucketOhThreads.removeElementAt(i);
}
else if (((Future)bucketOhThreads.elementAt(i)).isDone() ) {
Future future = (Future)bucketOhThreads.elementAt(i);
// Doing the above it loses track that the Future objects were
// returning a String. So we have to type cast.
String name = (String)future.get();
System.out.println("Thread Complete - Name[" + name + "] ThreadCount[" + (bucketOhThreads.size() - 1) + "]");
bucketOhThreads.removeElementAt(i);
}
} catch (ExecutionException e) {
System.out.println("ExecutionException caught - [" + e + "] ThreadCount[" + (bucketOhThreads.size() - 1) + "]");
bucketOhThreads.removeElementAt(i);
} catch (CancellationException e) {
System.out.println("CancellationException caught - [" + e + "]. We are checking the isCancelled property. We should NEVER receive this exception here.");
bucketOhThreads.removeElementAt(i);
}
}
// This is were other work should happen. We really shouldn't have
// this thread waiting around for the other threads to execute.
// We really don't want to spin in a tight loop do we, so let's sleep
// for a bit. ;)
Thread.sleep(100);
}
} catch (Exception e) {
System.out.println("Exception caught [" + e + "]");
}
}
}
/*
* ITEMS Not really messed with:
* - ScheduledThreadPoolExecutor
* - Future.get(timeout)
* - I want to know how we get the name (or any data for that matter) from
* the Thread object as well. Right now we have to wait for the thread
* to complete or generate and exception. (lol, I guess that's enough
* info.)
*/
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor;
public class CallableTest implements Callable
private String myName = "Unknown";
private Random rand = new Random();
public CallableTest(String name) {
// Wahoo I was given a birth name.
myName = name;
}
public String call() throws Exception {
for(int i = 0; i < 100; i++) {
System.out.println("CallableTest - Name [" + myName + "] Count [" + i + "]");
// Lets snooze the thread for a bit so they get out of sequence.
int value = rand.nextInt(101);
/* Execute Brittany Spears Exception when the value is 42 (The answer to all questions)
if(value == 42) {
String oops = "Ooops I did it again!";
System.out.println(oops);
throw new Exception(oops);
}
*/
Thread.sleep(value * 10);
}
return myName;
}
public String getName() {
return myName;
}
public static void main(String[] args) {
int total = 5;
ExecutorService executor = new ScheduledThreadPoolExecutor(total);
Future
for(int i = 0; i < total; i++) {
futures[i] = executor.submit(new CallableTest("Test [" + i + "]"));
}
/*
// Bah, we really didn't want to execute these threads. So lets see what
// happens below.
try {
for(int i = 0; i < total; i++) {
boolean flag = futures[i].cancel(true);
if(flag) {
System.out.println("Cancelled");
}
}
} catch (Exception e) {
System.out.println("Exception caught [" + e + "]");
}
*/
/*
// Even though we have multiple threads this logic lines the threads
// back up in the order of execution (submitting).
try {
for(int i = 0; i < total; i++) {
String name = futures[i].get();
System.out.println("Thread Complete [" + name + "]");
}
} catch (ExecutionException e) {
System.out.println("ExecutionException caught [" + e + "]");
} catch (CancellationException e) {
System.out.println("CancellationException caught [" + e + "]");
} catch (Exception e) {
System.out.println("Exception caught [" + e + "]");
}
*/
// Lets try to find the first completed thread.
try{
// Need to create a bucket mechanism so we know which thread completed.
// As a thread completes we empty the bucket. When the bucket is empty
// everything is done.
Vector
for(int i = 0; i < total; i++) {
bucketOhThreads.add(futures[i]);
}
while(bucketOhThreads.size() > 0) {
for(int i = 0; i < bucketOhThreads.size(); i++) {
try {
if (((Future)bucketOhThreads.elementAt(i)).isCancelled() ) {
System.out.println("Thread Cancelled - ThreadCount[" + (bucketOhThreads.size() - 1) + "]");
bucketOhThreads.removeElementAt(i);
}
else if (((Future)bucketOhThreads.elementAt(i)).isDone() ) {
Future future = (Future)bucketOhThreads.elementAt(i);
// Doing the above it loses track that the Future objects were
// returning a String. So we have to type cast.
String name = (String)future.get();
System.out.println("Thread Complete - Name[" + name + "] ThreadCount[" + (bucketOhThreads.size() - 1) + "]");
bucketOhThreads.removeElementAt(i);
}
} catch (ExecutionException e) {
System.out.println("ExecutionException caught - [" + e + "] ThreadCount[" + (bucketOhThreads.size() - 1) + "]");
bucketOhThreads.removeElementAt(i);
} catch (CancellationException e) {
System.out.println("CancellationException caught - [" + e + "]. We are checking the isCancelled property. We should NEVER receive this exception here.");
bucketOhThreads.removeElementAt(i);
}
}
// This is were other work should happen. We really shouldn't have
// this thread waiting around for the other threads to execute.
// We really don't want to spin in a tight loop do we, so let's sleep
// for a bit. ;)
Thread.sleep(100);
}
} catch (Exception e) {
System.out.println("Exception caught [" + e + "]");
}
}
}
/*
* ITEMS Not really messed with:
* - ScheduledThreadPoolExecutor
* - Future.get(timeout)
* - I want to know how we get the name (or any data for that matter) from
* the Thread object as well. Right now we have to wait for the thread
* to complete or generate and exception. (lol, I guess that's enough
* info.)
*/
Friday, September 7, 2007
Ok Nerd Moment, Close your eyes if your scared.
Here is a good link describing WebServices within JBOSS. Even shows a VB.NET example.
http://203.2.177.22/tutorial/html/chap9.html
http://203.2.177.22/tutorial/html/chap9.html
Subscribe to:
Posts (Atom)