cancel
Showing results for 
Search instead for 
Did you mean: 

How to make an event listener in nuxeo IDE

Parul_Puri
Champ in-the-making
Champ in-the-making

I am trying to schedule an event in nuxeo IDE but facing error while creating event listener in IDE:

the scheduler contribution is:

<?xml version="1.0"?>

<component name="org.nuxeo.sample.ScheduleCall" version="1.0">
<extension target="org.nuxeo.ecm.core.scheduler.SchedulerService"
 point="schedule">
 <schedule id="testschedule">
 <username>Administrator</username>
 <eventId>my_user_created</eventId>
 <eventCategory>default</eventCategory>

 <cronExpression>0 1 * * * ?</cronExpression>
 </schedule>
 </extension>  
</component>

the listener contribution is:

<component name="org.nuxeo.sample.listener.contrib.ScheduleListener">

  <extension target="org.nuxeo.ecm.core.event.EventServiceComponent"
    point="listener">

    <listener name="schedulelistener" async="false" postCommit="false"
      class="org.nuxeo.sample.ScheduleListener" priority="100">
    
          <event>my_user_created</event>
         </listener>
  </extension>

</component>

The listener java class is:

package org.nuxeo.sample;



import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.NuxeoException;
import org.nuxeo.ecm.core.event.Event;
import org.nuxeo.ecm.core.event.EventContext;
import org.nuxeo.ecm.core.event.EventListener;
import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
public class ScheduleListener implements EventListener {

    public void handleEvent(Event event) throws ClientException {
    	try{
    		
    		
    		EventContext ctx = event.getContext();
            if (!(ctx instanceof DocumentEventContext)) {
                return;
            }
        
          
    		DocumentModel doc = ((DocumentEventContext)ctx).getSourceDocument();
    		if(doc==null){
    			return;
    		}
    		
    		String doctype = doc.getType();
    		
    		System.out.println("*******"+doctype);
    		
       }
    	catch (Exception e){
    		e.printStackTrace();
    		
    	}
    }
    

}



The error is:

2016-06-03 11:47:01,005 ERROR [Quartz_Worker-1] [org.nuxeo.ecm.core.event.impl.EventServiceImpl] Exception during timezoneSelector sync listener execution, continuing to run other listeners
java.lang.ClassCastException: org.nuxeo.ecm.core.event.impl.EventContextImpl cannot be cast to org.nuxeo.ecm.core.event.impl.DocumentEventContext
	at org.nuxeo.ecm.user.center.profile.localeProvider.UserLocaleSelectorListener.handleEvent(UserLocaleSelectorListener.java:43)
	at org.nuxeo.ecm.core.event.impl.EventServiceImpl.fireEvent(EventServiceImpl.java:200)
	at org.nuxeo.ecm.core.scheduler.EventJob.execute(EventJob.java:119)
	at org.nuxeo.ecm.core.scheduler.EventJob.execute(EventJob.java:65)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
2016-06-03 11:47:01,008 WARN  [Nuxeo-Work-default-4] [org.nuxeo.ecm.platform.ec.notification.NotificationEventListener] Can not handle notification on a event that is not bound to a DocumentEventContext
5 REPLIES 5

Yousuf_Nejati
Star Contributor
Star Contributor

Have you investigated this further?

java.lang.ClassCastException: org.nuxeo.ecm.core.event.impl.EventContextImpl cannot be cast to org.nuxeo.ecm.core.event.impl.DocumentEventContext

I believe your problem is here, as I'm sure you are aware:

 DocumentModel doc = ((DocumentEventContext)ctx).getSourceDocument();

Seems as though you should be specifying an index because the event data is exposed as a list of arguments. Does that many any sense?

See the source code:

http://community.nuxeo.com/api/nuxeo/5.4/javadoc/org/nuxeo/ecm/core/event/EventContext.html

http://community.nuxeo.com/api/nuxeo/5.4/javadoc/org/nuxeo/ecm/core/event/impl/DocumentEventContext....

https://github.com/nuxeo/nuxeo/blob/434093e97825bf7e02578efd428e71f588704f20/nuxeo-core/nuxeo-core-e...

Hope this helps!

-Y

Thank you, yes the error lies on this line only. My IDE wasn't appropriately configured so now this error is gone but I am unable to get any output now. I just want to see if the output is printed after regular intervals. Please let me know if you can help with this error

Hey Parul,

The server log says there is an error with calling nuxeo online services but I don't know if it has anything to do with my code

Hi Parul,

Getting started

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.