Session Adobe CQ:关于事件侦听器中的会话

Session Adobe CQ:关于事件侦听器中的会话,session,aem,jcr,sling,Session,Aem,Jcr,Sling,我有一个关于事件侦听器的问题。我们有一个事件监听器,它监听删除节点事件并执行一些活动,比如“发送电子邮件” 在代码审查时,我发现了这一点,尽管这段代码运行良好,但我不相信这里正在处理的会话: @Activate protected void activate(ComponentContext context) { try{ final String path="/content/dam/"; Session session = repository.loginAdm

我有一个关于事件侦听器的问题。我们有一个事件监听器,它监听删除节点事件并执行一些活动,比如“发送电子邮件”

在代码审查时,我发现了这一点,尽管这段代码运行良好,但我不相信这里正在处理的会话:

@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方法中注销
  • 一旦在Observation Manager中添加了事件,我们真的需要会话对象吗?我在想我们是否应该从那里注销

    • EventListener在这里有点麻烦。我用JCR会话和其中的资源解析程序打了很多仗。问题是,只要事件侦听器处于活动状态,就需要保持会话处于活动状态。因此,您的代码中唯一缺少的是注销停用

      我创建了一个AbstractEventListener,它负责这一点,并提供以下两个方法和两个私有成员:

      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中,我只调用它们:

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