You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by AE...@idc.com on 2010/02/02 11:04:26 UTC

Jackrabbit Observation with Spring

Hi All,

I am new to observation, although the repository supports observation, 
onEvent() is never invocked. Here're the details:

I created a Param class, and it's just a POJO with setters and getters 
hasing properties that each represents a parameter for: 
ObservationManager.addEventListener()

I careted an interface XListener that extends EventListener, and defined a 
method: Params getParameters().

Crated MyEventListener implements XListener and implements the 
getParameters() and onEvent()
getParameters() just populate a Param object (with NODE_ADDED) and return 
it to be used later to add *this* listener,
and onEvent() currently it only prints out that NEW NODE ADDED.

Then created a new class ListenerRigistrar and added it in the 
applicationContext.xml for Spring with a <constructor-arg> which is a set 
of XListeners.
Added a new bean for MyEventListener.

back to ListenerRegistrar, I iterate over the set of listeners and use the 
Params object embeded with the listener to call 
ObservationManager.addEventListener()

The problem is that the message NEW NODE ADDED is never been printed means 
onEvent() method is never invocked!!

Can anyone help me please?

Thank you in advance,
shre3y


Re: Jackrabbit Observation with Spring

Posted by Alexander Klimetschek <ak...@day.com>.
On Thu, Feb 4, 2010 at 13:06,  <AE...@idc.com> wrote:
> When I try to get the number of registered listeners with the same session
> before to return as you see below I also get -1!

This could also indicate that your listeners list is empty for some reason.

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: Jackrabbit Observation with Spring

Posted by AE...@idc.com.
Thank you very much Ben.
I am using now the way of how to add the listeners in the 
applicationContext.xml, and in any case I will need to add listeners 
externally, sure I will use the code you sent to me. I just was curious to 
know why the jcrTemplate was not able to add the listeners.

Have a nice day :-)


Re: Jackrabbit Observation with Spring

Posted by Ben Short <be...@benshort.co.uk>.
Here is the code.....

/**
 * @see org.springmodules.jcr.SessionFactory#getSession()
 */
public Session getSession() throws RepositoryException {
return addListeners(repository.login(credentials, workspaceName));
}


/**
 * Hook for adding listeners to the newly returned session. We have to treat
 * exceptions manually and can't reply on the template.
 *
 * @param session JCR session
 * @return the listened session
 */
protected Session addListeners(Session session) throws RepositoryException {
if (eventListeners != null && eventListeners.length > 0) {
Workspace ws = session.getWorkspace();
ObservationManager manager = ws.getObservationManager();
if (log.isDebugEnabled())
log.debug("adding listeners " + Arrays.asList(eventListeners).toString() + "
for session " + session);

for (int i = 0; i < eventListeners.length; i++) {
manager.addEventListener(eventListeners[i].getListener(),
eventListeners[i].getEventTypes(),
eventListeners[i].getAbsPath(), eventListeners[i].isDeep(),
eventListeners[i].getUuid(),
eventListeners[i].getNodeTypeName(), eventListeners[i].isNoLocal());
}
}
return session;
}

Seems odd that it adds the listeners to the OM every time a session is
requested.




On 4 February 2010 16:22, <AE...@idc.com> wrote:

> will do. Thank you once more for your help.
>
>
>

Re: Jackrabbit Observation with Spring

Posted by AE...@idc.com.
will do. Thank you once more for your help.



Re: Jackrabbit Observation with Spring

Posted by Ben Short <be...@benshort.co.uk>.
Maybe have a look at the jcrsessionfactory source code and see how it
registers the listeners.

On 2/4/10, AElshereay@idc.com <AE...@idc.com> wrote:
> Ben, that's really wonderful I got it working finally as I added the stuff
> you sent to the applicationContext.xml and removed the listenerRegistrar
> bean too. Thank you so much.
>
> But I am really wondering what was wrong with
> observationManager.addEventListener() method  in the ListenerRegistrar?
> what is the difference between setting the eventListeners to the
> sessionFactory and getting the session via jcrTemplate then calling
> observationManager.addEventListener()?
>
>

-- 
Sent from my mobile device

Re: Jackrabbit Observation with Spring

Posted by AE...@idc.com.
Ben, that's really wonderful I got it working finally as I added the stuff 
you sent to the applicationContext.xml and removed the listenerRegistrar 
bean too. Thank you so much.

But I am really wondering what was wrong with 
observationManager.addEventListener() method  in the ListenerRegistrar? 
what is the difference between setting the eventListeners to the 
sessionFactory and getting the session via jcrTemplate then calling 
observationManager.addEventListener()?


Re: Jackrabbit Observation with Spring

Posted by Ben Short <be...@benshort.co.uk>.
Sorry that should of been 'can add the event listener'

You could pass the JcrSessionFactory into your ListenerRegistrar and in the
init method get a session and add the listeners via it. Make sure you
implement a destroy method that logs out the session.

On 4 February 2010 15:05, Ben Short <be...@benshort.co.uk> wrote:

> Hi,
>
> Looking at the documentation [1] you have to add the Event Listener to the
> JcrSessionFactory bean.
>
> <bean id="sessionFactory" class="org.springmodules.jcr.JcrSessionFactory">
>   ...
>   <property name="eventListeners">
>    <list>
>     <bean class="org.springmodules.jcr.EventListenerDefinition">
>      <property name="listener">
>       <bean class="org.springmodules.examples.jcr.DummyEventListener"/>
>      </property>
>      <property name="absPath" value="/rootNode/someFolder/someLeaf"/>
>     </bean>
>    </list>
>   </property>
> </property>
>
>
> [1]
> https://springmodules.dev.java.net/docs/reference/0.8/html_single/#d0e5257
>
>
> On 4 February 2010 15:00, Alexander Klimetschek <ak...@day.com> wrote:
>
>> On Thu, Feb 4, 2010 at 12:27,  <AE...@idc.com> wrote:
>> >                jcrTemplate.execute(new JcrCallback() {
>> >            public Object doInJcr(final Session session) throws
>> RepositoryException {
>> >                ObservationManager observationManager =
>> session.getWorkspace().getObservationManager();
>>
>> I think this session will be closed immediately after execution by the
>> spring-modules JcrCallback. An observation listeners is only active as
>> long as it's session is open.
>>
>> Regards,
>> Alex
>>
>> --
>> Alexander Klimetschek
>> alexander.klimetschek@day.com
>>
>
>

Re: Jackrabbit Observation with Spring

Posted by Ben Short <be...@benshort.co.uk>.
Hi,

Looking at the documentation [1] you have to add the Event Listener to the
JcrSessionFactory bean.

<bean id="sessionFactory" class="org.springmodules.jcr.JcrSessionFactory">
  ...
  <property name="eventListeners">
   <list>
    <bean class="org.springmodules.jcr.EventListenerDefinition">
     <property name="listener">
      <bean class="org.springmodules.examples.jcr.DummyEventListener"/>
     </property>
     <property name="absPath" value="/rootNode/someFolder/someLeaf"/>
    </bean>
   </list>
  </property>
</property>


[1]
https://springmodules.dev.java.net/docs/reference/0.8/html_single/#d0e5257

On 4 February 2010 15:00, Alexander Klimetschek <ak...@day.com> wrote:

> On Thu, Feb 4, 2010 at 12:27,  <AE...@idc.com> wrote:
> >                jcrTemplate.execute(new JcrCallback() {
> >            public Object doInJcr(final Session session) throws
> RepositoryException {
> >                ObservationManager observationManager =
> session.getWorkspace().getObservationManager();
>
> I think this session will be closed immediately after execution by the
> spring-modules JcrCallback. An observation listeners is only active as
> long as it's session is open.
>
> Regards,
> Alex
>
> --
> Alexander Klimetschek
> alexander.klimetschek@day.com
>

Re: Jackrabbit Observation with Spring

Posted by AE...@idc.com.
When I try to get the number of registered listeners with the same session 
before to return as you see below I also get -1!

@PostConstruct
        public void register(){
                jcrTemplate.execute(new JcrCallback() {
            public Object doInJcr(final Session session) throws 
RepositoryException {
                ObservationManager observationManager =
                    session.getWorkspace().getObservationManager();
                for(XListener listener : listeners){
                                ListenerProperties listenerProperties = 
listener.getListenerProperties();
 
observationManager.addEventListener(listenerProperties.getEventListener(),
 listenerProperties.getEventTypes(),
 listenerProperties.getAbsPath(),
 listenerProperties.isDeep(),
 listenerProperties.getUuid(),
 listenerProperties.getNodeTypeName(),
 listenerProperties.isNoLocal());
                        }
                long numberOfRegisteredListeners = 
observationManager.getRegisteredEventListeners().getSize();
                // the value of numberOfRegisteredListeners is -1 even I 
still haven't return!!
                return null;
            }
        });
        }


Re: Jackrabbit Observation with Spring

Posted by Alexander Klimetschek <ak...@day.com>.
On Thu, Feb 4, 2010 at 12:27,  <AE...@idc.com> wrote:
>                jcrTemplate.execute(new JcrCallback() {
>            public Object doInJcr(final Session session) throws RepositoryException {
>                ObservationManager observationManager = session.getWorkspace().getObservationManager();

I think this session will be closed immediately after execution by the
spring-modules JcrCallback. An observation listeners is only active as
long as it's session is open.

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: Jackrabbit Observation with Spring

Posted by AE...@idc.com.
Hi Alexander,

Thank you very much for replying. Here I provide more details:

Here're the java classes:
-------------------------------------

public class ListenerProperties {
        private EventListener eventListener;
        private int eventTypes;
        private String absPath;
        private boolean isDeep;
        private String[] uuid;
        private String[] nodeTypeName;
        private boolean noLocal;
        //setters.. and getters..
}
**********************************************************
public interface XListener extends EventListener {
        ListenerProperties getListenerProperties();
}
**********************************************************
public class NodeAddedListener implements XListener {
        @Override
        public ListenerProperties getListenerProperties() {
                ListenerProperties listenerProperites = new 
ListenerProperties();
                listenerProperites.setEventListener(this);
                listenerProperites.setEventTypes(Event.NODE_ADDED);
                listenerProperites.setAbsPath("/");
                listenerProperites.setDeep(true);
                listenerProperites.setUuid(null);
                listenerProperites.setNodeTypeName(null);
                listenerProperites.setNoLocal(false);
                return listenerProperites;
        }
        @Override
        public void onEvent(EventIterator arg0) {
                System.out.println(" *************************");
                System.out.println("            ** New node created **");
                System.out.println(" *************************");
        }
}
**********************************************************
public class ListenerRegistrar {
        @Resource  private JcrTemplate jcrTemplate;
        private Set<XListener> listeners;
 
        public ListenerRegistrar(Set<XListener> listeners){
                this.listeners = listeners;
        }
 
        @PostConstruct
        public void register(){
                jcrTemplate.execute(new JcrCallback() {
            public Object doInJcr(final Session session) throws 
RepositoryException {
                ObservationManager observationManager =
                    session.getWorkspace().getObservationManager();
                for(XListener listener : listeners){
                                ListenerProperties listenerProperties = 
listener.getListenerProperties();
 
observationManager.addEventListener(listenerProperties.getEventListener(),
 listenerProperties.getEventTypes(), listenerProperties.getAbsPath(),
 listenerProperties.isDeep(), listenerProperties.getUuid(),
 listenerProperties.getNodeTypeName(), listenerProperties.isNoLocal());
                        }
                                // FYI Alexander, here I even tried to 
call observationManager.getRegisteredEventListeners().getSize(), thought
                                // maybe it's a session issue, then it 
always returned -1
                return null;
            }
        });
        }
}
**********************************************************
And I added the following to the applicationContext.xml:
---------------------------------------------------------------------------------

        <bean id="listenerRegistrar" 
                class="events.ListenerRegistrar">
                <constructor-arg>
                        <set>
                                <ref bean="nodeAddedListener"/>
                        </set>
                </constructor-arg>
        </bean>
 
        <bean id="nodeAddedListener"    class="events.NodeAddedListener"/>

**********************************************************


 
I used OCM to CRUD the items.
so I have MyItem object as following:

@Configurable
@Node(jcrMixinTypes = "mix:versionable")
public class MyItem {
        @NotNull @Field (path = true) private String path;
        @Field (uuid = true) private String uuid;
        @Field private String body;
        // setters and getters..
}
**********************************************************
@Repository
public class OcmBioRepositoryImpl implements OcmBioRepository {

        @Resource private JcrTemplate jcrTemplate;
        private Mapper mapper;
 
        @PostConstruct
        private void initializeMapper(){
                List<Class> classes = new ArrayList<Class>();
                classes.add(MyItem.class);
                this.mapper = new AnnotationMapperImpl(classes);
        }
 
        public void insert(final MyItem myItem){
                jcrTemplate.execute(new JcrCallback() {
            public Object doInJcr(final Session session) throws 
RepositoryException {
                ObjectContentManager ocm = new 
ObjectContentManagerImpl(session, mapper);
                ocm.insert(myItem);
                        ocm.save();
                        return null;
            }
        });
        }

 
***************************************************************************************

        Hope this is helpfull.
 
        Thank you in advance


Re: Jackrabbit Observation with Spring

Posted by Alexander Klimetschek <ak...@day.com>.
On Tue, Feb 2, 2010 at 09:04,  <AE...@idc.com> wrote:
> The problem is that the message NEW NODE ADDED is never been printed means
> onEvent() method is never invocked!!

Can you provide the code that does the event listener registration and
the code snippet you use to create content (to see the node structure
created/modified)?

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com