2012-11-03

Spring Data MongoDB on Cloud Foundry tutorial Part 1

Hi, in this tutorial I will show you how to develop a Spring powered MongoDB project, with Gradle as dependency management, and how to deploy it on Cloud Foundry. The app will be a web app deployed on Tomcat.

Cloud Foundry allows you to run many instances of your application, add and remove services, when needed, and so on. You may want to read some info on getting started.

Register on Cloud Foundry

The first step is to register on Cloud Foundry. You will either manage your cloud app using a command line VMC tool, or using Cloud Foundry extension for Spring Tool Suite/Eclipse. I chose to use the IDE independent VMC.

The Spring Data MongoDB app

The Spring app I am using is available to download from my GitHub (specifically, the version for this article is tagged as BlogPost1 and you can also get it from the tags view). Below I will show you some of the steps you need to make to get your Spring MongoDB app up and running - locally for now. More information is available from the Cloud Foundry Spring Application Development page.

Things to remember:

  • Cloud Foundry technology support is limited, so you have to figure out beforehand whether your stack will even work.
  • At this moment, Java 7 is not supported on Cloud Foundry, so you need to downgrade your project to Java 6.
  • Similarly, only certain versions of Spring and Spring Data MongoDB are supported.
  • Only Tomcat 6 is supported at the moment. Locally I'm developing against Tomcat 7, but due to Cloud Foundry limitations it has to work on Tomcat 6 as well (no problems with that).
  • Servlet Spec 3.0 is not supported - back to 2.5.
First, the build.gradle file with dependencies. Gradle dependency resolution strategy is different from that of Maven, but if you prefer Maven, the dependencies should be similar enough.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

group = 'me.m1key.audiolicious'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = 1.6
targetCompatibility = 1.6

repositories {
   mavenLocal()
   mavenCentral()
   mavenRepo name: "spring-test-mvc", url: 'http://repo.springsource.org/libs-milestone/'
   mavenRepo url:'http://maven.springframework.org/milestone/'
}

project.ext.finalName = 'audiolicious-cloud.war'

war { archiveName = finalName }
task deployment(type: Copy) {
   from('build/libs/' + finalName)
   into("$System.env.TOMCAT_HOME/webapps/")
}

project.ext.springVersion = '3.1.2.RELEASE'

dependencies {
   compile group: 'org.springframework', name: 'spring-beans', version: springVersion, force: true
   compile group: 'org.springframework', name: 'spring-context', version: springVersion, force: true
   compile group: 'org.springframework', name: 'spring-core', version: springVersion, force: true
   compile group: 'org.springframework', name: 'spring-webmvc', version: springVersion, force: true

   compile group: 'org.springframework.data', name: 'spring-data-mongodb', version: '1.0.4.RELEASE'
   compile group: 'org.mongodb', name: 'mongo-java-driver', version: '2.9.1'

   compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.6'
   compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.6'
   compile group: 'org.apache.velocity', name: 'velocity', version: '1.7'

   compile group: 'org.cloudfoundry', name: 'cloudfoundry-runtime', version: '0.8.2'

   testCompile group: 'org.springframework', name: 'spring-test', version: springVersion
   testCompile group: 'junit', name: 'junit', version: '4.+'
   testCompile 'org.hamcrest:hamcrest-all:1.3'
   testCompile 'javax.servlet:servlet-api:2.5'
   testCompile 'org.springframework:spring-test-mvc:1.0.0.M2'
}

configurations.all {
   resolutionStrategy {
       force group: 'org.springframework', name: 'spring-aop', version: springVersion
       force group: 'org.springframework', name: 'spring-expression', version: springVersion
       force group: 'org.springframework', name: 'spring-tx', version: springVersion
   }
}
Meanwhile, in your src/main/webapp/WEB-INF/web.xml...
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   id="AudioliciousCloud" version="2.5" metadata-complete="true">
   <display-name>Spring MVC tutorial</display-name>

   <servlet>
       <servlet-name>audiolicious</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:audiolicious-servlet.xml</param-value>
       </init-param>
   </servlet>

   <servlet-mapping>
       <servlet-name>audiolicious</servlet-name>
       <url-pattern>*.go</url-pattern>
   </servlet-mapping>
</web-app>
And the Spring servlet file (src/main/resources/audiolicious-servlet.xml).
<?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:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
 http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

   <!-- Enabling Spring beans auto-discovery -->
   <context:component-scan base-package="me.m1key.audiolicious.cloud" />

   <!-- Enabling Spring MVC configuration through annotations -->
   <mvc:annotation-driven />

   <bean id="velocityConfig"
       class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
       <property name="resourceLoaderPath">
           <value>/</value>
       </property>
   </bean>

   <!-- Defining which view resolver to use -->
   <bean id="viewResolver"
       class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
       <property name="prefix">
           <value>/velocity/</value>
       </property>
       <property name="suffix">
           <value>.vm</value>
       </property>
   </bean>

   <mongo:db-factory id="mongoDbFactory" dbname="adlcs_cloud"
       host="127.0.0.1" port="27017" />

   <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
       <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
   </bean>

   <mongo:repositories base-package="me.m1key.audiolicious.cloud.repositories" />
</beans>
What's important here is MongoDB configuration. If you configure it this way, it will allow Cloud Foundry to inject its own values for host name and port, so that it also works on the cloud.

MongoDB repository

One of the nice features of Spring Data MongoDB is that you can (just like in Grails) ask it to create simple repositories for you - you just define interfaces, Spring will provide the implementation.
package me.m1key.audiolicious.cloud.repositories;
import me.m1key.audiolicious.cloud.entities.Song;
import org.springframework.data.repository.CrudRepository;

public interface SongRepository extends CrudRepository<Song, Long> {
}
This gives me a CRUD repo that I don't have to implement myself. You can add methods to this interface, such as findByLastName, and Spring will know how to implement them. For more information on this, see the repositories reference. You can extend this behaviour by implementing your own, more sophisticated methods.

Entity. Notice no annotations:

package me.m1key.audiolicious.cloud.entities;

public class Song {

    private String name;
    private String albumName;
    private String artistName;
    private String songKey;

    public Song(String name, String albumName, String artistName, String songKey) {
        super();
        this.name = name;
        this.albumName = albumName;
        this.artistName = artistName;
        this.songKey = songKey;
    }

    public String getName() {
        return name;
    }
    // Omitted for brevity.
}

In part 2 of this tutorial I will show you how to deploy this on Cloud Foundry.

PS. Tomcat debugging

This is the stuff I added in my catalina.sh (this is Linux syntax then) file to get remote debugging from my IDE to work.
JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n $JAVA_OPTS"
JPDA_TRANSPORT=dt_socket
JPDA_ADDRESS=8000

23 comments:

  1. Very nice blog, Thanks for sharing grate article.
    You are providing wonderful information, it is very useful to us.
    Keep posting like this informative articles.
    Thank you.

    From: Field Engineer
    Cloud Architect Certification

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. We provide 24/7 customer service to answer your questions and comments efficiently, polite to make sure you always receive vegus168 แทงบอลออนไลน์ from our friendly staff.

    ReplyDelete
  4. Vegus168 คาสิโนออนไลน์ ทางเข้า แทงบอล แทงหวย สมัครสมาชิก โปรโมชั่น เครดิตฟรี 2,000 บาท สมัครง่าย

    ReplyDelete

  5. Every weekend i used to pay a quick visit this web site,
    because i want enjoyment, for the reason that this this
    web page conations really nice funny data too.

    Feel free to visit my blog - 휴게텔
    (jk)

    ReplyDelete
  6. https://namnak.com/mobile-repairs.p80051part.
    Participate in theoretical and practical tests
    In order to be certified in the field of mobile repairs, you need to take theoretical and practical tests. In these tests, everything you have already learned will help you. You need to get the desired score from these exams in order to be known as a mobile repairman and be able to work in the market.

    ReplyDelete
  7. https://www.titrebartar.com/fa/news/172685/meet-the-best-specialists-in-vision-problems-in-iran In general, an ophthalmologist, to treat problems such as; Obstructor of lacrimal ducts, corneal opacity, dry eye, Retinal decolan, Eye refractive disorder (two nose and nearest nose and camera), high eyelid loss, blur and visual loss, laziness, shingle, eye disease, eye inflammation, eye inflammation, eye inflammation, Rupture and cornea ulcers, increase in intraocular pressure, eye cancer, internal inflammation of the eyes, cataract water, eye infections, pearliness, eyelashes, black water, and ... with prescribing glasses, medicine or surgery.

    ReplyDelete
  8. After the events that happened to Bitcoin in 2020, the digital currency market boomed among the people. This event starts the market bullish every four years. However, this year the situation was a little different. The uptrend that the digital currency https://www.golchinonline.ir/index.php/newposts/news/6344-%D8%A8%D8%B1%D8%B1%D8%B3%DB%8C-%D8%A7%D8%B1%D8%B2-%D8%AF%DB%8C%D8%AC%DB%8C%D8%AA%D8%A7%D9%84-%D8%A7%D9%84%D9%88%D9%86.html market experienced this time was different from the uptrend that everyone experienced.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Generally, programming assignments are charged differently from other assignments, but the charging procedure is as usual from the programming programming assignment help services. If you ask for the average price to get a programming assignment done, then it can vary between $100-180, which is still high enough for a student. But, to get the quality we are looking for, this is the amount that we will need.

    ReplyDelete
  11. Exceptional post however , I was wanting to know if you could write a litte more on this topic? I’d be very thankful if you could elaborate a little bit further. Thanks dqfanfeedback survey

    ReplyDelete
  12. I was reading some of your articles on this website and I conceive this web site is very instructive! Retain putting up. 토토사이트

    ReplyDelete
  13. I am pleased that I observed this site, exactly the right information that I was searching for. 파워볼

    ReplyDelete
  14. The best activator for windows 10 Everyone is very excited about all the new functions of the Windows 10 system but if you want to get the full benefit of updating, you will need to activate the software to see and use all the features. Windows will keep reminding you to activate the software through a popup so it’s best to do it sooner rather than later to avoid getting irritated with the popup.

    ReplyDelete
  15. The best windows 10 activator free download for all version for pc With Windows 10 Pro 2022 Torrent In addition, Microsoft also launched the server versions of the windows for the high performance of the servers. You can easily use them in making a complete network of your company all over the world. If your one office is in Pakistan and the other office is in America, don’t worry you can still make a network. The server versions of the windows help you out in this problem by giving the best solutions. In addition, they have several modules installed in your server windows that you can get free sectary with the network you installed. But you have to pay for the windows you are using and if you don’t want to pay the company and want to use it for free.

    ReplyDelete
  16. Atlantic System is the top point-of-sale system for liquor stores. Atlanticsystem is the most often used POS software for liquor stores.

    ReplyDelete
  17. Merci d'avoir partagé. J'ai trouvé beaucoup d'informations qui attirent l'attention ici. un post extrêmement sensé, 먹튀검증 extrêmement appréciable et plein d'espoir que vous puissiez simplement écrire d'autres posts comme celui-ci!

    ReplyDelete
  18. It is a great pleasure to browse your article. It's beautiful and great work. Please keep it up with more updates. Thanks so much for sharing. adsu-cut-off-mark

    ReplyDelete
  19. thanks for your marvelous posting! I really enjoyed reading it, you happen to be a great author.

    ReplyDelete
  20. Your web site is great. I was extremely touched by this post.

    ReplyDelete
  21. Thanks for your thoughts. It is a worth reading post. I am very happy to visit your blog. Now I've found what I want. I bazud.com every day and try to learn something from your blog. networth and breaking news about world bazuf.com future tech trends , sports, cricket, business, entertainment, weather, education.
    gooda

    ReplyDelete