Chapter 7. PojoCache

Chapter 7. PojoCache

Once you are in the shell, type sourceRelative("aop.bsh"); to execute the shell script. Basically, aop.bsh illustrates the steps to instantiate a cache, configure it, and then create entries under it. Here are the snippets:

import org.jboss.cache.PropertyConfigurator;
         import org.jboss.cache.aop.PojoCache;
         import org.jboss.cache.aop.test.Person;
         import org.jboss.cache.aop.test.Address;
         show(); // verbose mode for bean shell
         PojoCache tree = new PojoCache();
         PropertyConfigurator config = new PropertyConfigurator(); // configure tree cache.
         config.configure(tree, "META-INF/replSync-service.xml");
         Person joe = new Person(); // instantiate a Person object named joe
         joe.setName("Joe Black");
         joe.setAge(31);
         Address addr = new Address(); // instantiate a Address object named addr
         addr.setCity("Sunnyvale");
         addr.setStreet("123 Albert Ave");
         addr.setZip(94086); joe.setAddress(addr); // set the address reference
         tree.startService(); // kick start tree cache
         tree.putObject("/aop/joe", joe); 
	 // add aop sanctioned object (and sub-objects) into cache.
         // since it is aop-sanctioned, use of plain get/set methods will take care
	 // of cache contents automatically.
         joe.setAge(41);
      

Note the API needed to put the object (and its dependent ones) into cache is putObject. Once the second window finishes execution, you should see the first GUI window has been populated with entries of /aop/joe/address. Click on each tree node will display different values associated with that node.

Next step to see AOP in action, you can do plain get/set methods without ever worrying about put it in the cache. For example, you can do in the shell window joe.setAge(20); and see that GUI gets updated with the age field automatically (if not, click away and back will refresh the GUI content). Also to demonstrate the object graph replication, you can modify Joe's address and see the cache will update it automatically. For example, type addr.setCity("San Jose"); in the interactive shell, you should see in the GUI that the address got modified.

Finally, PojoCache also supports get/set with parameter type of Collection classes (i.e., List, Map, and Set). For example, type the following in the shell command line:

ArrayList lang = new ArrayList();
         lang.add("Ensligh");
         lang.add("Mandarin");
         joe.setLanguages(lang);