Friday, September 16, 2011

Spring MVC: Integrating MySQL, MongoDB, RabbitMQ, and AJAX - Part 4

Review

In the previous four sections, we've managed to create the core Event management system, integrate messaging via RabbitMQ, and add error persistence via MongoDB. We've also explored DataTables and jQgrid for presenting tabular data. In this section, the final section, we will build and deploy our project.

Where am I?

Table of Contents

  1. Event Management
  2. Messaging support
  3. Error persistence
  4. Build and deploy

Build and deploy

Cloud Foundry Deployment

Before we build and deploy the project, it is worth visiting first the live application as deployed in the cloud. To access the live app, visit the following url:

http://spring-mysql-mongo-rabbit.cloudfoundry.com

When you run the application for the first time, you might encounter some unexpected results that's because there's a well-known bug when using the JSTL tag c:url. The session id is attached on all URLs! Therefore, the resources don't display as expected. Refreshing the page should quickly resolve this issue. There are workarounds but I don't want to distract my readers from this issue. This is discussed and documented at stackoverflow.com (read it here).

If you're using SpringSource Tool Suite (STS), you can deploy your own app to Cloud Foundry for free! If you need instructions on how to deploy to Cloud Foundry, please check the following resources:

What is STS?
SpringSource Tool Suite™ (STS) provides the best Eclipse-powered development environment for building Spring-powered enterprise applications. STS supplies tools for all of the latest enterprise Java, Spring, Groovy and Grails based technologies as well as the most advanced tooling available for enterprise OSGi development. Source: http://www.springsource.com/developer/sts

What is Cloud Foundry?
Cloud Foundry is the open platform as a service project initiated by VMware. It can support multiple frameworks, multiple cloud providers, and multiple application services all on a cloud scale platform. Source: http://www.cloudfoundry.com/

Localhost Deployment

Before you can deploy the application locally, you need to ensure that the following services are running:
  • MySQL
  • MongoDB
  • RabbitMQ

If you need help on how to download install these services, please visit the FAQs section (at the beginning of this guide).

Create the Database

If all required services are running, go to MySQL and create a new database eventdb. For MongoDB and RabbitMQ, no further configuration is needed because their "databases" will be created during the application's startup. Here's a snippet of the application's db properties:

spring.properties


Run Maven, Deploy to Tomcat

After creating the MySQL database, we can now build the application. Because the application is a Maven project, we can easily build and deploy the application, either to Tomcat or Jetty server.

To build and deploy the application to Tomcat, run the following Maven command: mvn tomcat:run

This will build the project and deploy it in an embedded Tomcat server. To access the application's main page, open your browser and enter the following URL: http://localhost:8080/spring-jpa-mongo-rabbit/. This assumes Tomcat is running under port 8080, and you're using the latest Tomcat Maven plugin:



If the plugin version from org.apache.tomcat.maven will not work (I actually had difficulty using that version), I suggest using the org.codehaus.mojo version:



If successful, you should see the following output in the terminal:
[INFO] Running war on http://localhost:8080/spring-mysql-mongo-rabbit
[INFO] Using existing Tomcat server configuration at /home/Desktop/downloads/spring-mysql-mongo-rabbit/target/tomcat
09 16, 11 10:19:14 PM org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
09 16, 11 10:19:14 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
09 16, 11 10:19:14 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
09 16, 11 10:19:18 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
09 16, 11 10:19:18 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080

For details, see the following resources:
  • http://tomcat.apache.org/maven-plugin-2.0-SNAPSHOT/tomcat-maven-plugin/index.html (new)
  • http://mojo.codehaus.org/tomcat-maven-plugin/index.html (old)

Run Maven, Deploy to Jetty

If you prefer Jetty instead, run the following Maven command: mvn jetty:run

This will build the project and deploy it in an embedded Jetty server. To access the application's main page, open your browser and enter the following URL: http://localhost:8080. This assumes Jetty is running under port 8080, and you're using Jetty 7 Maven plugin:



If successful, you should see the following output in the terminal:
2011-09-16 22:20:48.950:INFO:/:Initializing Spring root WebApplicationContext
2011-09-16 22:20:52.688:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/home/Desktop/downloads/spring-mysql-mongo-rabbit/src/main/webapp/},file:/home/Desktop/downloads/spring-mysql-mongo-rabbit/src/main/webapp/
2011-09-16 22:20:52.739:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080 STARTING
[INFO] Started Jetty Server

For details, see the following resources:
  • http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin (new)
  • http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin (old)

Maven Caveats

When building our project, you might get exceptions pertaining to missing hibernatePersistenceUnit. This is used by the Spring Data JPA project and located under the src/main/java/META-INF folder:


By default, Maven will not copy this file when building the project. You have to manually instruct Maven to do such. There are various Maven plugins for copying external resources. Among these Maven plugins, I found the Maven-copy-plugin the best. For more info about this plugin, check out http://evgeny-goldin.com/wiki/Maven-copy-plugin

Here's the plugin declaration:


Conclusion

That's it. We have successfully integrated MySQL, MongoDB, and RabbitMQ in a single Spring MVC application. With Spring Data we have greatly reduced the data access layer to simple interfaces, and with Spring AMQP, we are able to integrate RabbitMQ without any difficulty. We've also explored various jQuery plugins for presenting tabular data.

Download the project
You can download the source code as a Maven project at GitHub at https://github.com/krams915/spring-mysql-mongo-rabbit-integration

Click the Downloads button at the upper-right corner to begin downloading. Or you can fork the source and start hacking with it.

If you need IDE support for exploring Git repos, use SpringSource Tool Suite (STS).

If you want to learn more about Spring and integration with various technologies, please visit the Tutorials section.
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring MVC: Integrating MySQL, MongoDB, RabbitMQ, and AJAX - Part 4 ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

5 comments:

  1. 2012-08-26 01:02:41.711::WARN: Nested in org.springframework.beans.factory.Bean
    CreationException: Error creating bean with name 'userController': Injection of
    autowired dependencies failed; nested exception is org.springframework.beans.fac
    tory.BeanCreationException: Could not autowire field: private org.krams.reposito
    ry.UserRepository org.krams.controller.UserController.repository; nested excepti
    on is org.springframework.beans.factory.BeanCreationException: Error creating be
    an with name 'userRepository': FactoryBean threw exception on object creation; n
    ested exception is java.lang.NoSuchMethodError: org.springframework.data.reposit
    ory.core.RepositoryMetadata.getDomainClass()Ljava/lang/Class;:
    java.lang.NoSuchMethodError: org.springframework.data.repository.core.Repository
    Metadata.getDomainClass()Ljava/lang/Class;
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.
    getTargetRepository(JpaRepositoryFactory.java:87)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.
    getTargetRepository(JpaRepositoryFactory.java:70)

    ReplyDelete
  2. Hi Mark, first at all, let me tell you that you did a fantastic job, however i have to say that there are some problems when i try to deploy the war on tomcat8, minor problems but there exists. for example, the tables are not created when the war is deploying. I can see that persistence.xml is located in src/main/java/META-INF and hibernate.hbm2dll.auto is set to validate, if i change the value to create and recreate the war with mvm clean install, the table Event is not created yet, so i try to create the table myself using MAMP and changing again the hibernate.hbm2dll.auto to validate. The table is like this:
    id bigint(20) pk autoincrement
    name varchar(50) not null false
    description varchar(100) not null true
    date date not null false
    participants tinyint(3) unsigned not null false.

    It is weird isn't?

    the error is [ERROR] [localhost-startStop-1 01:00:01] (SchemaValidator.java:validate:135) could not get database metadata
    java.sql.SQLException: Connections could not be acquired from the underlying database!
    ....

    Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

    The communication with rabbitmq and mongodb have no problems, but when i try to create an event, that error occurs. I will check it out, thanks anyway

    ReplyDelete
  3. well, after some updates of pom.xml with latest versions, now i have a war deployed successfully. I have forked the project with my changes, with your permission, mr Krams, , if anyone want to download it, https://github.com/alonsoir/spring-mysql-mongo-rabbit-integration

    Thank you, it was fun

    ReplyDelete
  4. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

    ReplyDelete