Weitere ähnliche Inhalte
Ähnlich wie JCR, Sling or AEM? Which API should I use and when? (20)
Mehr von connectwebex (19)
JCR, Sling or AEM? Which API should I use and when?
- 1. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 1
JCR, Sling or AEM? Which API should I use and
when?
Maciej Matuszewski
maciej.matuszewski@cognifide.com
- 2. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 2
What is the High-level API?
- 3. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 3
What is the Low-level API?
- 4. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 4
JCR, Sling and AEM
AEM
Sling
JCR
Other
- 5. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 5
Why bother?
- 6. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 6
Become an AEM Expert
- 7. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 7
resource.getChild("text")
node.getNode("text")
node.getProperty("jcr:title").getString()
resource.getChild("jcr:title").adaptTo(String.class)
page.getTitle()
- 8. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 8
node.addNode(String relPath)
resourceResolver.create(Resource parent, String name,
Map<String,Object> properties)
node.orderBefore(String srcChildRelPath, String destChildRelPath)
pageManager.order(Page page, String beforeName)
pageManager.order(Resource resource, String beforeName)
- 9. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 9
Don’t mix abstraction layers
- 10. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 10
Session session = resourceResolver.adaptTo(Session.class);
QueryManager queryManager = session.getWorkspace().getQueryManager();
Query query = queryManager.createQuery(QUERY_STRING, Query.JCR_SQL2);
QueryResult queryResult = query.execute();
NodeIterator nodeIterator = queryResult.getNodes();
while (nodeIterator.hasNext()) {
Node nextNode = nodeIterator.nextNode();
String path = nextNode.getPath();
Resource resource = resourceResolver.getResource(path);
veryImportantMethod(resource);
}
- 11. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 11
Iterator<Resource> resourcesIterator = resourceResolver
.findResources(QUERY_STRING, Query.JCR_SQL2);
while (resourcesIterator.hasNext()) {
Resource resource = resourcesIterator.next();
veryImportantMethod(resource);
}
- 12. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 12
@Inject
public CarModel(Resource resource) throws RepositoryException {
Node node = resource.adaptTo(Node.class);
Node pageNode;
if (node.getParent().getName().equals("rightParsys")) {
pageNode = node.getParent().getParent().getParent().getParent();
} else {
pageNode = node.getParent().getParent().getParent();
}
String pageTitle = node.getProperty("jcr:title").getString();
}
- 13. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 13
@Inject
public CarModel(@CurrentPage Page page) {
String pageTitle = page.getTitle();
}
- 14. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 14
resourceResolver.commit();
Session session = resourceResolver.adaptTo(Session.class);
session.save();
- 15. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 15
What are your tools?
- 16. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 16
Components
- 17. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 17
Components
Servlets
Filters
- 18. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 18
Components
OSGi Services
Servlets
Filters
- 19. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 19
Components
OSGi Services
Servlets
Filters
Templates
- 20. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 20
Components
OSGi Services
Servlets
Filters
Templates
Replication
- 21. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 21
Components
OSGi Services
Servlets
Filters
Templates
Querying Repository
Replication
- 22. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 22
Components
OSGi Services
Servlets
Filters
Templates
Querying Repository
Replication
Dispatcher
- 23. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 23
Components
OSGi Services
Servlets
Filters
Workflows
Templates
Querying Repository
Replication
Dispatcher
- 24. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 24
Components
OSGi Services
Servlets
Dynamic dialogs
Filters
Workflows
Templates
Querying Repository
Replication
Dispatcher
- 25. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 25
Components
OSGi Services
Servlets
Dynamic dialogs
Filters
Events
Jobs
Workflows
Templates
Querying Repository
Replication
Dispatcher
- 26. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 26
Components
OSGi Services
Servlets
Dynamic dialogs
Filters
Events
Jobs
Workflows
Templates
Digital Assets
Querying Repository
Replication
Dispatcher
- 27. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 27
Components
OSGi Services
Servlets
Dynamic dialogs
Filters
Events
Jobs
Workflows
Templates
Users and groups
ACLs
Digital Assets
Querying Repository
Replication
Dispatcher
- 28. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 28
Components
OSGi Services
Servlets
Dynamic dialogs
Filters
Events
Jobs
Workflows
Templates
Users and groups
ACLs
Digital Assets
Targeting and Analytics
Querying Repository
Replication
Dispatcher
- 29. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 29
Components
OSGi Services
Servlets
Dynamic dialogs
Filters
Events
Jobs
Workflows
Templates
Users and groups
ACLs
Digital Assets
Multi-Site Management
Targeting and Analytics
Querying Repository
Replication
Dispatcher
- 30. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 30
- 31. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 31
- 32. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 32
Sling advantages
More extendable/customizable
Simpler API, less exceptions
Adapters - http://localhost:4502/system/console/adapters
Support for ResourceDecorator, ResourceMerger and custom ResourceProviders
- 33. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 33
Models
SlingModels
Slice
Neba
etc.
- 34. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 34
@SlingServlet(resourceTypes = "myapp/core/components/car",
extensions = "json", selectors = "version")
public class CarServlet extends SlingSafeMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response)
throws ServletException, IOException {
Resource resource = request.getResource();
CarModel model = resource.adaptTo(CarModel.class);
model.getVersion();
model.getRegistration();
model.getFuelType();
//TODO
}
- 35. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 35
<div class="car-info">
Name: ${model.name}
Registration: ${model.registration}
Version: ${model.version}
</div>
- 36. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 36
AEM API is helpful
Authoring pages
Tagging
DAM
Replication
Workflows
- 37. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 37
Pages
Page containingPage = pageManager.getContainingPage(resource);
containingPage.getTitle();
Page newPage = pageManager.create(resource.getPath(), "child-page",
"/apps/myapp/core/templates/basic", "Child Page");
resource.adaptTo(ReplicationStatus.class).isActivated()
pageManager.touch(Node page, boolean shallow, Calendar now,
boolean clearRepl)
- 38. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 38
Digital Asset Management
DamUtils
Asset
Metadata
MIME type
Renditions
- 39. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 39
Tags
RangeIterator<Resource> tagsIterator = tagManager.find("geometrixx:events");
TagManager.FindResults findResults = tagManager.findByTitle("Events");
Tag[] tags = tagManager.getTags(resource);
for (Tag tag : tags) {
long count = tag.getCount();
Iterator<Resource> resources = tag.find();
}
- 40. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 40
Keep JCR for special occasions
Workspace Management
Versioning
Access Control Management
Users and Groups
Locking
Lifecycle Management
Mixins
Node Type Management
Primary Type Change
- 41. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 41
Performance
- 42. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 42
Performance
Important argument to use low level API
Higher level API usually means additional overhead
Best performance is always achieved by using lowest possible API
Let’s test it!
- 43. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 43
Node rootNode = getNode("/content/geometrixx")
println countChildren(rootNode)
long countChildren(Node root) {
long count = 0
NodeIterator iterator = root.getNodes()
while (iterator.hasNext()) {
Node child = iterator.nextNode()
count += countChildren(child)
count++
}
return count
}
- 44. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 44
Resource root = getResource("/content/geometrixx")
println countChildren(root)
long countChildren(Resource root) {
long count = 0
Iterator<Resource> iterator = root.listChildren()
while (iterator.hasNext()) {
Resource child = iterator.next()
count += countChildren(child)
count++
}
return count
}
- 45. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 45
0 2 4 6 8
AEM 5.6.1
AEM 6.0 SP2
Traversing /content/geometrixx tree
Sling API JCR API
- 46. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 46
GEOMETRIXX_CONTENT_PATH =
"/content/geometrixx/en/toolbar/profiles/forgot/thank_you/jcr:content/par"
Node parsysNode = getNode(GEOMETRIXX_CONTENT_PATH)
for (int i = 0; i < 10000; i++) parsysNode.getNode("text")
Resource parsysResource = getResource(GEOMETRIXX_CONTENT_PATH)
for (int i = 0; i < 10000; i++) parsysResource.getChild("text")
- 47. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 47
GEOMETRIXX_CONTENT_PATH =
"/content/geometrixx/en/toolbar/profiles/forgot/thank_you/jcr:content/par"
Node parsysNode = getNode(GEOMETRIXX_CONTENT_PATH)
for (int i = 0; i < 10000; i++) parsysNode.getNode("text")
Resource parsysResource = getResource(GEOMETRIXX_CONTENT_PATH)
for (int i = 0; i < 10000; i++) parsysResource.getChild("text")
JCR time – 37 ms
Sling time – 931 ms
Times for 10000 operations
- 48. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 48
Performance summary
JCR is much faster than Sling
- 49. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 49
Performance summary
JCR is much faster than Sling
Q.E.D.
- 50. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 50
Performance summary
JCR is much faster than Sling
Q.E.D.
So what?!
- 51. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 51
Component’s performance
Sample page with 3 Rich Text components
Let each component get it’s child 3 times
Resource.getChild() operation takes 0.0931 ms
Node.getNode() operation takes 0.0037 ms
Header
Menu
Carousel
Rich Text
Rich Text Rich Text
Article List
Footer
- 52. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 52
Component’s performance
Sample page with 3 Rich Text components
Let each component get it’s child 3 times
Resource.getChild() operation takes 0.0931 ms
Node.getNode() operation takes 0.0037 ms
Difference between JCR and Sling for entire
page is less than 1 ms!
Header
Menu
Carousel
Rich Text
Rich Text Rich Text
Article List
Footer
- 53. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 53
Component’s performance
Sample page with 3 Rich Text components
Let each component get it’s child 3 times
Resource.getChild() operation takes 0.0931 ms
Node.getNode() operation takes 0.0037 ms
Difference between JCR and Sling for entire
page is less than 1 ms!
And it’s without cache on dispather
or performance tunning.
Header
Menu
Carousel
Rich Text
Rich Text Rich Text
Article List
Footer
- 54. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 54
Batch operations
Data import/export
Non-cached
Constantly repeated
Low-level
Components
Servlets
Non-critical
One time ops
High-level
- 55. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 55
- 56. The future of digital marketing. London, Poland, Copenhagen.
© 29/06/2015 Page 56
Thank you!
Przemysław Pakulski
Tomasz Rękawek
maciej.matuszewski@cognifide.com