Adobe CQ : Regarding Session in Event Listener

a 夏天 提交于 2020-01-02 09:19:10

问题


I have a question regarding event listener. We have a event listener which listen to delete node event and perform some activity say "send email".

While code review i found this, although this code is working fine i am not convinced with the session being handled here :

@Activate
protected void activate(ComponentContext context) {
try{
        final String path="/content/dam/";
       Session session = repository.loginAdministrative(repository.getDefaultWorkspace());
        observationManager = session.getWorkspace().getObservationManager();
        observationManager.addEventListener(this, Event.PROPERTY_REMOVED, path, true, null, null, true);
        checkOutProperty = OsgiUtil.toString(context.getProperties()
                .get(ASSET_LOCK_PROPNAME_UPDATE), ASSET_LOCK_PROPNAME_DEFAULT);
        if (session != null && session.isLive()) {
                session.save();
       }
    } catch (RepositoryException e) {
        if(LOG.isErrorEnabled()){
            LOG.error("Error Occured in activate method of Property Removed Listener class:" + e.getMessage());
        }
    }catch (Exception e) {
        if(LOG.isErrorEnabled()){
            LOG.error("Error Occured in activate method of Property Removed Listener class:"+e.getMessage());
        }
    }

}



@Deactivate
protected void deactivate(ComponentContext componentContext) {
    try {
        if (observationManager != null) {
            observationManager.removeEventListener(this);
        }
    } catch (RepositoryException e) {
        if(LOG.isErrorEnabled()){
            LOG.error("Error Occured " + e);
        }
    } catch (Exception e) {
        if(LOG.isErrorEnabled()){
            LOG.error(e.getMessage());
        }
    }
}

Questions:

  • Best practice would be to create session object private to this class and should be logout in deactivate method?
  • Once an event is added in Observation Manager, do we really need session object? I was expecting if we should logout from session there.

回答1:


EventListener are a bit cumbersome here. I fought many battles with JCR Sessions and Sling ResourceResolvers within them. The problem is, you need to keep the Session active as long as the Event Listener is active. So the only thing missing in your code is a logout on deactivate.

I created an AbstractEventListener which takes care of this and provides the following two methods and has two private members:

private Session session;
private ObservationManager observationManager;

protected void addEventListener(final EventListener eventListener,
        final int eventTypes, final String path, final String[] nodeTypes) {
    try {
        session = getRepositorySession();
        observationManager = session.getWorkspace().getObservationManager();
        observationManager.addEventListener(eventListener, eventTypes,
                path, true, null, nodeTypes, true);
    } catch (RepositoryException e) {
        LOGGER.error("Repository error while registering observation: ", e);
    }
}

protected void removeEventListener(final EventListener eventListener) {
    if (observationManager != null) {
        try {
            observationManager.removeEventListener(eventListener);
        } catch (RepositoryException e) {
            LOGGER.error(
                    "Repository error while unregistering observation: ", e);
        } finally {
            logoutSession(session);
        }
    }
}

And then in the actual EventListener I just call them:

protected void activate(ComponentContext context) {
    addEventListener(this, Event.PROPERTY_ADDED| Event.PROPERTY_CHANGED, "/content/mysite", null);
    }
}

protected void deactivate(ComponentContext componentContext) {
    removeEventListener(this);
}


来源:https://stackoverflow.com/questions/27533506/adobe-cq-regarding-session-in-event-listener

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!