Max_Salah
Max_Salah

Reputation: 2497

sling eventhandler reacts only one time when a page is activated

I use the following code to log some info, when some cq5 page is activated.

@Component
@Service
@Property(name="event.topics",value= {ReplicationAction.EVENT_TOPIC})
public class ExampleEventHandler implements EventHandler {
 Logger log = LoggerFactory.getLogger(this.getClass());
 public void handleEvent(Event event) {
 ReplicationAction action = ReplicationAction.fromEvent(event);

   if(action.getType().equals(ReplicationActionType.ACTIVATE)){

   log.info("test Event handler")
  }
 }
}

Now when I activate the page, this handler reacts and logs the string "test Event handler". Now when I activate the page second or third time, the Event handler does not react. I have to restart Service ExampleEventHandler from Felix console, sothat it react again (one time).

How to configure the eventhandler (or the ReplicationAction), sothat the ExampleEventHandler reacts everytime, when the page is activated whithout Need of restart the Service ExampleEventHandler everytime?

Upvotes: 2

Views: 1944

Answers (2)

Abhinav
Abhinav

Reputation: 21

Or in felix console, you can add your package namespace "com.yourpackage*" to the whitelisted list of packages. This will ensure that event handlers in your package are never blacklisted by event admin service.

Upvotes: 1

Tomek Rękawek
Tomek Rękawek

Reputation: 9304

I have installed your component in my CQ5 instance and it works perfectly fine, every time. However, if you do some complex, time-consuming things in your event handler (instead of simply logging events) it may get banned by the Apache Felix Event Admin. By default, it will happen if your handleEvent() runs longer than 5 seconds.

In this case you should use JobUtil.processJob() method to pass the event to the JobProcessor:

@Component
@Service(value = EventHandler.class)
@Property(name = EventConstants.EVENT_TOPIC, value = { ReplicationAction.EVENT_TOPIC })
public class Test implements EventHandler, JobProcessor {

    private static final Logger LOG = LoggerFactory.getLogger(Test.class);

    @Override
    public void handleEvent(Event event) {
        ReplicationAction action = ReplicationAction.fromEvent(event);
        if (action.getType().equals(ReplicationActionType.ACTIVATE)) {
            JobUtil.processJob(event, this);
        }
    }

    @Override
    public boolean process(Event job) {
        LOG.info("Do some complex logic here");
        return true;
    }
}

Upvotes: 2

Related Questions