Wim Van den Brande
Wim Van den Brande

Reputation: 357

JCR - EventListener - activate method not found - Adobe CQ 5.6.0

I'm trying to implement an EventListener to propagate some JCR changes (property changed, node added ...).

This topic has been discussed in a number of posts but different implementations are available and I'm inclinded to describe the nitty gritty details on the required annotation as a very well kept secret.

SlingRepository is annotated without specific properties in following document: http://experiencedelivers.adobe.com/cemblog/en/experiencedelivers/2012/04/event_handling_incq.html

and Component annotation has been set the following way:

@Component
public class JCRListener implements EventListener{
}

With this flavor, I'm able to install my bundle from the Adobe CQ Web Console but my Active and Deactive methods are not triggered.

SlingRepository is annotated with specific properties (policy = ReferencePolicy.STATIC, cardinality = ReferenceCardinality.MANDATORY_UNARY) in following document: https://groups.google.com/forum/#!topicsearchin/day-communique/postEvent%7Csort:date%7Cspell:true/day-communique/BvJsgMzpsAM

and Component annotation has a specific setting

@Component(immediate=true)
@Service
public class JCRListener implements EventListener{
    @Activate
    public void activate(ComponentContext componentContext){
      .... 
    } 
    @Deactivate
    public void deactivate(ComponentContext componentContext){
      .... 
    } 
    public void onEvent(EventIterator events) {
     .... 
    }
} 

With this flavor, the deployment of my bundle fails and I'm getting the error [com.adobe.cq.JCRListener] activate method [activate] not found; Component will fail

Any help is very much appreciated.

BTW, does someone know the status of the adobe forum site (http://help-forums.adobe.com/content/adobeforums/en.html)??? We are inclined to believe the web site is not often available ....

Wim

Upvotes: 0

Views: 1345

Answers (2)

Wim Van den Brande
Wim Van den Brande

Reputation: 357

Was able to resolve this problem. The signature of the activate and deactivate (or start and stop) methods (identified by the Activate and Deactivate annotation keyword) should correspond to the signature Felix (being the open source OSGI container) likes or agrees with.

For me and AFAIK only these signatures are CORRECT

@Activate
public void start() {
   .... add your Event Listeners 
} 

@Deactivate
public void stop() {
   .... remove your Event Listeners 
} 

and following signature is INCORRECT:

@Activate
public void start(ComponentContext ctx) {
   .... add your Event Listeners 
} 

@Deactivate
public void stop(ComponentContext ctx) {
   .... remove your Event Listeners 
} 

Be aware of the warning you get when compiling the bundle. If your siognatures are wrong you'll likely end up with following warning:

[WARNING] @Component: Lifecycle method start has wrong argument .... at Java annotations in com.adobe.cq.JCRListenerImp:25

Please refer to the CORRECT implementation of a JCR EventListener documented quite well in following post: http://cqdump.wordpress.com/2012/11/13/cq-coding-patterns-sling-vs-jcr-part-2/

I hope this answer will be helpful for many other people.

regards,

Wim

Upvotes: 1

Bertrand Delacretaz
Bertrand Delacretaz

Reputation: 6100

Your code looks correct but importing the wrong Activate annotations class might cause this error.

http://markmail.org/message/eiqcgnckgd4mfbw7 has some info about how to troubleshoot this.

You might also want to compare your code and bundle with a known good example such as Sling's ThumbnailGenerator sample.

Upvotes: 0

Related Questions