ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

JBoss Cache as a POJO Cache
Pages: 1, 2, 3, 4, 5, 6

Code Snippet

Below is a code snippet that instantiates a PropagationManager instance and sets the appropriate relationship between the station and the sensor nodes. Finally, we use the TreeCacheAop API putObject() to put the POJO (in this case, the PropagationManager instance) under cache management. After that, any POJO operation will be fine-grain replicated; e.g., a setState() operation will only replicate the corresponding state field (which is an integer).

protected void setUp() throws Exception {
  cache1_ = createCache("TestCluster");
  cache2_ = createCache("TestCluster");

protected void tearDown() throws Exception {

private TreeCacheAop createCache(String name) throws
    Exception {
  // configure the cache through injection
  PropertyConfigurator config = new
  // read in the replSync xml.
  // Here we use synchronous mode replication.
  config.configure(tree, "META-INF/replSync-service.xml");
  // We can set a different cluster group.
  tree.start(); // kick start the cache
  return tree;

* Populate the propagation tree.
protected void initPm() throws Exception {
  pm_ = new PropagationManagerImpl();


  pm_.addNode("Japan", "Tokyo"); // Tokyo station

  // Wind sensor device
  pm_.addNode("Japan.Tokyo", "WindSensor1");
  pm_.addStateItem("Japan.Tokyo.WindSensor1", 1000,
    "power supply", 1040); // power supply
  pm_.addStateItem("Japan.Tokyo.WindSensor1", 1001,
    "sensor unit", 1040); // sensor unit

  // rain sensor device
  pm_.addNode("Japan.Tokyo", "RainSensor1");
  pm_.addStateItem("Japan.Tokyo.RainSensor1", 1002,
    "power supply", 1040); // power supply
  pm_.addStateItem("Japan.Tokyo.RainSensor1", 1003,
    "sensor unit", 1040); // sensor unit

  pm_.addNode("Japan", "Yokohama"); // Yokohama station

  // wind sensor device
  pm_.addNode("Japan.Yokohama", "WindSensor2");
  pm_.addStateItem("Japan.Yokohama.WindSensor2", 1000,
    "power supply", 1040); // power supply
  pm_.addStateItem("Japan.Yokohama.WindSensor2", 1001,
    "sensor unit", 1040); // sensor unit

  // rain sensor device
  pm_.addNode("Japan.Yokohama", "RainSensor2");
  pm_.addStateItem("Japan.Yokohama.RainSensor2", 1002,
    "power supply", 1040); // power supply
  pm_.addStateItem("Japan.Yokohama.RainSensor2", 1003,
    "sensor unit", 1040); // sensor unit

  // summary node for wind sensors in this network
  pm_.createNode("WindSummary", "WindSummary");
    "Japan.Tokyo.WindSensor1"); // association
    "Japan.Yokohama.WindSensor2"); // association

  // summary node for rain sensor in this network
  pm_.createNode("RainSummary", "RainSummary");
    "Japan.Tokyo.RainSensor1"); // association
    "Japan.Yokohama.RainSensor2"); // association

* Main starting point. Called by main.
public void testPropagation() throws Exception {
  // Here we ask the pojo cache to manage pm
  cache1_.putObject("/monitor", pm_);

  // Output
  printStatus("Initial state", pm_);

  // Retrieve the pojo from the Manager #2
  PropagationManager pm2 = (PropagationManager)

  System.out.println("Modified on Manager #1");
  // A state has been changed in one of the item.
  // This will be fine-grained replicated.
  pm_.stateChange("Japan.Tokyo.RainSensor1", 1003, 1041);
  printStatus("Japan.Tokyo.RainSensor1: id: 1003 state:
    1040->1041 (retrieved from Manager #2!)", pm2);

  System.out.println("Modified on Manager #2");
  // A state has been changed in one of the item.
  // This will be fine-grained replicated.
    1001, 1041); // Modified state on cache #2
  printStatus("Japan.Yokohama.WindSensor2: id: 1001 state:
    1040->1041 (retrieved from Manager #1!)", pm1);

    "Add a new VibrationSensor on Tokyo station");
  // Vibration sensor device
  pm_.addNode("Japan.Tokyo", "VibrationSensor1");
  pm_.addStateItem("Japan.Tokyo.VibrationSensor1", 1004,
    "power supply", 1040); // power supply
  pm_.addStateItem("Japan.Tokyo.VibrationSensor1", 1005,
    "sensor unit", 1040); // sensor unit
    (retrieved from cache #2)", pm2);

public static void main(String[] args) throws Exception {
  PropagationReplAopTest pmTest =
    new PropagationReplAopTest();

In the above example snippet, please note the following key points:

  1. The two cache instances are configured through XML injection and started thereafter. They can be run on two separate JVMs (and different physical machines).
  2. A PropagationManager instance is put into cache management via a putObject() API call. After that, only pure POJO operations are needed. For example, we can add additional nodes to the PropagationManager instance and it will replicate accordingly.
  3. We retrieve a fresh instance of PropagationManager on cache #2 via a getObject() call.
  4. Various setState() calls trigger only fine-grained replication to the other cache instance.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow