Search code examples
quartz-schedulerseam

Asynchronous method in seam always return null QuartzTriggerHandle ?


QuartzTriggerHandle object that returned by Asynchronous method in Seam always 'null', the job starts but cann't cancelled or paused.

In, seam forum i found the next example that should be work,but it doesn't work with me.

@Name("quartzObserver")
public class SCSQuartzObserver {

    @In(create = true)
    SCSQuartzTask quartzTask;

    @SuppressWarnings("unused")
    @Observer("org.jboss.seam.postInitialization")
    public void observe() {
        try {
            Calendar cal = Calendar.getInstance();
            cal.set(2040, Calendar.MAY, 10);
            QuartzTriggerHandle handle = quartzTask.performTask(new Date(),
                    86400000l);
            handle.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Name("quartzTask")
@AutoCreate
public class SCSQuartzTask {
    @Asynchronous
    public QuartzTriggerHandle performTask(@Expiration java.util.Date when,
            @IntervalDuration long duration) {
        // do stuff
        QuartzTriggerHandle handle = new QuartzTriggerHandle("SCSQuartzTask");
        return handle;
    }
}

thnx for help.


Solution

  • You shouldn't create the QuartzTriggerHandle. Just do your work in the body of the performTask method, seam runtime will take care to return the QuartzTriggerHandle object. Something like this:

    @Asynchronous
    public QuartzTriggerHandle performTask(@Expiration java.util.Date when,
            @IntervalDuration long duration) {
        // do stuff
        return null;
    }
    

    The QuartzTriggerHandle is serializable, you can keep it in a database table so you can later cancel the task.