[jboss-cvs] JBossCache/examples/PojoCache/passivation/src/test/examples ...

Ben Wang bwang at jboss.com
Mon Nov 6 23:26:52 EST 2006


  User: bwang   
  Date: 06/11/06 23:26:52

  Added:       examples/PojoCache/passivation/src/test/examples 
                        StudentMaintTest.java
  Log:
  passivation example.
  
  Revision  Changes    Path
  1.1      date: 2006/11/07 04:26:52;  author: bwang;  state: Exp;JBossCache/examples/PojoCache/passivation/src/test/examples/StudentMaintTest.java
  
  Index: StudentMaintTest.java
  ===================================================================
  package test.examples;
  
  import junit.framework.TestCase;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.factories.XmlConfigurationParser;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.CacheListener;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.CacheSPI;
  import org.jgroups.View;
  import examples.Address;
  import examples.Course;
  //import examples.Person;
  import examples.Student;
  
  import java.util.Map;
  
  /**
   * Driver test to illustrate the PojoCache usage. By using the cache, it will have:
   * <ul>
   * <li>automatic state fail over</li>
   * <li>fine-grained replication</li>
   * <li>preservation of object graph relationship</li>
   * </ul>
   */
  public class StudentMaintTest extends TestCase {
  
     private Student joe_;
     private Student mary_;
     private Course foo_;
     private Course bar_;
     private MyCacheListener listener_;
  
     // cache1 and cache2 are in the same clustering group.
     private PojoCache cache1_;
     private PojoCache cache2_;
  
     protected void setUp() throws Exception {
        cache1_ = createCache("TestCluster");
        listener_ = new MyCacheListener();
        cache1_.getCache().addCacheListener(listener_);
  
        cache2_ = createCache("TestCluster");
        init();
     }
  
     protected void tearDown() throws Exception {
        cache1_.getCache().removeNode(Fqn.fromString("/"));
        cache1_.stop();
        cache2_.stop();
     }
  
     private PojoCache createCache(String name) throws Exception {
        XmlConfigurationParser parser = new XmlConfigurationParser();
        // This is PojoCache passivation configuration. Key is there is only one global region.
        Configuration conf = parser.parseFile("META-INF/pojocache-passivation-service.xml");
        conf.setClusterName(name); // We can set a different cluster group.
        boolean toStart = true;
        PojoCache cache = PojoCacheFactory.createCache(conf, toStart);
        // Or
  //      PojoCache cache = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart);
        return cache;
     }
  
     /**
      * Populate the propagation tree.
      *
      * @throws Exception
      */
     protected void init() throws Exception {
        mary_ = new Student();
        mary_.setName("Mary Smith");
  
        Address address = new Address();
        address.setStreet("456 Oak Drive");
        address.setCity("Pleasantville, CA");
        address.setZip(94555);
  
        mary_.setAddress(address);
  
        joe_ = new Student();
        joe_.setName("Joe Smith");
        joe_.setSchool("Engineering");
  
        // Mary and Joe have the same address
        joe_.setAddress(address);
  
        foo_ = new Course();
        foo_.setTitle("Intro to Foo");
        foo_.setInstructor("Jones");
  
        joe_.addCourse(foo_);
        mary_.addCourse(foo_);
  
        bar_ = new Course();
        bar_.setTitle("Advanced Bar");
        bar_.setInstructor("Woods");
        bar_.setRoom("104 Encina");
  
     }
  
  
     private void sanityCheck() throws InterruptedException
     {
        Thread.sleep(100);
        if(listener_.getActivationCounter() == 0 || listener_.getPassivationCounter() == 0)
        {
           System.err.println("**** Sanity checking for passivation failed. Counters: activation - "
                   +listener_.getActivationCounter()
            + " passivation - " +listener_.getPassivationCounter());
        }
  
        listener_.reset();
     }
  
     public void testPropagation() throws Exception {
  
        // Here we ask the pojo cache to manage mary_ and joe_
        String testData = "/org/jboss/test/data/";
        cache1_.attach(testData +"students/54321", mary_);
        cache1_.attach(testData +"students/65432", joe_);
        cache1_.attach(testData +"courses/101", foo_);
        cache1_.attach(testData +"courses/401", bar_);
  
        // sleep should trigger passivation
        Thread.sleep(9000);
  
        // Output
        printStatus("Initial state for Mary", mary_);
        printStatus("Initial state for Joe", joe_);
  
        // Retrieve the pojos from the Server #2
        Student mary2 = (Student) cache2_.find(testData +"students/54321");
        Student joe2  = (Student) cache2_.find(testData +"students/65432");
        Course  foo2  = (Course) cache2_.find(testData +"courses/101");
  
        System.out.println("---------------------------------------------");
        System.out.println("Modified on Server #1");
         // Change state in one of the items. This will be fine-grained replicated
        foo_.setRoom("101 Alvarez"); // Modified state on cache #2
        printStatus("Course Update: id: 401  room: null->101 Alvarez (retrieved from cache #2)"
                    , foo2);
  
        System.out.println("---------------------------------------------");
        System.out.println("Modified on Server #2");
         // Change state in one of the items. This will be fine-grained replicated
        joe2.addCourse(bar_); // Modified state on cache #2
        printStatus("Student Update: id: 65432  addCourse: Advanced Bar (retrieved from cache #1)"
                    , joe_);
  
        System.out.println("---------------------------------------------");
        System.out.println("Modified on Server #1");
         // Change state in one of the items. This will be fine-grained replicated.
        mary_.setSchool("Engineering");
        printStatus("Student Update: id: 65432  school: null->Engineering (retrieved from cache #2)", mary2);
  
        sanityCheck();
     }
  
     private void printStatus(String msg, Object obj) {
        System.out.println("---------------------------------------------");
        System.out.println(msg);
        System.out.println("---------------------------------------------");
        System.out.println(obj.toString());
     }
  
     public static void main(String[] args) throws Exception {
        StudentMaintTest smTest = new StudentMaintTest();
        smTest.setUp();
        smTest.testPropagation();
        smTest.tearDown();
     }
  
     public class MyCacheListener implements CacheListener
     {
        int activationCounter = 0;
        int passivationCounter = 0;
  
        public int getActivationCounter()
        {
           return activationCounter;
        }
  
        public int getPassivationCounter()
        {
           return passivationCounter;
        }
  
        public void reset()
        {
           activationCounter = 0;
           passivationCounter = 0;
        }
  
        public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
        {
        }
  
        public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, Map data)
        {
           //To change body of implemented methods use File | Settings | File Templates.
        }
  
        public void nodeRemoved(Fqn fqn, boolean pre, boolean isLocal, Map data)
        {
           //To change body of implemented methods use File | Settings | File Templates.
        }
  
        public void nodeVisited(Fqn fqn, boolean pre)
        {
           //To change body of implemented methods use File | Settings | File Templates.
        }
  
        public void nodeEvicted(Fqn fqn, boolean pre, boolean isLocal)
        {
           //To change body of implemented methods use File | Settings | File Templates.
        }
  
        public void nodeLoaded(Fqn fqn, boolean pre, Map data)
        {
           //To change body of implemented methods use File | Settings | File Templates.
        }
  
        public void nodeMoved(Fqn from, Fqn to, boolean pre)
        {
           //To change body of implemented methods use File | Settings | File Templates.
        }
  
        public void nodeActivated(Fqn fqn, boolean pre)
        {
           if(!pre)
           {
              System.out.println("nodeActivated: fqn - " +fqn);
              activationCounter++;
           }
        }
  
        public void nodePassivated(Fqn fqn, boolean pre)
        {
           if(pre)
           {
              System.out.println("nodePassivated: fqn - " +fqn);
              passivationCounter++;
           }
        }
  
        public void cacheStarted(CacheSPI cache)
        {
           //To change body of implemented methods use File | Settings | File Templates.
        }
  
        public void cacheStopped(CacheSPI cache)
        {
           //To change body of implemented methods use File | Settings | File Templates.
        }
  
        public void viewChange(View new_view)  // might be MergeView after merging
        {
           //To change body of implemented methods use File | Settings | File Templates.
        }
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list