Spring Session Scoped Bean

I have always hated direct usages of “HttpSession”. You know how tricky it becomes when you want to do advanced things like session replications / caching etc. I have previously used home grown session factory abstraction. But just recently I came across Spring “Session Scoped Bean”. So far, we have only used dependency injection of spring beans which had only 2 scopes – singleton & prototype. But now spring supports custom scopes for beans and one of them is “Session”. It means that this bean is created when a new HttpSession is created and preserved as long as the HttpSession is valid. Spring uses AOP to extract the sessionId from the httprequest and manage the lifecycle of the bean. So instead of putting your object directly into the session, you get to put them in an injected POJO based placeholder. This makes it easy to do the testing and getting rid of HttpSession dependency. So when time comes, you can use solutions like “terracotta” to distribute your session without changing your code! Isn’t that wonderful! Another niche Spring trick!

The details of how to setup the configurations can be found here. I followed the exact steps and it worked flawlessly.

“Enso”—where were you all these days!

Surfing the net is like a hobby/passion/addiction for me, And I mostly surf technology / computer related stuff! I work with other software professionals who are geeks. So anything new, useful comes to my attention early. Yet I am very disappointed to know that Its only today I came to know about Enso! I’v been using it for a few hours now but I am sure its going to be my most useful windows utility forever! It might even become a reason why I prefer Windows over Linux as my development environment [ They don’t have the linux version yet 😦 ]. Just look at the demo and try using it. Using Computer will never be the same again!!!!! I pity myself for not using it earlier 😦

The Productive Programmer

I’v been reading the book “The Productive Programmer“. Being the lazy programmer myself, I always look for more efficient ways of doing things. There are tricks I’v learned from my experience over the years and still learning new things every now and then. But it looks like this book is the summery of all that! Any developer who wants a better way of doing things must have go through this book. How many times have you come across a niche tool and said “Wow! I only wish I knew about this earlier!”. Do you remember first time you used cygwin on Windows! This book is the compilation of all those tools and tricks!

Asterisk-java + Spring

Those who don’t know about Asterisk Java, its a wonderful java library to talk to a asterisk server through AGI & AMI. We are using it heavily to control our Asterisk box from our “main application”. Our main application is a full fledged spring application running on tomcat. At first we were running the asterisk java components (AGIServer , AgiScripts etc) as a normal java application but soon we wanted our AgiScripts to talk to our spring beans Or even better, we want our agi scripts to be Spring beans.

The beauty of the spring and asterisk-java library is that they were made for each other! Asterisk java library was written in wonderful object-oriented way with clear separation on dependencies. So it was very easy to define the AGIServer and all its dependencies as Spring Bean. So We can inject the MappingStrategy with our own implementation of “BeanNameAwareAGIMappingStrategy” which implements ApplicationContextAware looks like this

 protected AgiScript createAgiScriptInstance(String beanName) {
        Object bean = applicationContext.getBean(beanName)
        if(bean == null) {
           throw new IllegalArgumentException(\"No bean with name: [\" + beanName +\"] found. Make sure that you have all beans defined which are there in your fastagi-mapping.properties\");

        if(!(bean instanceof AgiScript)) {
          throw new IllegalArgumentException(\"spring bean : \" + beanName + \" must implement org.asteriskjava.fastagi.AgiScript interface\");

        return (AgiScript) bean;

So now, instead of defining fully qualified classnames in your “fastagi-mapping.properties”, you only give the bean name and our mapping strategy looks it up from the application context. So your plain agi scripts can talk to your dao, services and do all sort of fancy things! Isn’t that wonderful!