I have update the post and put it my company blog http://xebee.xebia.in/2011/12/25/deploying-spring-roo-mysql-applications-on-jboss-as7/
Category: spring
Using MongoDB Replica Set With Spring MongoDB 1.0.0.RC1
The primary means for replication is to ensure data survives single or multiple machine failures. The more replicas you have, the more likely is your data to survive one or more hardware crashes. With three replicas, you can afford to lose two nodes and still serve the data. MongoDB supports two forms of replication, Replica Sets and Master Slave. Replica Sets is the recommended way to do replication in MongoDB and will cover only Replica Sets in this post.
Couple of weeks back I was working in POC where we need to set up MongoDB replication. As I am Spring aficionado I decided to use Spring MongoDB to interact with Replica Set. We used Spring Roo to quickly bootstrap the project. All the project setup, Spring MongoDB setup, JUnit test cases, evern Spring MVC UI was created in minutes thanks to Spring Roo. I am big Spring Roo fan — I just love it. Thanks SpringSource for such an amazing project. Spring Roo uses Spring MongoDB version 1.0.0.M5 which has a bug that it does not support WriteConcern value REPLICAS_SAFE. But with the current release 1.0.0.RC1 that issue has been fixed and now you can use REPLICAS_SAFE. REPLICAS_SAFE is the recommended value for WriteConcern in case of replication. This is a step by step guide from creation of Spring project to working MongoDB replica set.
- Create the project using Spring Roo. If you are not aware of Spring Roo you can read my Spring Roo series. I am using Spring Roo to quickly configure a Spring MongoDB project.
project --topLevelPackage com.xebia.mongodb.replication --projectName mongodb-replication-demo --java 6 mongo setup --databaseName bookshop --host localhost --port 27017 entity mongo --class ~.domain.Book --testAutomatically --identifierType org.bson.types.ObjectId field string --fieldName title --notNull field string --fieldName author --notNull field number --type double --fieldName price --notNull repository mongo --interface ~.repository.BookRepository --entity ~.domain.Book
This will create a Spring maven project, configure MongoDB to work with Spring, create one Collection Book and will add three fields title, author, and price to the collection. All the CRUD operations will carried out using BookRepository.
- Start the MongoDB server using ./mongod and run BookIntegrationTest and make sure all tests pass.
- Setup replica set following the MongoDB documentation http://www.mongodb.org/display/DOCS/Replica+Set+Tutorial.
- Update the applicationContext-mongo.xml as shown below but before add the property mongo.replicaset which will have all nodes.
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:cloud="http://schema.cloudfoundry.org/spring" xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://schema.cloudfoundry.org/spring http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.8.xsd"> <mongo:db-factory dbname="${mongo.database}" id="mongoDbFactory" mongo-ref="mongo"/> <mongo:repositories base-package="com.xebia.mongodb.replication"/> <!-- To translate any MongoExceptions thrown in @Repository annotated classes --> <context:annotation-config/> <bean class="org.springframework.data.mongodb.core.MongoTemplate" id="mongoTemplate"> <constructor-arg ref="mongoDbFactory"/> </bean> <mongo:mongo id="mongo" replica-set="${mongo.replicaset}" write-concern="REPLICA_SAFE"> <mongo:options auto-connect-retry="true"/> </mongo:mongo> </beans>If you run the tests again all the tests will fail and you will see following exception.
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from file [/home/shekhar/dev/workspaces/writing/mongodb-replication-demo/target/classes/META-INF/spring/applicationContext-mongo.xml]; nested exception is java.lang.ArrayIndexOutOfBoundsException: 1 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader. doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
The reason for this exception is because there is a bug in Spring MongoDB 1.0.0.M5 which is not able to parse WriteConcern REPLICA_SAFE value.
- To make it work we have to work with Spring MongoDB latest version 1.0.0.RC1. This is released just 3 days back on 7th December 2011.Update the pom.xml with 1.0.0.RC1.
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.0.0.RC1</version> </dependency>Run the BookIntegrationTest the tests will fail again and see the following exception stacktraces.
java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils .getAnnotation(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;) Ljava/lang/annotation/Annotation; at org.springframework.transaction.annotation.SpringTransactionAnnotationParser .parseTransactionAnnotation(SpringTransactionAnnotationParser.java:38)
- To make it ran you have to use latest Spring version 3.1.0.RC2 in pom.xml
<spring.version>3.1.0.RC2</spring.version>
- Final change you need to make is in applicationContext-mongo.xml. Change the value of write-concern to REPLICAS_SAFE.
<mongo:mongo id="mongo" replica-set="${mongo.replicaset}" write-concern="REPLICAS_SAFE"> <mongo:options auto-connect-retry="true"/> </mongo:mongo> - Run the tests and all the tests will pass.
Spring Roo CompareTo Add-on Released
Over the weekend I created an advanced Spring Roo add-on to provide support for creation of compareTo method. This add-on is on similar lines of Spring Roo own equals add-on which provide implementation of equals and hashcode methods. The add-on is released and available for community to use. The add-on is listed in Spring Roo repository XML http://spring-roo-repository.springsource.org/roobot/roobot.xml . It is built using Spring Roo latest version 1.2.0.RC1. This post will talk about how to install and use the add-on.
Installing the compareTo add-on
Open the operating system command line and type roo. In case you don’t have Spring Roo setup please refer to my IBM DeveloperWork article on Spring Roo. After the roo shell initiates type the following command to install the add-on.
addon install bundle --bundleSymbolicName org.xebia.roo.addon.compareto
If you don’t have automatic pgp trust enable you will see a message on Roo shell saying that you first have to trust my pgp key before you can install the add-on. To trust the key type the following Roo command.
pgp trust --keyId 0x9B68220C
Once it is enable re-run the addon install command and add-on should get installed. To verify that add-on is installed type the osgi ps command and you should compareto add-on listed at the bottom of the command output.
[ 78] [Active ] [ 1] spring-dw-roo-compareto-addon (0.1.1.BUILD)
This add-on exposes two commands compareto setup and compareto and these commands are enabled after you have created entity. This is because before you have created any entity it does not make sense to have compareTo method.
Using the compareTo add-on
Lets first create a simple bookshop application. Type the following command which will create the project, setup database and create a simple book entity.
project --topLevelPackage com.xebia.bookshop --projectName bookshop jpa setup --database HYPERSONIC_IN_MEMORY --provider HIBERNATE entity jpa --class ~.domain.Book --testAutomatically field string --fieldName title --notNull field string --fieldName author --notNull field number --type double --fieldName price --notNull
After you have ran the commands above you can now create compareTo method for book entity.To create the compareTo method type the following commands.
compareto setup compareto add --type ~.domain.Book
The first command will add the required dependencies and second will create the Book_Roo_Compareto.aj ITD which contains the compareTo method.
If you want to exclude some fields in compareTo method you should specify i.e. if you don’t want author and price field to be used in compareTo method you can specify them in excludeFields attribute as shown below.
compareto add --type ~.domain.Book --excludeFields "author price"
Use this add-on and share feedback.
I will be reviewing Spring Roo 1.1 Cookbook
I will be reviewing Spring Roo 1.1 cookbook in next one month. You can download the first chapter of the book free of cost. 
IBM DeveloperWorks Introducing Spring Roo, Part 4: Rapid application development in cloud with Spring Roo and Cloud Foundry
Take the rapid development of Roo a step further by creating applications to work in the cloud with Cloud Foundry, the first open platform as a service project created by VMWare. Learn more about the environment and then deploy an application into Cloud Foundry using the Roo shell. Read about it here http://www.ibm.com/developerworks/opensource/library/os-springroo4/index.html
Introducing Spring Roo, Part 3: Developing Spring Roo add-ons
Spring Roo is a RAD tool that lets you build applications (mainly web) quickly and easily. Under the hood, Spring Roo is based on OSGI add-on architecture, which makes it easy to extend Spring Roo by adding add-ons. Spring Roo provides commands to create add-ons that can be very easily made available to the Spring Roo user community. In this article, we first talk about Spring Roo architecture, talking about how Spring Roo leverages its own add-on architecture to provide different features, then we will create add-ons using the Roo shell and modify them to suit our needs. Please read the third article at IBM DeveloperWorks.
How to exclude a sub package from Spring component scanning?
Suppose you are using Spring component scanning and you don’t want to scan classes in the sub-package you should use <context:exclude-filter> subtag of <context:component-scan> tag as shown below
<context:component-scan base-package="com.test.ws"> <context:exclude-filter type="aspectj" expression="com.test.ws.client.*" /> </context:component-scan>
Here we have defined a aspectj pointcut which will exclude all the classes in the sub-package.
Spring Scoped Proxy Beans – An Alternative to Method Injection
Few months back, I wrote on JavaLobby about how you can use Method Injection in scenarios where bean lifecycles are different i.e. you want to inject a non-singleton bean inside a singleton bean. For those of you who are not aware of Method Injection, it allows you to inject methods instead of objects in your class. Method Injection is useful in scenarios where you need to inject a smaller scope bean in a larger scope bean. For example, you have to inject a prototype bean inside an singleton bean , on each method invocation of Singleton bean. Just defining your bean prototype, does not create new instance each time a singleton bean is called because container creates a singleton bean only once, and thus only sets a prototype bean once. So, it is completely wrong to think that if you make your bean prototype you will get new instance each time prototype bean is called.
To make this problem more concrete, lets consider we have to inject a validator instance each time a RequestProcessor bean is called. I want validator bean to be prototype because Validator has a state like a collection of error messages which will be different for each request.
RequestProcessor class
@Component
public class RequestProcessor implements Processor {
@Autowired(required = true)
private Validator validator;
public Response process(Request request) {
List errorMessages = validator.validate(request);
if (!errorMessages.isEmpty()) {
return null;
}
return null;
}
}
Validator class
@Component
@Scope(value = "prototype")
public class Validator {
private List<String> errorMessages = new ArrayList<String>();
public Validator() {
System.out.println("New Instance");
}
public List<String> validate(Request request) {
errorMessages.add("Validation Failed");
return errorMessages;
}
}
Because I am using Spring annotations, the only thing I need specify in xml is to activate annotation detection and classpath scanning for annotated components.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.shekhar.spring.scoped.proxy"></context:component-scan> </beans>
Now lets write a test to see how many instances of prototype bean gets created when we call the processor 10 times.
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class RequestProcessorTest {
@Autowired
private Processor processor;
@Test
public void testProcess() {
for (int i = 0; i < 10; i++) {
Response response = processor.process(new Request());
assertNull(response);
}
}
}
If you run this test, you will see that only one instance of validator bean gets created as “New Instance” will get printed only once. This is because container creates RequestProcessor bean only once, and thus only sets Validator bean once and reuses that bean on each invocation of process method.
One possible solution for this problem is to use Method Injection which I have already talked about in my JavaLobby article. I didn’t liked method injection approach because it imposes restriction on your class i.e. you have to make your class abstract. In this blog, I am going to talk about the second approach for handling such a problem. The second approach is to use Spring AOP Scoped proxies which injects a new validator instance each time RequestProcessor bean is called. To make it work, the only change you have to do is to specify proxyMode in Validator class.
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
public class Validator {
private List<String> errorMessages = new ArrayList<String>();
public Validator() {
System.out.println("New Instance");
}
public List<String> validate(Request request) {
errorMessages.add("Validation Failed");
return errorMessages;
}
}
There are four possible values for proxyMode attribute.I have used TARGET_CLASS which creates a class based proxy. This mode requires that you have CGLIB jar in your classpath.If you run your test again you will see that 10 instances of Validator class will be created. I found this solution more cleaner as I don’t have to make my class abstract and it makes unit testing of RequestProcessor class easier.
DDD Specification Support in Hades
My Latest Article on Specification support in Hades. Read More
Reduce Boilerplate Code for DAO’s — Hades Introduction
Most web applications will have DAO’s for accessing the database layer. A DAO provides an interface for some type of database or persistence mechanism, providing CRUD and finders operations without exposing any database details. So, in your application you will have different DAO’s for different entities. Most of the time, code that you have written in one DAO will get duplicated in other DAO’s because much of the functionality in DAO’s is same (like CRUD and finder methods).
One of way of avoiding this problem is to have generic DAO and have your domain classes inherit this generic DAO implementation. You can also add finders using Spring AOP; this approach is explained Per Mellqvist in this article. There is a problem with the approach: this boiler plate code becomes part of your application source code and you will have to maintain it. The more code you write, there are more chances of new bugs getting introduced in your application. So, to avoid writing this code in an application, we can use an open source framework called Hades.