timeReactivates a Scheduler Job written in Groovy on code change
Developed by

Clemens Robbenhaar

0 Votes
LicenseGNU Lesser General Public License 2.1


Note: this is only meant for development. Do not use it in production!

When writing a Scheduler Job in Groovy one needs to deactivate and reactivate the job every time one saves the corresponding page. Otherwise the code from the last version of the page is used.

This can be done with two clicks in the Scheduler UI, but it is also possible to automatize this.

For this create a page somewhere, and add the following objects in the object editor:

One object of class XWiki.ComponentClass with the following fields.

Component Role Type
Component Role Hint
Component Scope
Current Wiki    (or whatever you prefer)

Three objects of class  XWiki.ComponentMethodClass with the following fields:

Method name
Method body code
import org.xwiki.bridge.event.*

xcontext.method.output.value = [new DocumentUpdatedEvent()]
Method name
Method body code
xcontext.method.output.value = "schedulerJobReinitOnSave"
Method name
Method body code
// avoid getting called twice:
if (xcontext.request.getAttribute('schedulertaskreinit running') == null) {
  xcontext.request.setAttribute('schedulertaskreinit running', true)

 def updatedDoc = xcontext.method.input.get(1)
 def jobInfo = updatedDoc.getObject('XWiki.SchedulerJobClass')

 if (jobInfo != null && xwiki.scheduler.getJobStatus(jobInfo).getValue() == 'Normal') {
   if (xwiki.scheduler.getJobStatus(jobInfo).getValue() == 'Normal') {
      System.err.println('rescheduled ' + updatedDoc.getFullName())
   } else {
      System.err.println('ERROR when rescheduling ' + updatedDoc.getFullName() + ' status is now ' + xwiki.scheduler.getJobStatus(jobInfo).getValue())

Afterwards every time you save your job page you should see a message like:

rescheduled MyApplication.Code.MyFancyJob


Prerequisites & Installation Instructions

Creation and editing of the page must be done with a user with programming rights.

You can also download the XAR and import it into your wiki.


Get Connected