How to make an event listener in nuxeo IDE

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
0 votes

1 answers

4298 views

ANSWER



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.html

https://github.com/nuxeo/nuxeo/blob/434093e97825bf7e02578efd428e71f588704f20/nuxeo-core/nuxeo-core-event/src/main/java/org/nuxeo/ecm/core/event/impl/ShallowEvent.java

Hope this helps!

-Y

0 votes



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
06/07/2016

Hey Parul,

Are you debugging? Maybe throw some print statements before your return statements? Or, try the for loop, or similar:

    for (Object arg : ctx.getArguments()) {

        if (arg instanceof DocumentModel) {
            DocumentModel doc = (DocumentModel) arg;

            String doctype = doc.getType( );

            System.out.println(&quot;*******&quot;+doctype);
       }
    }

Have you checked the server log for any warnings? That might be a good place to start.

-Yousuf

06/07/2016

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
06/08/2016

Hi Parul,

Going to your log directory in the nuxeo directory, do tail -f server.log (this command is your friend, you'll be using it a lot, trust me) from the command line in your terminal, assuming you're using Ubuntu.

Trigger the event on the platform that the listener is listening to, and simultaneously watch the output of the server log in your terminal window.

If you get any online services errors or warnings (or anything else for that matter), that is likely your problem, or one of them.

If you can, please post the errors or warnings you are getting, as it would make all of our lives easier.

Good luck,

06/08/2016