コミュニティアチーブメントバーを展開する。

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

この会話は、活動がないためロックされています。新しい投稿を作成してください。

解決済み

Question Regarding Session in Event Listener

Avatar

以前のコミュニティメンバー
Hi All,
 
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.

 

Regards,
D

1 受け入れられたソリューション

Avatar

正解者
Employee Advisor

Hi,

to actually use JCR observation the session, in which you register the listener, must be alive and active. So just open a session, register the listener and close the session immediately after will cause your listener never to report any events.

See [1] for an example.

kind regards,
Jörg

[1] https://cqdump.wordpress.com/2012/11/13/cq-coding-patterns-sling-vs-jcr-part-2/

元の投稿で解決策を見る

3 返信

Avatar

正解者
Employee Advisor

Hi,

to actually use JCR observation the session, in which you register the listener, must be alive and active. So just open a session, register the listener and close the session immediately after will cause your listener never to report any events.

See [1] for an example.

kind regards,
Jörg

[1] https://cqdump.wordpress.com/2012/11/13/cq-coding-patterns-sling-vs-jcr-part-2/

Avatar

Level 10

Best practice would be to logout in the activate method itself having finally block for the try block so that you dont have to wait till the deactivation of the component.

Avatar

以前のコミュニティメンバー

Thanks Jorg ! Your blogs on coding pattern JCR Vs Sling was very helpful.

I am not sure but any update or follow-up article on CRUD operation support withing Sling would be really great !!