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

37 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
  22. I can't offer a specific lesson for "Spring Data MongoDB on Cloud Foundry" as of my most recent knowledge update in September 2021 since tutorial availability varies and I lack real-time internet connection to look for the most recent materials.

    Honorarios de Abogado de Bienes

    ReplyDelete
  23. This comprehensive tutorial on developing a Spring Data MongoDB project for Cloud Foundry is very informative and well-structured. It provides a clear and detailed guide for developers looking to deploy their applications on the cloud.
    Abogado Violencia Doméstica Nueva Jersey Cherry Hill

    ReplyDelete
  24. semi truck accident law firm
    The title effectively communicates the tutorial's content, focusing on "Spring Data MongoDB on Cloud Foundry." The introduction engages readers with a hook or brief overview, emphasizing its relevance. The tutorial provides step-by-step guidance, catering to readers of varying skill levels. If applicable, visual aids like screenshots or code snippets can enhance the learning experience. The conclusion generates anticipation for Part 2, providing a glimpse of what readers can expect in the next installment. Overall, the title effectively communicates the tutorial's content and engages readers.

    ReplyDelete
  25. Great tutorial! I've been looking for a comprehensive guide on how to use Spring Data MongoDB on Cloud Foundry, and this definitely delivered. The step-by-step instructions were clear and easy to follow, even for someone like me who is new to both technologies.
    I really appreciated the detailed explanation of how to set up the development environment and configure the necessary dependencies. It made the whole process much smoother and helped me avoid common pitfalls.
    The code snippets provided were invaluable in showcasing the key concepts and best practices when working with MongoDB and Cloud Foundry. I particularly liked how the tutorial integrated real-world examples, making it easier to understand how to apply the concepts to my own projects.
    Overall, this tutorial was a fantastic resource for anyone looking to leverage Spring Data MongoDB on Cloud Foundry. Thanks for putting it together!

    va uncontested divorce


    ReplyDelete
  26. Great informative post to read. Keep posting more good interesting blogs. abogado transito mecklenburg va

    ReplyDelete
  27. The "Spring Data MongoDB on Cloud Foundry Tutorial Part 1" is a comprehensive and well-structured guide for developers venturing into cloud-based MongoDB applications. The review applauds the tutorial's clarity in explaining the integration of Spring Data with MongoDB on the Cloud Foundry platform. The step-by-step approach ensures accessibility for developers at various skill levels. The inclusion of real-world examples and practical insights enhances the tutorial's value, making it a valuable resource for those seeking to leverage these technologies effectively. In summary, an excellent tutorial laying a strong foundation for Spring Data MongoDB deployment on Cloud Foundry. bancarrota capítulo 7 cerca de mí





    ReplyDelete
  28. The tutorial Part 1 on 'Spring Data MongoDB on Cloud Foundry' provides a comprehensive guide for developers, highlighting its relevance for modern cloud-based applications. It offers step-by-step instructions, clear explanations, practical examples, and a well-structured format. The tutorial is a valuable resource for mastering the intricacies of Spring Data MongoDB integration within a Cloud Foundry environment, making it accessible to developers of varying expertise.
    Best Divorce Lawyer in New York 10013

    ReplyDelete

  29. The "Spring Data MongoDB on Cloud Foundry" tutorial Part 1 provides a comprehensive and insightful guide to integrating Spring Data MongoDB within the Cloud Foundry environment. It effectively lays the groundwork for developers looking to leverage these technologies, offering step-by-step instructions and practical examples. The tutorial's clarity and organization make it accessible for both beginners and experienced developers, fostering a smooth learning curve. By breaking down complex concepts, it ensures a solid foundation for building robust applications using Spring Data MongoDB on Cloud Foundry. Overall, an invaluable resource for those seeking a seamless integration between these two powerful technologies. abogado dui botetourt va

    ReplyDelete
  30. The "Spring Data MongoDB on Cloud Foundry tutorial Part 1" is a comprehensive guide for developers utilizing Spring Data MongoDB in cloud environments. It provides clear explanations and step-by-step instructions, making it accessible to both experienced and new developers. The tutorial emphasizes practical implementation and best practices, equipping developers with the knowledge and skills to build robust and scalable applications. This must-read resource is essential for anyone working with MongoDB on Cloud Foundry.
    virginia beach motorcycle accident attorney

    ReplyDelete
  31. file uncontested divorce virginia
    The "Spring Data MongoDB on Cloud Foundry tutorial" is a comprehensive guide for developers deploying Spring Data MongoDB applications on Cloud Foundry. It provides step-by-step instructions, explanations, prerequisites, troubleshooting tips, links to additional resources, and best practices for optimizing applications. The tutorial also encourages reader engagement by inviting them to share their experiences and feedback. The tutorial effectively communicates the subject matter, providing a clear understanding of the integration and best practices, and fostering community interaction. Overall, it serves as a valuable resource for developers looking to leverage these technologies.

    ReplyDelete
  32. The tutorial outlines the setup and deployment of a Spring Data MongoDB project on Cloud Foundry. It emphasizes the importance of Java version compatibility, dependency management using Gradle, compatibility with Spring and Spring Data MongoDB versions, Tomcat version, and Servlet specification. It also recommends using the Cloud Foundry CLI instead of VMC for managing applications. The tutorial also provides a step-by-step deployment guide, addressing common issues and providing troubleshooting tips. Additional topics include configuration management, security considerations, scaling and monitoring, continuous integration and deployment (CI/CD), documentation and support, and future improvements. The tutorial encourages users to refer to official documentation and community forums for further assistance. It also mentions future updates and improvements, such as support for newer versions of Java, Spring, or Cloud Foundry, and user feedback. The tutorial aims to help users successfully set up and deploy their Spring Data MongoDB applications on Cloud Foundry. abogado lesiones personales virginia

    ReplyDelete
  33. In Virginia, and particularly in Fairfax, driving recklessly can have dire repercussions, including steep fines, license suspension, and even jail time. To keep yourself and other drivers safe when driving, it's imperative that you follow the law.
    reckless driving virginia fairfax

    ReplyDelete
  34. The review comments on "Breaking Bad S05E07: Say My Name" reveal a pivotal episode, sparking curiosity and potentially engaging fans without providing spoilers.
    family law retainer fee

    ReplyDelete