How to implement timeout with Quartz?

11,299

Because the java platform does not provide any way to stop a thread, Quartz does not provide any way to stop a job executing on a thread.

Jobs need to take care of themselves, as Quartz can have no idea what code is in their execute() method.

I'd suggest using System.currentTimeMillis() at the beginning of your job execute() method to record the current time, and then every time through your job's main loop use it again to get the current time. Look at the difference to see if your maximum time has past,and if so break out of your main loop and exit from the execute() method.

Share:
11,299
Jorge
Author by

Jorge

I'm a software developer with knowledge in C# javascript, Jquery .Net asp.net-mvc, web-api node.js Always interested in agile methodologies. My StackOverflow CV twitter

Updated on July 20, 2022

Comments

  • Jorge
    Jorge almost 2 years

    i'm trying to find the best way to implement a timeout with quartz but i want to know if this framework already contains a class or interface to do it. The timeout that need to implement it's because i want to know how long have been work the job, and take the desicion of turn off the job.

  • jhouse
    jhouse almost 13 years
    Also see the InterruptableJob interface. And the JobListener interface, which give you tools for adding some simple features to the framework that meet your needs.
  • Stefano
    Stefano over 3 years
    But if the job is one shot and without loop, How can I check that you said ?