Misfiring Neurons Just another geek with a blog

16Mar/06Off

Load balancing with Spring AOP

As we all know, AOP lets you insert arbitrary code at pre-defined program boundaries - like just before method calls for example. One cool side-effect is that you can decide to not make the call at all, or to call a different method instead. Spring's AOP framework recognizes that not all calls will necessarily route to the same target object, and provides the TargetSource interface which can be used in conjunction with dynamic proxies to supply objects on demand, as calls happen. The following is a rather trivial round-robin load balancing that demonstrates how we can distribute the load over any number of Java objects transparently to callers and callees.

Download: Source code including unit tests and examples.

Some ideas for improvement:

  • ability to integrate with Commons Pool (for non-reentrant objects) in place of the simple list currently used (extend AbstractPoolingTargetSource?)
  • add an interceptor to detect a configurable list of exceptions and automatically remove bad objects from pool
  • ability to re-create beans from a prototype definition in the bean factory as needed (similar to the existing PrototypeTargetSource)
Filed under: Development Comments Off
Comments (1) Trackbacks (0)
  1. hi,your blog is google ! I like it very much! I’m agreeable to your point of view! I hope to make feiends with you ! please remember me .my name is 打折机票 of chinese. I come from hongkong.

Trackbacks are disabled.