Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Help troubleshooting EventListener

Avatar

Avatar
Springboard
Level 3
jkpanera
Level 3

Likes

18 likes

Total Posts

107 posts

Correct Reply

2 solutions
Top badges earned
Springboard
Establish
Validate 10
Validate 1
Ignite 5
View profile

Avatar
Springboard
Level 3
jkpanera
Level 3

Likes

18 likes

Total Posts

107 posts

Correct Reply

2 solutions
Top badges earned
Springboard
Establish
Validate 10
Validate 1
Ignite 5
View profile
jkpanera
Level 3

31-07-2019

Hi guys,

I'm trying to implement a javax.jcr.observation.EventListener:

import com.google.common.collect.Lists;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import java.util.List;


@Component(immediate=true,
   service= EventListener.class)

public class KeyValueChangeListener implements EventListener{

  Logger log = LoggerFactory.getLogger(this.getClass());

  private Session adminSession;

  @Reference
  SlingRepository repository;

  @Reference
  ResourceResolverFactory resourceResolverFactory;

  @Activate
  public void activate(ComponentContext context) throws Exception {

   log.info("activating Observation");
  try {

   //adminSession = repository.loginAdministrative(null);
   ResourceResolver resourceResolver = null;
   adminSession = repository.loginService("service-name", null);
   adminSession.getWorkspace().getObservationManager().addEventListener(

   this, //handler
   Event.PROPERTY_ADDED|Event.PROPERTY_CHANGED|Event.PROPERTY_REMOVED, //binary combination of event types
   "/content/service/keys", //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 {

  List<Event> events = Lists.newArrayList(eventIterator);
   Object beforeValue, afterValue;
  for (Event event : events){

   switch (event.getType()) {

   case Event.PROPERTY_ADDED:

  afterValue = event.getInfo().get("afterValue");
   log.info("Added property: [{}] with value: [{}]", event.getIdentifier(), afterValue);
  break;
  case Event.PROPERTY_CHANGED:

  beforeValue = event.getInfo().get("beforeValue");
   afterValue = event.getInfo().get("afterValue");
   log.info("Changed property: [{}] from value: [{}] to value [{}]", event.getIdentifier(), beforeValue, afterValue);
  break;
  case Event.PROPERTY_REMOVED:

  beforeValue = event.getInfo().get("beforeValue");
   log.info("Removed property: [{}] had value: [{}]", event.getIdentifier(), beforeValue);
  break;
   }

  }

  } catch(RepositoryException e){

   log.error("Error while treating events",e);
   }

  }

}

but it's being flakey. Sometimes I'm getting the events I expect. Sometimes not. I enabled debug logging for javax.jcr.observation but nothing is logged.

Does anyone see what I'm doing wrong? Any help?

What package should I use to see debugging info for these events?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Springboard
Level 3
jkpanera
Level 3

Likes

18 likes

Total Posts

107 posts

Correct Reply

2 solutions
Top badges earned
Springboard
Establish
Validate 10
Validate 1
Ignite 5
View profile

Avatar
Springboard
Level 3
jkpanera
Level 3

Likes

18 likes

Total Posts

107 posts

Correct Reply

2 solutions
Top badges earned
Springboard
Establish
Validate 10
Validate 1
Ignite 5
View profile
jkpanera
Level 3

31-07-2019

So it seems that the events have been going through reliably, but the Java debugger does not reliably stop in the onEvent method. Strange.

The packages org.apache.jackrabbit.oak.jcr and org.apache.jackrabbit.commons have some good logging info for this situation.

Answers (0)