SpringOne 2GX 2011 - Writing applications for Cloud Foundry using Spring and MongoDB
1. Writing applications for Cloud Foundry
using Spring and MongoDB
Thomas Risberg, VMware
<trisberg@vmware.com> - @trisberg
Jared Rosoff, 10gen
<jsr@10gen.com> - @forjared
Š 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Wednesday, October 26, 11 1
2. Outline
What we will cover:
⢠Introduction to Cloud Foundry
⢠Introduction to MongoDB
⢠Spring Data support for MongoDB
â Using the MongoTemplate
â Using the Mongo Repository support
⢠Why run MongoDB in the cloud?
2
Wednesday, October 26, 11 2
3. What is Cloud Foundry?
⢠Cloud Foundry is a PaaS
â The application platform will be delivered as a service in the
cloud era
â The industry calls this platform as a service (PaaS)
⢠PaaS makes it much easier to deploy, run and scale
applications
⢠But PaaS solutions in the market have fatal ďŹaws today
â Limited in framework, application services and/or cloud support
⢠Cloud Foundry aim to ďŹx thatâŚ
3
Wednesday, October 26, 11 3
4. Characteristics of PaaS
The application platform for the cloud era
⢠Integrated software stack
⢠Application execution engine
⢠Self-service application deployment
⢠Automated application infrastructure provisioning
⢠Curated, updated and operated as a service
4
Wednesday, October 26, 11 4
5. Cloud Foundry â The ďŹrst open PaaS
⢠Self-service application execution engine
â Build applications with latest high productivity frameworks
⢠Automation engine for deployment and lifecycle
management
â Deploy and cloud-scale applications in seconds
⢠Open architecture
â Choice of clouds for deployment
â Choice of industry-standard frameworks
â Choice of application infrastructure services
â Extensible architecture to âdigestâ future cloud innovation
â Available as open source
5
Wednesday, October 26, 11 5
7. Choice of application services
.js
Ap
pli
ca
Data Services
tio
n
Se
rvi
ce
Msg Services
Int
er
fa
ce
Other
Services
7
Wednesday, October 26, 11 7
8. Choice of clouds
.js
Ap
pli
ce
Private
ca
rfa
Data Services
tio
nte
Clouds
n
rI
Se
ide
rvi
Public
ov
ce
Msg Services
Pr
Clouds Partners
Int
d
ou
er
.COM
fa
Cl
Micro
ce
Other
Services
Clouds
8
Wednesday, October 26, 11 8
10. MongoDB
Application Document
Oriented
High { author: âsteveâ,
date: new Date(),
Performance text: âAbout MongoDB...â,
tags: [âtechâ, âdatabaseâ]}
Horizontally Scalable
10
Wednesday, October 26, 11 10
11. Why MongoDB
⢠Double click serving 400,000 ads / second
⢠People writing their own datastores
⢠Caching is de rigueur
⢠Complex ORM frameworks
⢠Computer architecture trends
⢠Cloud computing
11
Wednesday, October 26, 11 11
12. Costs go up!
Launch
+90 Days
+1 Year
12
Wednesday, October 26, 11 12
13. Productivity
Great!
Denormalize
data model
Stop using
joins
Custom
caching
Custom
sharding
Project Start
+90 Days
+1 Year
13
Wednesday, October 26, 11 13
24. Demo
Cloud Foundry and Mongo DB
24 Š 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Wednesday, October 26, 11 24
25. Spring Data Intoduction
What is Spring Data all about?
⢠Bring classic Spring value propositions to NOSQL
â Productivity
â Programming model consistency
⢠Support for a wide range of NOSQL databases
⢠Also, support for new features for JDBC and JPA
⢠Support for other data related projects like Hadoop and
GemďŹre
25
Wednesday, October 26, 11 25
26. Spring Data MongoDB Support
Spring Data support for MongoDB:
⢠MongoTemplate
âMongoConverter interface for mapping Mongo documents
⢠Built-in Advanced Mapping
â Annotation based (@Document, @Id, @DbRef)
⢠MappingMongoConverter for POJO mapping support
⢠Leverage Spring 3.0 TypeConverters and SpEL
âException translation
âJava based Query, Criteria, and Update DSLs
⢠MongoRepository
âBuilt on Spring Data JPA (Hades) support for JPA Repositories
âQueryDSL integration to support type-safe queries.
26
Wednesday, October 26, 11 26
27. Working with POJOs
@Document
public class Book {
private String title;
@DBRef
@Document
private Author author;
public class Author {
@Id
@Id @SuppressWarnings("unused")
private String isbn;
private String id;
private BigDecimal price;
private String name;
private Date published;
public String getName() {
return name;
private Set<String> categories;
}
// getters and setters
public void setName(String name) {
}
this.name = name;
}
}
27
Wednesday, October 26, 11 27
28. Mongo Template CRUD Methods
Mongo mongo = new Mongo("localhost", 27017);
MongoDbFactory mongoDbFactory =
new SimpleMongoDbFactory(mongo, "db");
MongoTemplate mongoTemplate = new
MongoTemplate(mongoDbFactory);
mongoTemplate.insert(book);
List<Book> books = mongoTemplate.findAll(Book.class);
Book book = mongoTemplate.findOne(query, Book.class);
mongoTemplate.save(book);
mongoTemplate.remove(query, Book.class);
28
Wednesday, October 26, 11 28
30. Cloud Foundry MongoDB Support
Namespace support for MongoDB
<cloud:mongo-db-factory id="mongoDbFactory" write-concern="FSYNC_SAFE">
    <cloud:mongo-options connections-per-host="10" max-wait-time="2000" />
</cloud:mongo-db-factory>
<dependency>
    <groupId>org.cloudfoundry</groupId>
    <artifactId>cloudfoundry-runtime</artifactId>
    <version>0.8.1</version>
</dependency>
<repository>
    <id>org.springframework.maven.milestone</id>
    <name>Spring Framework Maven Milestone Repository</name>
    <url>http://maven.springframework.org/milestone</url>
</repository>
30
Wednesday, October 26, 11 30
31. Demo
MongoTemplate example:
https://github.com/trisberg/springone-mongotemplate
31 Š 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Wednesday, October 26, 11 31
32. Spring Data Repository
Spring Data Repository basics:
⢠Generic repository implementation
⢠Basic CRUD (create, read, update and delete) methods
⢠Generating code for queries deďŹned in repository interface
â ďŹndAll
â ďŹndByName ...
⢠Pagination and sorting support
⢠Currently has JPA and Mongo implementations
32
Wednesday, October 26, 11 32
33. Spring Data Repository Interfaces
CrudRepository
PagingAndSortingRepository
33
Wednesday, October 26, 11 33
34. Spring Data Repository Query Methods
Spring Data Repository query methods:
and more, see reference docs ...
34
Wednesday, October 26, 11 34
35. Mongo Repository Example
public interface BookRepository extends Repository<Book, String> {
Book save(Book book);
Book findOne(String isbn);
void delete(String isbn);
List<Book> findAll();
List<Book> findByPublishedGreaterThan(Date date);
List<Book> findByCategoriesIn(String[] categories);
List<Book> findByPublishedGreaterThanAndCategoriesIn(
Date date, String[] categories);
}
35
Wednesday, October 26, 11 35
36. Demo
Mongo Repository example:
https://github.com/trisberg/springone-repository
36 Š 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Wednesday, October 26, 11 36
37. QueryDSL Repository
QueryDSL allows you to build typesafe queries using Java for
JPA, SQL, MongoDB and more
⢠Generic repository implementation
⢠Code completion in IDE
⢠No Strings
â reference types and properties safely
â easier refactoring
⢠Incremental query deďŹnition is easier
⢠Annotation preprocessor generates query types
37
Wednesday, October 26, 11 37
38. Demo
Mongo QueryDSL Repository example:
https://github.com/trisberg/springone-repo-qdsl
38 Š 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Wednesday, October 26, 11 38
39. Why run MongoDB in the cloud?
39
Wednesday, October 26, 11 39
45. MongoDB in the cloud
⢠Elastic database capacity
â Add more storage, compute, memory dynamically
⢠Portable across environments
â Public cloud
â Private cloud
â Micro cloud
⢠SimpliďŹes operations
â No need to engineer solutions for one-off hardware platforms
â âJust another VMâ
45
Wednesday, October 26, 11 45
46. Q&A
46 Š 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Wednesday, October 26, 11 46