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

advertisement

AddThis Social Bookmark Button

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

Result Output

Finally, when the example is run, the resulting output will printed as follows:

   
Initial state
---------------------------------------------
Japan (Summary : 2000 [ok])
+ Tokyo (Summary : 2000 [ok])
+ + WindSensor1 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1000, state =1040)
+ + | ( name = sensor unit, id = 1001, state =1040)
+ + RainSensor1 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1002, state =1040)
+ + | ( name = sensor unit, id = 1003, state =1040)
+ Yokohama (Summary : 2000 [ok])
+ + WindSensor2 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1000, state =1040)
+ + | ( name = sensor unit, id = 1001, state =1040)
+ + RainSensor2 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1002, state =1040)
+ + | ( name = sensor unit, id = 1003, state =1040)

---------------------------------------------
Modified on Manager #1
StateItem.setState(): id: 1003 state changed \
  from 1040 to 1041
---------------------------------------------
Japan.Tokyo.RainSensor1: id: 1003 state: 1040->1041 \
   (retrieved from Manager #2!)
---------------------------------------------
Japan (Summary : 2004)
+ Tokyo (Summary : 2004)
+ + WindSensor1 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1000, state =1040)
+ + | ( name = sensor unit, id = 1001, state =1040)
+ + RainSensor1 (Summary : 2004)
+ + | ( name = power supply, id = 1002, state =1040)
+ + | ( name = sensor unit, id = 1003, state =1041)
+ Yokohama (Summary : 2000 [ok])
+ + WindSensor2 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1000, state =1040)
+ + | ( name = sensor unit, id = 1001, state =1040)
+ + RainSensor2 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1002, state =1040)
+ + | ( name = sensor unit, id = 1003, state =1040)

---------------------------------------------
Modified on Manager #2
StateItem.setState(): id: 1001 state changed \
  from 1040 to 1041
---------------------------------------------
Japan.Yokohama.WindSensor2: id: 1001 state: 1040->1041 \
   (retrieved from Manager #1!)
---------------------------------------------
Japan (Summary : 2004)
+ Tokyo (Summary : 2004)
+ + WindSensor1 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1000, state =1040)
+ + | ( name = sensor unit, id = 1001, state =1040)
+ + RainSensor1 (Summary : 2004)
+ + | ( name = power supply, id = 1002, state =1040)
+ + | ( name = sensor unit, id = 1003, state =1041)
+ Yokohama (Summary : 2004)
+ + WindSensor2 (Summary : 2004)
+ + | ( name = power supply, id = 1000, state =1040)
+ + | ( name = sensor unit, id = 1001, state =1041)
+ + RainSensor2 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1002, state =1040)
+ + | ( name = sensor unit, id = 1003, state =1040)

---------------------------------------------
Add a new VibrationSensor on Tokyo station
StateItem.setState(): id: 1004 state changed from \
  2000 to 1040
StateItem.setState(): id: 1005 state changed from \
  2000 to 1040
---------------------------------------------
Japan.Tokyo.VibrationSensor1: (retrieved from cache #2)
---------------------------------------------
Japan (Summary : 2004)
+ Tokyo (Summary : 2004)
+ + WindSensor1 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1000, state =1040)
+ + | ( name = sensor unit, id = 1001, state =1040)
+ + RainSensor1 (Summary : 2004)
+ + | ( name = power supply, id = 1002, state =1040)
+ + | ( name = sensor unit, id = 1003, state =1041)
 + + VibrationSensor1 (Summary : 2000 [ok])
   + + | ( name = power supply, id = 1004, state =1040)
   + + | ( name = sensor unit, id = 1005, state =1040)
+ Yokohama (Summary : 2004)
+ + WindSensor2 (Summary : 2004)
+ + | ( name = power supply, id = 1000, state =1040)
+ + | ( name = sensor unit, id = 1001, state =1041)
+ + RainSensor2 (Summary : 2000 [ok])
+ + | ( name = power supply, id = 1002, state =1040)
+ + | ( name = sensor unit, id = 1003, state =1040)

Please note the bold text lines. Basically, we do a POJO operation setState first on manager #1 and then print out the propagation tree from the second manager to verify that the state has been updated, and vice versa. It is worthwhile to repeat that, although not shown in the output for the replication layer traffic, each setState() operation will only trigger a fine-grained field-level replication. In addition, if the call is under a transaction context, then the update will be batched; i.e., replicated only when it is ready to commit. Finally, notice that we have the capability to add a new sensor into the network on the fly. A traditional system would have required some sort of restart mechanism.

If you are interested in running this example yourself, you can download JBoss Cache release 1.2.4. Check under the directory examples/aop/sensor that contains the full source and instructions to run.

Conclusion

In this article we have demonstrated the capability of JBoss Cache acting as a POJO cache by leveraging the TreeCacheAop component. By using the POJO cache functionality, it provides seamless failover capability for POJOs by performing fine-grained replication while preserving object-graph relationship.

Acknowledgment

The author would like to acknowledge Mr. Yusuke Komori of SMG Co. from Japan for kindly contributing the use case in this article.

References

Ben Wang is currently the lead for JBoss Clustering and POJO Cache.


Return to ONJava.com.