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/
Tag: spring-roo
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
Blog created in couple of minutes using Spring Roo and Cloud Foundry
Yesterday I gave a session on Cloud Foundry at SiliconIndia Cloud conference and I showed audience how they can create a blog in couple of minutes. I developed blog using Spring Roo and deployed to Cloud Foundry public cloud using roo cloud foundry add-on. The audience really liked the demo and saw the power of Spring Roo and Cloud Foundry. In case you want to create your blog using Spring Roo and Cloud Foundry . Open the roo shell and fire these commands.
project --topLevelPackage com.xebia.blog --projectName xebiablog mongo setup entity mongo --class ~.domain.Blog field string --fieldName title --class ~.domain.Blog --notNull field string --fieldName body --notNull --sizeMax 200000 field date --type java.util.Date --fieldName publishDate --notNull field boolean --fieldName publish --primitive --notNull field string --fieldName author --notNull entity mongo --class ~.domain.Comment field string --fieldName email --notNull --class ~.domain.Comment field string --fieldName comment --notNull --sizeMax 4000 field set --type ~.domain.Comment --fieldName comments --class ~.domain.Blog field reference --type ~.domain.Blog --fieldName blog --class ~.domain.Comment --notNull repository mongo --interface ~.repository.BlogRepository --entity ~.domain.Blog repository mongo --interface ~.repository.CommentRepository --entity ~.domain.Comment service --interface ~.service.BlogService --entity ~.domain.Blog service --interface ~.service.CommentService --entity ~.domain.Comment web mvc setup web mvc all --package ~.web mongo setup --cloudFoundry true perform package cloud foundry login --email <username> --password <password> cloud foundry deploy --appName xebiablog --path /target/xebiablog-0.1.0.BUILD-SNAPSHOT.war --memory 512 cloud foundry create service --serviceName xebiablog-mongo --serviceType mongodb cloud foundry bind service --appName xebiablog --serviceName xebiablog-mongo cloud foundry start app --appName xebiablog cloud foundry list apps
This blog will use MongoDB as its datastore. If you already have Spring Roo and CloudFoundry addon installed it will take less than couple of minutes to have your blog created and deployed in cloud.
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.
Creating Application using Spring Roo and Deploying on Google App Engine
Spring Roo is an Rapid Application Development tool which helps you in rapidly building spring based enterprise applications in the Java programming language. Google App Engine is a Cloud Computing Technology which lets you run your application on Google’s infrastructure. Using Spring Roo, you can develop applications which can be deployed on Google App Engine. In this tutorial, we will develop a simple application which can run on Google App Engine.
Read more