Using Spring and MongoDB with
CloudFoundry

Josh Long
@starbuxman
Spring Developer Advocate
SpringSource, a division of VMware




                                     © 2009 VMware Inc. All rights reserved
About Josh Long


                  Spring Developer Advocate

                  @starbuxman
                  josh.long@springsource.com




                                         th
                                           si
                                            is
                                                im
                                                 po
                                                     rta
                                                      nt
                                                        !
                                    Free Book!
                                    SpringSource.org/roo




                                                            2
Spring




         3
Spring’s aim:
 bring simplicity to Java development
                                                             data
  web tier                                  integration
                                batch                       access
    &        service tier                        &                           mobile
                             processing                   / NoSQL /
   RIA                                      messaging
                                                          Big Data


                            The Spring framework
the cloud:                    lightweight                    traditional
     CloudFoundry                                                  WebSphere
                                          tc Server
       VMForce                                                      JBoss AS
                                           Tomcat
   Google App Engine                                               WebLogic
                                             Jetty
  Amazon Web Services                                          (on legacy versions, too!)




                                                                                            4
Spring Data




              5
Data Access Challenge #1: Scale Horizontally




                                               6
Data Access Challenge #2: Heterogeneous data access needs




                                                            7
New demands on data
               access
•... until we needed
inexpensive horizontal
scaling for some large web
based applications ...
• ... and we needed to
deploy our apps in the
cloud ...

                             * image courtesy of Bitcurrent   8
NoSQL offers several data store categories




Key-Value       Column          Document     Graph




  Redis,         Cassandra,       MongoDB    Neo4J
  Riak           HBase




                                                     9
NoSQL offers several data store categories




Key-Value       Column          Document                        Graph




                                  MongoDB
                                  (who cares about the rest?)




                                                                    10
Spring Framework
   built-in data access support
•Transaction abstractions
•Common data access exception hierarchy
•JDBC - JdbcTemplate
•ORM - Hibernate, JPA support
•OXM - Object to XML mapping
•Serializer/Deserializer strategies (Spring 3.0)
•Cache support (Spring 3.1)

                                                   11
http://www.springsource.org/spring-data

•Spring Data Key-value
•Spring Data Document
•Spring Data Graph
•Spring Data Column
•Spring Data Blob
•Spring Data JPA Repository / JDBC Extensions
•Spring Gemfire / Spring Hadoop ...
•Grails iNcOnSeQuentiaL
                                                12
Spring Data Building Blocks



•Low level data access APIs
   ✓MongoTemplate, RedisTemplate ...
•Object Mapping (Java and GORM)
•Cross Store Persistence Programming model
•Generic Repository support
•Productivity support in Roo and Grails

                                             13
Spring MongoDB




                 14
Spring Data Document
                Mongo
•MongoTemplate interface for mapping Mongo documents
  •MongoConverter
  •SimpleMongoConverter for basic POJO mapping support
  •Leverage Spring 3.0 TypeConverters and SpEL
  •Exception translation
•Advanced Mapping(@Document, @Id, @DbRef)
  •Annotation based
•MongoRepository
  •Built on Hades support for JPA Repositories
                                                         15
Simple Domain Class




                      16
Mongo Template

Direct Usage of the Mongo Template:




                                      17
Mongo Template

Direct Usage of the Mongo Template:




                 Insert into “Person”
                     Collection




                                        17
Mongo Template

Direct Usage of the Mongo Template:




           findOne using query: { "name" : "Joe"}
              in db.collection: database.Person




                                                    17
Mongo Template

Direct Usage of the Mongo Template:




          Dropped collection [database.person]




                                                 17
Generic Repository

Interface for generic CRUD operations on a repository for a specific type




                                                                            18
Paging and Sorting Repository


Paging and Sorting Repository:   Extends “CrudRepository”




                                                            19
Paging and Sorting Repository


Paging and Sorting Repository:   Extends “CrudRepository”




 Usage:




                                                            19
Custom Repository

Custom Repository:




                     20
Custom Repository

Custom Repository:




Keywords :
Keyword       Sample                               Logical result

GreaterThan   findByAgeGreaterThan(int age)        {"age" : {"$gt" : age}}

LessThan      findByAgeLessThan(int age)           {"age" : {"$lt" : age}}

Between       findByAgeBetween(int from, int to)   {"age" : {"$gt" : from, "$lt" : to}}

NotNull       findByFirstnameNotNull()             {”firstname" : {"$ne" : null}}

Null          findByFirstnameNull()                {”firstname" : null}

Like          findByFirstnameLike(String name)     "firstname" : firstname} (regex)
                                                                                 20
Cross Store




              21
JPA and MongoDB

 JPA “Customer” with a “SurveyInfo” Document




                                               22
Using a Cross-Store

  Saving a Customer with a SurveryInfo




                                         23
Using a Cross-Store

  Saving a Customer with a SurveryInfo
                           Create Customer




                                             23
Using a Cross-Store

  Saving a Customer with a SurveryInfo

                            Create SurveyInfo




                                                23
Using a Cross-Store

  Saving a Customer with a SurveryInfo




             Assign Survey to Customer




                                         23
Using a Cross-Store

  Saving a Customer with a SurveryInfo




                         Save




                                         23
Using a Cross-Store

  Saving a Customer with a SurveryInfo




                         Save




  Mongo Document:




                                         23
Cloud Foundry




                24
Cloud Foundry

   github.com/cloudfoundry
   cloudfoundry.org
   @cloudfoundry
   Supports:
    • Languages: Java, Scala, Node.js, Grails, Ruby/Rails/Sinatra, etc.
    • Services: MongoDB, Redis, MySQL, PostgreSQL, RabbitMQ
 ecosystem forks:
    • AppFog - PHP
    • Stacato: Python, Perl, etc., from ActiveState




                                                                          25
Choice of clouds




                                                               .js




                                                                       ce
                                                                     rfa
                       Ap




                                                                   te
         Data                                                              Private
                          p




                                                                 In
                              lic

        Services
                                                                           Clouds




                                                               er
                               at
                                io




                                                              vid
                                    n



                                                                     Public


                                                            o
                                    Se



                     Msg




                                                         Pr
                   Services
                                       r



                                                                     Clouds
                                          vic




                                                          d
                                                        ou
                                              e
                                             In


                                                      Cl

                                                              Micro
                                               te




                                Other
                                                  r




                               Services
                                                              Clouds
                                                  fa
                                                   e c




                                                                                     26
Choice of clouds




                                                               .js




                                                                       ce
                                                                     rfa
                       Ap




                                                                   te
         Data                                                              Private
                          p




                                                                 In
                              lic

        Services
                                                                           Clouds
                                                                                            …



                                                               er
                               at
                                io




                                                              vid
                                    n



                                                                     Public


                                                            o
                                    Se



                     Msg




                                                         Pr
                   Services
                                       r



                                                                     Clouds
                                          vic




                                                          d
                                                        ou
                                              e
                                             In




                                                                                     .COM
                                                      Cl

                                                              Micro
                                               te




                                Other
                                                  r




                               Services
                                                              Clouds
                                                  fa
                                                   e c




                                                                                            26
Setting up Cloud Foundry Demo




                                27
Accessing Services Bound to Cloud Foundry (producer)

private String mongoDatabaseServiceName = "survey-mongo";

@Bean
public CloudEnvironment cloudEnvironment() {
	   return new CloudEnvironment();
}

@Bean
public MongoServiceInfo mongoServiceInfo() {
	   return cloudEnvironment().getServiceInfo(
       mongoDatabaseServiceName, MongoServiceInfo.class);
}

@Bean
public MongoDbFactory mongoDbFactory() {
	   MongoServiceCreator msc = new MongoServiceCreator();
	   MongoDbFactory db = msc.createService(mongoServiceInfo());
}


                                                                 28
Accessing Services Bound to Cloud Foundry (consumption)


@Inject private Mongo mongo ;

@Inject private MongoTemplate mongoTemplate;




                                                          29
Cloud Foundry MongoDB Demo




                             30
Where to Find More

 Spring Data Project:
 http://bit.ly/spring-data
 CloudFoundry Samples:
 http://bit.ly/cloudfoundry-samples
 MicroCloud Foundry for Spring Developers
 http://bit.ly/mcf4spring
 Spring Data Mongo on Cloud Foundry (webinar, 12/01/2011)
 http://bit.ly/spring-mongo-cloudfoundry




                                                             31

using Spring and MongoDB on Cloud Foundry

  • 1.
    Using Spring andMongoDB with CloudFoundry Josh Long @starbuxman Spring Developer Advocate SpringSource, a division of VMware © 2009 VMware Inc. All rights reserved
  • 2.
    About Josh Long Spring Developer Advocate @starbuxman [email protected] th si is im po rta nt ! Free Book! SpringSource.org/roo 2
  • 3.
  • 4.
    Spring’s aim: bringsimplicity to Java development data web tier integration batch access & service tier & mobile processing / NoSQL / RIA messaging Big Data The Spring framework the cloud: lightweight traditional CloudFoundry WebSphere tc Server VMForce JBoss AS Tomcat Google App Engine WebLogic Jetty Amazon Web Services (on legacy versions, too!) 4
  • 5.
  • 6.
    Data Access Challenge#1: Scale Horizontally 6
  • 7.
    Data Access Challenge#2: Heterogeneous data access needs 7
  • 8.
    New demands ondata access •... until we needed inexpensive horizontal scaling for some large web based applications ... • ... and we needed to deploy our apps in the cloud ... * image courtesy of Bitcurrent 8
  • 9.
    NoSQL offers severaldata store categories Key-Value Column Document Graph Redis, Cassandra, MongoDB Neo4J Riak HBase 9
  • 10.
    NoSQL offers severaldata store categories Key-Value Column Document Graph MongoDB (who cares about the rest?) 10
  • 11.
    Spring Framework built-in data access support •Transaction abstractions •Common data access exception hierarchy •JDBC - JdbcTemplate •ORM - Hibernate, JPA support •OXM - Object to XML mapping •Serializer/Deserializer strategies (Spring 3.0) •Cache support (Spring 3.1) 11
  • 12.
    http://www.springsource.org/spring-data •Spring Data Key-value •SpringData Document •Spring Data Graph •Spring Data Column •Spring Data Blob •Spring Data JPA Repository / JDBC Extensions •Spring Gemfire / Spring Hadoop ... •Grails iNcOnSeQuentiaL 12
  • 13.
    Spring Data BuildingBlocks •Low level data access APIs ✓MongoTemplate, RedisTemplate ... •Object Mapping (Java and GORM) •Cross Store Persistence Programming model •Generic Repository support •Productivity support in Roo and Grails 13
  • 14.
  • 15.
    Spring Data Document Mongo •MongoTemplate interface for mapping Mongo documents •MongoConverter •SimpleMongoConverter for basic POJO mapping support •Leverage Spring 3.0 TypeConverters and SpEL •Exception translation •Advanced Mapping(@Document, @Id, @DbRef) •Annotation based •MongoRepository •Built on Hades support for JPA Repositories 15
  • 16.
  • 17.
    Mongo Template Direct Usageof the Mongo Template: 17
  • 18.
    Mongo Template Direct Usageof the Mongo Template: Insert into “Person” Collection 17
  • 19.
    Mongo Template Direct Usageof the Mongo Template: findOne using query: { "name" : "Joe"} in db.collection: database.Person 17
  • 20.
    Mongo Template Direct Usageof the Mongo Template: Dropped collection [database.person] 17
  • 21.
    Generic Repository Interface forgeneric CRUD operations on a repository for a specific type 18
  • 22.
    Paging and SortingRepository Paging and Sorting Repository: Extends “CrudRepository” 19
  • 23.
    Paging and SortingRepository Paging and Sorting Repository: Extends “CrudRepository” Usage: 19
  • 24.
  • 25.
    Custom Repository Custom Repository: Keywords: Keyword Sample Logical result GreaterThan findByAgeGreaterThan(int age) {"age" : {"$gt" : age}} LessThan findByAgeLessThan(int age) {"age" : {"$lt" : age}} Between findByAgeBetween(int from, int to) {"age" : {"$gt" : from, "$lt" : to}} NotNull findByFirstnameNotNull() {”firstname" : {"$ne" : null}} Null findByFirstnameNull() {”firstname" : null} Like findByFirstnameLike(String name) "firstname" : firstname} (regex) 20
  • 26.
  • 27.
    JPA and MongoDB JPA “Customer” with a “SurveyInfo” Document 22
  • 28.
    Using a Cross-Store Saving a Customer with a SurveryInfo 23
  • 29.
    Using a Cross-Store Saving a Customer with a SurveryInfo Create Customer 23
  • 30.
    Using a Cross-Store Saving a Customer with a SurveryInfo Create SurveyInfo 23
  • 31.
    Using a Cross-Store Saving a Customer with a SurveryInfo Assign Survey to Customer 23
  • 32.
    Using a Cross-Store Saving a Customer with a SurveryInfo Save 23
  • 33.
    Using a Cross-Store Saving a Customer with a SurveryInfo Save Mongo Document: 23
  • 34.
  • 35.
    Cloud Foundry  github.com/cloudfoundry  cloudfoundry.org  @cloudfoundry  Supports: • Languages: Java, Scala, Node.js, Grails, Ruby/Rails/Sinatra, etc. • Services: MongoDB, Redis, MySQL, PostgreSQL, RabbitMQ  ecosystem forks: • AppFog - PHP • Stacato: Python, Perl, etc., from ActiveState 25
  • 36.
    Choice of clouds .js ce rfa Ap te Data Private p In lic Services Clouds er at io vid n Public o Se Msg Pr Services r Clouds vic d ou e In Cl Micro te Other r Services Clouds fa e c 26
  • 37.
    Choice of clouds .js ce rfa Ap te Data Private p In lic Services Clouds … er at io vid n Public o Se Msg Pr Services r Clouds vic d ou e In .COM Cl Micro te Other r Services Clouds fa e c 26
  • 38.
    Setting up CloudFoundry Demo 27
  • 39.
    Accessing Services Boundto Cloud Foundry (producer) private String mongoDatabaseServiceName = "survey-mongo"; @Bean public CloudEnvironment cloudEnvironment() { return new CloudEnvironment(); } @Bean public MongoServiceInfo mongoServiceInfo() { return cloudEnvironment().getServiceInfo( mongoDatabaseServiceName, MongoServiceInfo.class); } @Bean public MongoDbFactory mongoDbFactory() { MongoServiceCreator msc = new MongoServiceCreator(); MongoDbFactory db = msc.createService(mongoServiceInfo()); } 28
  • 40.
    Accessing Services Boundto Cloud Foundry (consumption) @Inject private Mongo mongo ; @Inject private MongoTemplate mongoTemplate; 29
  • 41.
  • 42.
    Where to FindMore  Spring Data Project: http://bit.ly/spring-data  CloudFoundry Samples: http://bit.ly/cloudfoundry-samples  MicroCloud Foundry for Spring Developers http://bit.ly/mcf4spring  Spring Data Mongo on Cloud Foundry (webinar, 12/01/2011)  http://bit.ly/spring-mongo-cloudfoundry 31

Editor's Notes

  • #2  \n
  • #3 Hello, thank you or having me. Im pleased to have the opportunity to introduce you today to Spring and the SpringSource Tool Suite \n\nMy anem is Josh Long. I serve as the developer advocate for the Spring framework. I’ve used it in earnest and advocated it for many years now. i’m an author on 3 books on the technology, as well as a comitter to many of the Spring projects. Additionally, I take community activism very seriously and do my best to participate in the community. Sometimes this means answering question on Twitter, or in the forums, or helping contribute to the InfoQ.com and Artima.com communities\n
  • #4 \n
  • #5 these different framerworks let u tackle any problem youre likely to want to solve today \n- we support javee1.4 + 1.5 + 1.6; \n\nsits above target platform \nruns in cloud \n\n
  • #6 \n
  • #7 Oracle RAC clustering is very expensive. $70K/CPU according to list. Even at ½ the cost, it’s still really expensive to build out a horizontal data access grid. \n
  • #8 The amount of log data amassed in Facebook’s operations is staggering. Rothschild said Facebook manages more than 25 terabytes of data per day in logging data, which he said was the equivalent of about 1,000 times the volume of mail delivered daily by the U.S. Postal Service.\n\nBASE (basically available, soft state, eventually consistent)\n
  • #9 \n
  • #10 \n
  • #11 \n
  • #12 \n
  • #13 \n
  • #14 \n
  • #15 \n
  • #16 \n
  • #17 \n
  • #18 \n
  • #19 \n
  • #20 \n
  • #21 \n
  • #22 \n
  • #23 \n
  • #24 \n
  • #25 \n
  • #26 \n
  • #27 \n
  • #28 \n
  • #29 \n
  • #30 \n
  • #31 \n
  • #32 \n
  • #33 \n
  • #34 \n
  • #35 \n
  • #36 \n
  • #37 \n
  • #38 \n
  • #39 \n
  • #40 \n
  • #41 \n
  • #42 \n
  • #43 \n
  • #44 \n
  • #45 \n
  • #46 \n
  • #47 \n
  • #48 \n