Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Event handler in CQ5

Avatar

Level 4

Hi,

My requirement is whenever i create a new page CQ has to add the new property.

so i prefered Even handlers and followed the below code.

The problem is i dont know weather the code is running?

i have checked the console i didn't get any log as well.

could anyone please help me and explain how event handlers running in CQ?

 

@Component public class ExampleObservation implements EventListener {  Logger log = LoggerFactory.getLogger(this.getClass());  private Session adminSession;  @Reference  SlingRepository repository;  @Activate  public void activate(ComponentContext context) throws Exception {  log.info("activating ExampleObservation");  try {    adminSession = repository.loginAdministrative(null);    adminSession.getWorkspace().getObservationManager().addEventListener(
      this, //handler
      Event.PROPERTY_ADDED|Event.NODE_ADDED, //binary combination of event types
      "/content/", //path
      true, //is Deep?
      null, //uuids filter
      null, //nodetypes filter       false);   } catch (RepositoryException e){   log.error("unable to register session",e);   throw new Exception(e);  } }
@Deactivate public void deactivate(){  if (adminSession != null){   adminSession.logout();  } }   public void onEvent(EventIterator eventIterator) {   try {     while (eventIterator.hasNext()){
      log.info("something has been added : {}", eventIterator.nextEvent().getPath());
    }    } catch(RepositoryException e){    log.error("Error while treating events",e);   }  } }
1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Michael,

if you just want to add a default property to a page when it's created, you can also just add it to the template node. Feike wrote a blog post [1], which shows how you can add default components to a page. But this approach works also if you just want to add a single default property.

Jörg

[1] http://experiencedelivers.adobe.com/cemblog/en/experiencedelivers/2012/06/default-components-in-your...

View solution in original post

6 Replies

Avatar

Level 10

In your OSGi bunlde -- you should not be using 

adminSession = repository.loginAdministrative(null);

to create a Session.

The preferred way is to inject a Sling ResourceResolverFactory:

//Inject a Sling ResourceResolverFactory
@Reference
private ResourceResolverFactory resolverFactory;
 

//Queries the AEM JCR for customer data and returns

//the data within an XML schema   
public String getCustomerData(String filter) {

Customer cust = null;

List<Customer> custList = new ArrayList<Customer>();
try {
           
    //Invoke the adaptTo method to create a Session used to create a QueryManager
    ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
    session = resourceResolver.adaptTo(Session.class);

To learn how to use this code- see:
http://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

2nd -- are you referring to an online doc to learn how to use Event hanlders for AEM in an OSGi bundle? If so - can you provide the URL?  
 

 

Avatar

Level 8

If you navigate to http://localhost:4502/system/console/components - do a find on the page for "ExampleObservation" and you should see it listed.

Please see the attached image for reference.

Avatar

Level 4

Hi Lee,

Thanks a lot for ur quick response.

Regards,

Mike

Avatar

Correct answer by
Employee Advisor

Michael,

if you just want to add a default property to a page when it's created, you can also just add it to the template node. Feike wrote a blog post [1], which shows how you can add default components to a page. But this approach works also if you just want to add a single default property.

Jörg

[1] http://experiencedelivers.adobe.com/cemblog/en/experiencedelivers/2012/06/default-components-in-your...

Avatar

Level 4

Hi Jorg,

Thanks a lot for your reply. its really helpful for me.

Regards,

Mike