Adobe CQ:关于事件侦听器中的会话 [英] Adobe CQ : Regarding Session in Event Listener

查看:91
本文介绍了Adobe CQ:关于事件侦听器中的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对事件监听器有疑问。我们有一个事件侦听器,它侦听删除节点事件并执行一些活动,例如发送电子邮件。

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());
        }
    }
}

问题:


  • 最佳做法是创建该类私有的会话对象,并应在deactivate方法中注销?

  • 一次在观察管理器中添加了一个事件,我们真的需要会话对象吗?我在期待是否应该从那里的会话注销。

推荐答案

EventListener在这里有点麻烦。我与其中的JCR会话和Sling ResourceResolvers进行了许多战斗。问题是,只要事件监听器处于活动状态,就需要保持会话处于活动状态。因此,您的代码中唯一缺少的是注销时注销。

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.

我创建了一个AbstractEventListener,它负责此工作,并提供以下两种方法并具有两个私有成员:

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);
        }
    }
}

然后在实际的EventListener中我只是称呼他们为他们:

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);
}

这篇关于Adobe CQ:关于事件侦听器中的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆