Spring custom annotations using context:component-scan

Scan Spring custom annotations using context:component scan

Everyone seems to think annotations are cool and want to use them now days. A lot has been written about the pros and cons of using annotations and personally I think they should be used with caution. Mixing annotation domains to a point of total confusion, spreading configuration related meta-data across large code bases to a point where refactoring becomes very hard are only some of the risks. Nevertheless, annotations are cool.

I wanted to add annotation functionality to some “aspect” of our code base. An aspect being an interface Foo and multiple implementations of Foo in various artifacts (e.g., FooA from project A, FooB from project B and so on).

Read More »

Elasticsearch with Spring

ElasticSearch Elasticsearch with Spring

Elasticsearch is a good abstraction around Lucene Search Engine and an alternative to Solr. ElasticSearch provides out of the box index distribution along with a decent JSON RESTful interface backed by a Java/Groovy API. ElasticSearch also elaborate list of modules for a variety of integrations.

I wanted to integrate ElasticSearch in my Spring-based application and a factory/configuration abstraction came to mind.

A an embedded node within a cluster can be instantiated and used to execute requests against the distributed index. It deserves its own factory bean since it has lifecycle management operations and configuration associated with it. Note that configLocation can be provided along with local map of settings, if neither are provided elasticsearch will looks for its configuration files in the working directory or a “config” subdirectory. The local property can also be set (overridable via system property for unit tests).

Read More »

Leader Election with Zookeeper

Zoo Leader Election with Zookeeper

Recently I had to implement an active-passive redundancy of a singleton service in our production environment where the general rule is always have “more than one of anything”. The main motivation is to alleviate the need to manually monitor and manage these services, whose presence is crucial to the overall health of the site.

This means that we sometime have a service installed on several machines for redundancy, but only one of the is active at any given moment. If the active services goes down for some reason, another service rises to do its work. Turns out this is actually called leader election. One of the most prominent open source implementation facilitating the process of leader election is Zookeeper.

Originally developed by Yahoo reasearch, Zookeepr is a service providing reliable distributed coordination. It is highly concurrent, very fast and suitable mainly for read-heavy access patterns. Reads can be done against any node of a Zookeeper cluster while writes a quorum-based. To reach a quorum, Zookeeper utilizes an atomic broadcast protocol.

If you wish to run your data center the democratic way, where important decisions are made in coordination with other stakeholders, Zookeeper certainly helps

Creating a Facebook app with Google App Engine and Google Web Toolkit

social Creating a Facebook app with Google App Engine and Google Web Toolkit
One of the challenges with writing a presumably successful Facebook application is taking care of scale. with an ever growing user base and the high viral growth potential brought by this social platform you could be looking at a very high traffic if your application is successful. It is wise to plan ahead. Integrating a service like Google App Engine or Amazon AWS could do the trick. Especially for small and medium enterprises which can find the cost model beneficial for their first baby steps.

This post is not going to dwell on the details of creating an App Engine account or a Facebook application. Those are described very well by their providers and across the web.

The example is an application called “famousity”. It authenticates a Facebook user then looks through his friends and determines how famous they are by executing a Google search on their exact name and comparing the hit count. Not very clever but will do in order to get a first feel of these tools.

How to quickly set-up a Facebook Application using Google AppEngine, Google Web Toolkit and Spring Source STS version of Eclipse with the Google Plugin

Quartz and Spring Integration

clock Quartz and Spring Integration
Quartz is an excellent, open-source scheduler which provides many enterprise features such as job persistence to a variety of job store implementations (e.g., RAM, JDBC, etc.), transactions and clustering. Spring offers good integration for Quartz and provides some nice abstractions for using it within the IoC container such as MethodInvokingJobDetailFactoryBean which allows you to use any old bean/method as a JobDetail.

Quartz and Spring Integration using ObjectFactoryCreatingFactoryBean with JobDetail and Trigger

TestNG with Cobertura

eclipse TestNG with Cobertura
For most cases I prefer JUnit over TestNG, the fact that it the de-facto standard for unit testing make integration with eclipse, CI servers and build systems easier. However, TestNG has its merits, it is more flexible than JUnit, grouping is highly configurable and the DataProvider feature is also good.

Cobertura is one of the key static analysis tool every developer should work with. I’ve seen numerous project increase their code coverage using this tool and which in turn can and will reduce QA cycles and overall bug counts any given system.

Cobertura is one of the key static analysis tool every developer should work with. I've seen numerous project increase their code coverage using this tool and which in turn can and will reduce QA cycles and overall bug counts any given syste

Spring prototype scoped beans and dependency injection

toy car Spring prototype scoped beans and dependency injection
Within the typical Spring container most of the beans with dependency on other beans end up being stateless singleton beans. However every once in a while you want a stateful prototype beans with dependencies. Read More »

Spring Application Context Hierarchy and ContextSingletonBeanFactoryLocator

hierarchy Spring Application Context Hierarchy and ContextSingletonBeanFactoryLocator
Large scale development environments are typically composed of multiple application component stacks each of which can be represented as a directed graph of dependencies between components.

How to design Spring ApplicationContext in a hierarchy using ContextSingletonBeanFactoryLocator

List of my favorite eclipse plugins

connect List of my favorite eclipse plugins
Can anyone remember the days when jBuilder was the king of J2EE IDEs? can you imagine what J2EE development look like without Eclipse?

However, eclipse truely reveals its power when you add healthy dosage of plugins to the mix. Everyone has their list, here are my favorites:

The most useful eclipse plugins to use in a J2EE development environment

Pattern based resource matching with Spring’s PathMatchingResourcePatternResolver

puzzle Pattern based resource matching with Springs PathMatchingResourcePatternResolver
Spring has a very good resource abstraction mechanism allowing resources to be defined in the application context in a simple fashion, automatic assignment to class members based on type, etc. In addition Spring provides an Ant-like pattern for locating resources, this comes into play in the resource location definition of an application context (when defined in web.xml, beanRefContext.xml etc.):
How to use Spring's PathMatchingResourcePatternResolver to resolve classes in the classpath using ANTLR style patterns