FileDocCategorySizeDatePackage
DistributedTxCache.javaAPI DocJBoss 4.2.17588Fri Jul 13 21:02:32 BST 2007org.jboss.aspects.versioned

DistributedTxCache

public class DistributedTxCache extends Object implements org.jboss.ha.framework.interfaces.HAPartition.HAPartitionStateTransfer
This is a LRU cache. The TxCache itself is not transactional but any accesses to objects within the cache ARE transactional.

Fields Summary
protected static Logger
log
protected long
lockTimeout
protected DistributedSynchronizationManager
synchManager
protected DistributedVersionManager
versionManager
protected String
partitionName
protected org.jboss.ha.framework.interfaces.HAPartition
partition
protected String
cacheName
protected LRUCache
cache
protected int
maxSize
Constructors Summary
public DistributedTxCache(int maxSize, long lockTimeout, String cacheName)


         
   
      this(maxSize, lockTimeout, cacheName, "DefaultPartition");
   
public DistributedTxCache(int maxSize, long lockTimeout, String cacheName, String pName)

      this.lockTimeout = lockTimeout;
      this.partitionName = pName;
      this.maxSize = maxSize;
      this.cacheName = "DistributedTxCache/" + cacheName;
   
Methods Summary
public synchronized void_flush()

      cache.clear();
   
public synchronized void_insert(java.lang.Object key, java.lang.Object obj)

      cache.put(key, obj);
   
public synchronized void_remove(java.lang.Object key)

      cache.remove(key);
   
public voidcreate()

      this.partition = findHAPartitionWithName(partitionName);
      //REVISIT: doesn't really buy us anything until JGroups synchronizes
      // initial state correctly
      //partition.subscribeToStateTransferEvents(cacheName, this);
      //REVISIT AGAIN: Actually I talked to Bela about this.  I can change the
      //Clustering framework to do state transfer correctly
      partition.registerRPCHandler(cacheName, this);
      synchManager = new DistributedSynchronizationManager(cacheName, null, partition);
      versionManager = new DistributedVersionManager(lockTimeout, synchManager);
      synchManager.versionManager = versionManager;
      synchManager.create();
   
protected org.jboss.ha.framework.interfaces.HAPartitionfindHAPartitionWithName(java.lang.String name)

      HAPartition result = null;
      MBeanServer server = MBeanServerLocator.locate();
      QueryExp exp = Query.and(
                        Query.eq(
                           Query.classattr(),
                           Query.value(ClusterPartition.class.getName ())
                        ),
                        Query.match(
                           Query.attr("PartitionName"),
                           Query.value(name)
                        )
                      );

      Set mbeans = server.queryMBeans (null, exp);
      if (mbeans != null && mbeans.size () > 0)
      {
         ObjectInstance inst = (ObjectInstance)(mbeans.iterator ().next ());
         ClusterPartitionMBean cp = (ClusterPartitionMBean) MBeanProxyExt.create (
                                                      ClusterPartitionMBean.class, 
                                                      inst.getObjectName (),
                                                      server);
         result = cp.getHAPartition();
      }
      
      return result;
   
public voidflush(java.lang.Object key)

      Object[] args = {};
      try
      {
         partition.callMethodOnCluster(cacheName, "_flush", args, false);
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public synchronized java.lang.Objectget(java.lang.Object key)

      Object obj = cache.get(key);
      if (obj instanceof GUID)
      {
         GUID guid = (GUID)obj;
         obj = synchManager.getObject(guid);
      }
      return obj;
   
public java.io.SerializablegetCurrentState()

      log.trace("getCurrentState called on cache");
      return cache;
   
public voidinsert(java.lang.Object key, java.lang.Object obj)

      try
      {
         obj = versionManager.makeVersioned(obj);
         if (versionManager.isVersioned(obj))
         {
            log.trace("Inserting versioned object");
            obj = VersionManager.getGUID((InstanceAdvised)obj);
         }
         else
         {
            log.trace("Inserting a non-Versioned object");
         }
         Object[] args = {key, obj};
         partition.callMethodOnCluster(cacheName, "_insert", args, false);
      }
      catch (Exception ex)
      {
         ex.printStackTrace();
         throw ex;
      }
   
protected voidpullState()

      Object[] args = {};
      List rsp = partition.callMethodOnCluster(cacheName, "getCurrentState", args, true);
      if (rsp.size() > 0)
      {
         setCurrentState((Serializable)rsp.get(0));
      }
   
public voidremove(java.lang.Object key)

      Object[] args = {key};
      try
      {
         partition.callMethodOnCluster(cacheName, "_remove", args, false);
      }
      catch (Exception ex)
      {
         throw new RuntimeException(ex);
      }
   
public voidsetCurrentState(java.io.Serializable newState)

      log.trace("setCurrentState called on cache");
      synchronized (this)
      {
         this.cache = (LRUCache)newState;
      }
   
public synchronized voidstart()

      synchManager.start();
      pullState();
      if (cache == null) cache = new LRUCache(maxSize);