Cron event handler - synchronous or asynchronous
Hello - I have created an event handler for a scheduled event. The handler will perform a long-running operation. Looking through the Nuxeo code, I see various classes implementing AbstractWork for background processing. My question is: should I consider using the AbstractWork code? Or – will the event already be running on it's own thread by virtue of the fact that it was invoked according to a schedule?
A scheduled event should be managed by an asynchronous listener.
If you use a synchronous listener, it will execute in the thread of the Quartz process dealing with the scheduling of that event, and such threads are on purpose kept in very small numbers (to avoid consuming unneeded resources — there's actually just one thread in the default platform, see templates/common/config/quartz.properties
). If you write such a synchronous listener for a scheduled event then it should execute very quickly, but if it needs to it can also do asynchronous Work by passing a subclass of AbstractWork
to the WorkManager
method schedule
.
But directly writing an asynchronous listener (which is implemented using a subclass of AbstractWork
itself) is simpler.
org.nuxeo.ecm.core.scheduler.TestSchedulerService.testScheduleRegistration
is one such test.nuxeo-core/nuxeo-core-event/src/test/resources/OSGI-INF/test-scheduler-config.xml