Email: dev@concena.com       Twitter: @dbrucegrant
«Back

Nuxeo 5.6 - One Time Event Trigger example

Four parts to creating and using a one-time event (leveraging the Nuxeo SchedulerService):

1. Register a 'scheduled' event - in this case the event will fire in 10 minutes time.  The event that fires is called myNuxeoEvent in this sample.

    public class FireInTenMinutes {

    public void task() {
        
        SchedulerService service;
        try {
            service = Framework.getService(SchedulerService.class);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        
        ScheduleImpl schedule = new ScheduleImpl();
        schedule.cronExpression = "0 0/10 * * * ?";
        schedule.id = "test1";
        schedule.username = "Administrator";
        schedule.eventId = "myNuxeoEvent";
        schedule.eventCategory = "default";
        service.registerSchedule(schedule);
        
    }

}

2. Create a listener to respond the event created above. In this example all I am doing is wirting to the log when the event fires. Since we want this to be a one-time event, I unregister it (again using the SchedulerService) once the event is handled.

    public void handleEvent(Event event) throws ClientException {
        
        if (!("myNuxeoEvent".equals(event.getName()))) {
            return;
        }
        
        log.info("*** Fired in 10 minutes! ***");
        
        // remove the one-time event after it fires
        SchedulerService service;
        try {
            service = Framework.getService(SchedulerService.class);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        service.unregisterSchedule("test1");
    }

3. I have to register the listener in a contribution file.

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

         <listener name="test1" async="false" postCommit="false"
            class="com.concena.mckellar.coreapp.delete.TenMinuteListener" priority="135">
            <event>myNuxeoEvent</event>
        </listener>  

         etc...

4. And finally, you'll need to initiate the event from your existing code. In my example I add two lines to an existing event handler.

    protected void handleTrashCompactorEvent(Event event) {
        try {
            RepositoryManager repoMgr = Framework.getService(RepositoryManager.class);
            Repository repo = repoMgr.getDefaultRepository();
            RemoveDocuments removeDocs = new RemoveDocuments(repo.getName());
            removeDocs.runUnrestricted();
        } catch (Exception e) {
            log.error("*** Error removing documents in trash *** error: " + e.getMessage());
        }

        // fire test event in 10 minutes
        FireInTenMinutes fireInTen = new FireInTenMinutes();
        fireInTen.task();

That's all there is to it!

Comments
Trackback URL:

No comments yet. Be the first.

Recent Entries Recent Entries

RSS (Opens New Window)
Showing 1 - 5 of 15 results.
of 3