Creating Application using Spring Roo and Deploying on Google App Engine

DZone 's Guide to

Creating Application using Spring Roo and Deploying on Google App Engine

· Java Zone ·
Free Resource
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.

Roo configures and manages your application using the Roo Shell. Roo Shell can be launched as a stand-alone, command-line tool, or as a view pane in the SpringSource Tool Suite IDE.

Before we can start using Roo shell, we need to download and install all pre-requites.

  1. Download and install SpringSource Tool Suite 2.3.3. M2. Spring Roo 1.1.0.M2 comes bundled with STS. While installing STS, installer asks for the location where STS should be installed. In that directory, it will created a folder with name "roo-%release_number%" which will contain roo stuff. Add %spring_roo%/roo-1.1.0.M2/bin in your path so that when you can fire roo commands from command line.
  2. Start STS and go to the Dashboard (Help->Dashboard)
  3. Click on the Extensions tab
  4. Install the "Google Plugin for Eclipse" and the "DataNucleus Plugin".
  5. Restart STS when prompted.

After installing all the above we can start building application.

ConferenceRegistration.Roo Application
Conference Registration is a simple application where speaker can register themselves and create a session they want to talk about. So, we will be having two entities Speaker and Presentation. Follow the instructions to create the application:
  1. Open your operating system command-line shell
  2. Create a directory named conference-registration
  3. Go to the conference-registration directory in your command-line shell
  4. Fire roo command. You will see a roo shell as shown below. Hint command gives you the next actions you can take to manage your application.

  5. Type the hint command and press enter.  Roo will tell you that first you need to create a project and for creating a project you should type 'project' and then hit TAB. hint command is very useful as you don't have to cram all the commands it will always give you the next logical steps that you can take at that point.

  6. Roo hint command told us that we have to create the project so type the project command as shown below
    project --topLevelPackage com.shekhar.conference.registration --java 6
    This command created a new maven project with top-level package name as com.shekhar.conference.registration and created directories for storing source code and other resource files. In this command, we also specified that we are using Java  version 6.
  7. Once you have created the project, type in the hint command again, roo will tell you that now you have to set up the persistence. Type the following command
    persistence setup --provider DATANUCLEUS --database GOOGLE_APP_ENGINE --applicationId roo-gae
    This command set up all the things required for the persistence. It creates persistence.xml, adds all the dependencies required for persistence in pom.xml. We have chosen provider as DATANUCLEUS and database as GOOGLE_APP_ENGINE because we are developing our application for Google App Engine and it uses its own datastore.  applicationId is also required when we deploy our application to Google App Engine. Now our persistence set up is completed.
  8. Type the hint command again, roo will tell you that you have to create entities now. So, we need to create our entities Speaker and Presentation. To create Speaker entity, we will type the following commands
    entity --class ~.domain.Speaker --testAutomatically
    field string --fieldName fullName --notNull
    field string --fieldName email --notNull --regexp ^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
    field string --fieldName city
    field date --fieldName birthDate --type java.util.Date --notNull
    field string --fieldName bio
    The above six lines created an entity named Session with different fields. In this we have used notNull constraint, email regex validation, date field. Spring Roo on app engine does not support enum and references yet which means that you can't define one-one or one-to-many relationships between entities yet. These capabilities are supported on Spring MVC applications but Spring MVC applications can't be deployed on app engine as if now. Spring Roo jira has these issues. They will be fixed in future releases(Hope So :) ).
  9. Next create the second entity of our application Presentation. To create Presentation entity type the following commands on roo shell
    entity --class ~.domain.Presentation --testAutomatically
    field string --fieldName title --notNull
    field string --fieldName description --notNull
    field string --fieldName speaker --notNull
    The above four lines created a JPA entity called Presentation, located in the domain sub-package, and added three fields -- title,description and speaker. As you can see, speaker is added as a String (just enter the full name). Spring Roo on Google App Engine still does not support references.
  10. Now that we have created our entities, we have to create the face of our application i.e. User Interface. Currently, only GWT created UI runs on app engine. So, we will create GWT user interface. To do that type
    gwt setup
    This command will add the GWT controller as well as all the UI required stuff. This command may take couple of minutes if you don't if you don't have those dependencies in your maven repository.
  11. Next you can configure the log4j to Debug level using the following command
    logging setup --level DEBUG
  12. Quit the roo shell
  13. You can easily run your application locally if you have maven installed on your system, simply type "mvn gwt:run" at your command line shell while you are in the same directory in which you created the project. This will launch the GWT development mode and you can test your application. Please note that applications does not run in Google Chrome browser when you run from your development environment. So, better run it in firefox.
  14. To deploy your application to Google App Engine just type
    mvn gwt:compile gae:deploy
    It will ask you app engine credentials(emailid and password).

I hope this tutorial will help you in building applications which can be deployed on Google App Engine using Spring Roo. You can test the application @ http://roo-gae.appspot.com/

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}