PojoCache is an in-memory, transactional, and replicated POJO (plain old Java object) cache system that allows users to operate on a POJO transparently without active user management of either replication or persistency aspects. This tutorial focuses on the usage of the PojoCache API.
For details of configuration, usage and APIs, please refer to the users manual.
PojoCache creation and modification
Replication of POJO fields
Using Collections in PojoCache
Transactions
First download the JBoss Cache 2.x distribution from the download page . You probably want the JBossCache-pojo-2.X.Y.zip distribution. Unzip it, and you will get a directory containing the distribution, such as JBossCache-pojo-2.X.Y . For the sake of this tutorial, I will refer to this as PojoCache .
The configuration files are located under the PojoCache/etc directory. You can modify the behavior of the underlying cache through editing the various configuration files.
log4j.xml . Logging output. You can enable logging, specify log levels or change the name and path to the log file.
META-INF/replSync-service.xml . Cache configuration file used for this tutorial.
pojocache-aop.xml . PojoCache configuration file that contains, amongst other things, the annotation to use on POJOs so that they're aspectised. For more information, please the PojoCache users manual .
The only script needed for this tutorial is the PojoCache/build.xml ant script and the accompanying driver scripts ( build.sh for Unix and build.bat for Windows).
The example POJO classes used for PojoCache demo are: org.jboss.cache.pojo.test.Person and org.jboss.cache.pojo.test.Address . They are located under tests/functional directory.The demo will demonstrate that once a POJO has been attached to the cache, plain get/set POJO methods will be intercepted by the cache.
Here is the snippet of the class definition for Person and Address with the Replicable annotation.
@org.jboss.cache.pojo.annotation.Replicable public class Person { ... public String getName() { return name; } public void setName(String name) { this.name=name; } ... public List<String> getLanguages() { return languages; } public void setLanguages(List<String> languages) { this.languages = languages; } ... public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } ... }
@org.jboss.cache.pojo.annotation.Replicable public class Address { ... public String getStreet() { return street; } public void setStreet(String street) { this.street=street; } ... }
The demo is run by calling the ant script (via the driver) with the run.demo.pojocache target. E.g.,
./build.sh run.demo.pojocache
This will cause a GUI window to appear, giving you a tree view of the cache in the top pane and a BeanShell view of the JVM in the lower pane.
The BeanShell view is preset with the following variables:
The references made available to the BeanShell window point to the same cache instance used by the tree view in the GUI above.
To run the demo as a replicated demo, it is useful to start another command line window and run the ant script again as you did above. Now you will have two cache instances running in two separate GUIs, replicating state to each other.
For this tutorial, start two instance of the demo GUI. In this tutorial, we will:
joe = new Person(); joe.setName("Joe Black"); joe.setAge(31); addr = new Address(); addr.setCity("Sunnyvale"); addr.setStreet("123 Albert Ave"); addr.setZip(94086); joe.setAddress(addr);
cache.attach("pojo/joe", joe);
joe.setAge(41);
joe = cache.find("pojo/joe"); mary = new Person(); mary.setName("Mary White"); mary.setAge(30); mary.setAddress(joe.getAddress());
cache.attach("pojo/mary", mary);
mary.getAddress().setZip(95000);
cache.detach("pojo/joe"); cache.detach("pojo/mary");
joe.setName("Joe White");
For this tutorial, start two instances of the demo GUI. In this tutorial, we will:
joe = new Person(); joe.setName("Joe Black"); lang = new ArrayList(); lang.add("Spanish"); joe.setLanguages(lang);
cache.attach("pojo/joe", joe);
proxyLang = joe.getLanguages(); proxyLang.add("English");
cache.detach("pojo/joe");
proxyLang.add("French");
For this tutorial, start two instances instance of the demo GUI. Repeat the exercises in the previous tutorial, only starting transactions before attaching/detaching nodes or modiying the POJOs. This will depict how replication only occurs on transaction boundaries. Try rolling back a few transactions as well, to see how nothing gets replicated in these cases.