EventListener event never triggered

Avatar

Avatar

Jdruwe

Avatar

Jdruwe

Jdruwe

19-01-2018

I am trying to hook to the rollout event of a page to a live copy. I am experimenting with the EvenListener but I am unable to get it to work. This is my code:

@Component(metatype = true)

@Service

public class MyCustomListener implements EventListener {

private static final Logger LOG = LoggerFactory.getLogger("logger");

@Reference

private ResourceResolverFactory resolverFactory;

private Session session;

private ObservationManager observationManager;

public void run() {

LOG.info("Running...");

}

//Place app logic here to define the AEM Custom Event Handler

protected void activate(ComponentContext ctx) {

try {

//Invoke the adaptTo method to create a Session

ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(AutoClosableResourceResolverFactory.getCredentials());

session = resourceResolver.adaptTo(Session.class);

observationManager = session.getWorkspace().getObservationManager();

final String[] types = {"cq:Page", "nt:unstructured"};

final String path = "/"; // define the path

observationManager.addEventListener(this, Event.NODE_ADDED, path, true, null, null, false);

LOG.info("Observing property changes to {} nodes under {}", Arrays.asList(types), path);

} catch (Exception e) {

LOG.error("unable to register session", e);

}

}

protected void deactivate(ComponentContext componentContext) throws RepositoryException {

if (observationManager != null) {

observationManager.removeEventListener(this);

}

if (session != null) {

session.logout();

session = null;

}

}

@Override

public void onEvent(EventIterator eventIterator) {

System.out.println();

}

}

My debug point in the onEvent method never gets called. Am I doing something wrong?

NOTES:

- my session or resource resolver are NOT null.

- AEM 6.3.1.0

Replies

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

19-01-2018

You are using Felix SRC annotations. Look at the SimpleResourceListener that uses DS annotations here - Creating an Adobe Experience Manager 6.3 Project using Adobe Maven Archetype 12

Avatar

Avatar

Ratna_Kumar

MVP

Avatar

Ratna_Kumar

MVP

Ratna_Kumar
MVP

19-01-2018

Hi,

We got this working as Scott mentioned above that we are using SimpleResourceListener class that uses DS annotations.

We will be releasing this article on early next week i.e., Monday or Tuesday!!

Watch this thread for more updates.

Thanks,

Ratna

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

19-01-2018

Avatar

Avatar

Jdruwe

Avatar

Jdruwe

Jdruwe

21-01-2018

Avatar

Avatar

cquser1

Avatar

cquser1

cquser1

21-01-2018

Hi,

In 6.3 we have an event listener working fine. Few thoughts on what we have done, in case it helps :

We have @Component, @Properties & @Service Annotations being used.

@Service

public class Sample implements ResourceChangeListener {

@Reference

private ResourceResolverFactory resourceResolverFactory;

protected ResourceResolver resourceResolver;

public void onChange(List<ResourceChange> changes) {

// Get the session object from the service user and the required logic.

}

Avatar

Avatar

Jdruwe

Avatar

Jdruwe

Jdruwe

21-01-2018

It works but only if I use the following code to retrieve a session:

observationSession = repository.loginAdministrative(null);

If I use the following my onEvent method is never called:

ResourceResolver serviceResourceResolver = resolverFactory.getServiceResourceResolver(AutoClosableResourceResolverFactory.getCredentials());

Any ideas? NOTE: we use the service user for our entire application (it has all rights)

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

22-01-2018

Seems to be a bug - i would whitelist the bundle and use that call. I see the same behaviour.

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

942

Correct Reply

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

942

Correct Reply

1.0K
Jörg_Hoh
Employee

22-01-2018

Is your code picking up the correct service user?

Avatar

Avatar

Jdruwe

Avatar

Jdruwe

Jdruwe

22-01-2018

It is, we use the same method all over our code-base to modify all kind of node structures. smacdonald2008 seems to have the same issue.