Created

Jan 10, 2013

Using Event filters for handling JCR events through an event handler.

Posted by Nicolas Peltier

In a previous post, i was explaining how to handle events generally, with several solutions among which:

  • jcr observer
  • sling event handler

a distinction was made between both around the fact jcr observers are low level, but allow to do more. While this is still true :-) this POST will explain how to do a fine-grained jcr event listening through sling event handler (and take profit of the sling event handling framework for those events).

While in jcr, everything is content, in sling, everything is a resource, so instead of listening to node/property added/removed we'll listen to resource changed, through the 3 topics :

  1. SlingConstants.TOPIC_RESOURCE_ADDED,
  2. SlingConstants.TOPIC_RESOURCE_CHANGED,
  3. SlingConstants.TOPIC_RESOURCE_REMOVED

Now we should have a handler that listens for all the repository, for those -very common- events, which will certainly be a performance killer. Thus we need to filter out this by adding osgi event filtering

This EventConstants.EVENT_FILTER property takes LDAP filter syntax, and has a path attribute, so we can do e.g. (path=/content/my/root/*) or even (path=/content/my/root/*/myNode).

 

A sample code would then be :

@Component
@Service(value=EventHandler.class)
@Properties({
 @Property(name= EventConstants.EVENT_TOPIC,value= {SlingConstants.TOPIC_RESOURCE_ADDED,SlingConstants.TOPIC_RESOURCE_CHANGED, SlingConstants.TOPIC_RESOURCE_REMOVED}),
 @Property(name=EventConstants.EVENT_FILTER,value="(path=/content/my/root/*/myNode)")
})
public class ExampleServiceImpl implements EventHandler { 
 final String[] eventProps = { "resourceAddedAttributes", "resourceChangedAttributes", "resourceRemovedAttributes" };

 public void handleEvent(org.osgi.service.event.Event event) {
  for (String eventProp : eventProps){
   String[] props = (String[])event.getProperty(eventProp);
   …
 }		
}

Pros of this approach vs. Jcr Observer are : 

  • this is simpler.
  • this is cluster ready.
  • you have the user id information in it (userid attribute of the event).

A con i can see is that in the particular case of a bulk save done of N concerned resources, your handler will be called N times, and then if your handler does persist, you'll have a total of N (handler persistences) + 1 (bulk save) persistences done. While with a jcr observer, you can loop through all the changes of the transaction, and do another batch save, reducing the persistences to 2.

COMMENTS

  • By Orik Parte - 10:36 AM on Mar 16, 2013   Reply
    Excellent tutorial, I has always had issues when trying to handle JCR events. That's the first tutorial that has sample code. I'll try this sample in my code, thanks!
    • By Andy Miles - 10:52 AM on Mar 16, 2013   Reply
      As you mentioned ,These are the resources:

      SlingConstants.TOPIC_RESOURCE_ADDED,
      SlingConstants.TOPIC_RESOURCE_CHANGED,
      SlingConstants.TOPIC_RESOURCE_REMOVED

      I think you forgot
      SlingConstants.TOPIC_RESOURCE_UPDATED

      Andy
      • By Fridge now - 12:57 AM on Mar 17, 2013   Reply
        I have searched the internet a lot for a tutorial about handling JCR events, and finally found it here. Thanks!
        • By small wood burning stoves - 6:15 AM on Apr 01, 2013   Reply
          awesome information man, thx
          • By hdi bee propolis - 6:15 AM on Apr 01, 2013   Reply
            what a great post, thx for the share
            • By linen pants for men - 6:16 AM on Apr 01, 2013   Reply
              wow great information, thank you
              • By manfaat madu asli - 6:23 AM on Apr 01, 2013   Reply
                wow, so useful thank you so much
                • By jual bee propolis - 6:23 AM on Apr 01, 2013   Reply
                  a new information for me, thx
                  • By free minecraft server hosting - 11:54 AM on Apr 13, 2013   Reply
                    Awesome article! This will provide our sites with 2 very important things. First it is always adding fresh up to date content and secondly it will help me earn more knowledge about this topic.
                    • By dien dan seo - 4:52 AM on Apr 15, 2013   Reply
                      its so useful, keep it up
                      • By hostgator coupon 1 cent - 12:40 PM on Apr 16, 2013   Reply
                        While this is still true that is a nice article
                        • By bao phu nu - 4:35 PM on Apr 16, 2013   Reply
                          its so useful
                          • By forum game - 4:36 PM on Apr 16, 2013   Reply
                            i love is jobs, and management of data is done through electronic means. the t disadvantages of using air mails, snail mails, thanks!
                            • By hostgator promo code - 9:18 AM on Apr 17, 2013   Reply
                              Nice article...!!! You put really very helpful information.
                              • By dog food - 7:39 PM on Apr 19, 2013   Reply
                                I've just learned about event listeners a month ago. I've been using them and I must say its really facilitates development work.
                                • By michael - 9:12 PM on May 09, 2013   Reply
                                  google is the king
                                  • By Visit this site - 10:18 PM on May 09, 2013   Reply
                                    It’s really glad to read about the following topic which has enhanced my knowledge regarding topic and plus has given a lot of ideas which I can think on. So I would say thank you to the blog owner for providing this amazing information.
                                    • By CHEAP MINECRAFT SERVER HOSTING - 2:10 PM on May 13, 2013   Reply
                                      I have to convey my respect for your kindness for all those that require guidance on this one field. Your special commitment to passing the solution up and down has been incredibly functional and has continually empowered most people just like me to achieve their dreams. Your amazing insightful information entails much to me and especially to my peers. Thanks a ton; from all of us. : <a href="http://www.cheapminecraftserverhosting.org/"> CHEAP MINECRAFT SERVER HOSTING </a>
                                      • By Soutien scolaire - 4:36 AM on May 14, 2013   Reply
                                        I had been actually greater than thrilled to find this web-site. I preferred to I cherish your effort and time using this outstanding read !! I surely taking pleasure in every single tiny spot of it and also I've you bookmarked to look at new things you post. Am I Equipped To just claim what a comfort to find out somebody who in truth knows what theyre speaking of on the web. And also check this: <a href="http://www.financeutile.com">investissement participatif</a> and <a href="http://mathsmalin.fr/exercices10/6,aires-et-perimetres.html">exercices aires et périmètres 6eme</a>.
                                        • By Harshia Agrwal - 7:49 AM on May 15, 2013   Reply
                                          Well, I guess this is one of the most attention drawing article I read after 2 days.
                                          Love your writing. I hope I will get same knowledge each time.
                                          • By online accounting degree programs - 12:40 PM on May 16, 2013   Reply
                                            There are also a number of home remedies that can tried to treat sinus infection. Here is a great article on home remedies for sinus infection.
                                            • By website borwali - 4:05 PM on May 16, 2013   Reply
                                              nice post for reading and learning
                                              • By Cộng Đồng Game - 4:46 AM on May 17, 2013   Reply
                                                its so useful, keep it up
                                                • By car audio forum - 10:50 PM on May 17, 2013   Reply
                                                  its so useful, keep it up
                                                  • By How Can I Stop Snoring Now - 10:53 PM on May 19, 2013   Reply
                                                    I am very much interested in reading articles. This blog was really an awesome site which I had never found it anywhere. Lots of stuff in this site ! Really helpful for most of them !
                                                    • By Comment Maigrir Vite - 1:11 PM on May 20, 2013   Reply
                                                      Mmm.. good to be here in your article or post, whatever, I think I should also work hard for my own website like I see some good and updated working in your site
                                                      • By Education - 11:08 AM on May 21, 2013   Reply
                                                        instead of listening to node/property added/removed we'll listen to resource changed, through the 3 topics :
                                                        • By FIFA 13 ultimate team coins - 7:04 AM on May 22, 2013   Reply
                                                          Package Boss currently demonstrates the bundle measurement, filters used, and contains a web link so that you can acquire this.
                                                          • By Old School RS Gold - 7:06 AM on May 22, 2013   Reply
                                                            handler that listens for all the repository, for those -very common- events, which will certainly be a performance killer. can share some details?
                                                            • By Junk Removal Vancouver - 8:51 PM on May 22, 2013   Reply
                                                              I was looking for crucial information on this subject. The information was important as I am about to launch my own portal. Thanks for providing a missing link in my business.
                                                              • By buy twitter followers - 10:47 PM on May 22, 2013   Reply
                                                                the article has provided me with a useful set of information on event handling. The details that you have added such as the 5 methods of implementation provided a good read. Keep sharing your views and ideas in this manner.

                                                                ADD A COMMENT