How to prevent concurrent execution of a job in Grails?

后端 未结 4 1200
长情又很酷
长情又很酷 2021-02-19 05:12

I have a quartz job in grails, that needs to be executed in every 5s, but I need this sequentially. In some situations the execution of the job exceeds this 5s, in this case I d

4条回答
  •  我寻月下人不归
    2021-02-19 05:52

    Assuming that you're using the grails quartz plugin, you should just be able to set the concurrent property of your job class to false.

    From the Quartz Plugin Documentation:

    "By default Jobs are executed in concurrent fashion, so new Job execution can start even if previous execution of the same Job is still running. If you want to override this behavior you can use 'concurrent' property, in this case Quartz's StatefulJob will be used"

    In more recent versions of the quartz plugin (version 2.0.13 for Grails 3.3.*), that would look like this:

    class MyJob {
    
        static concurrent = false
    
        void execute() {
            println "Job run!"
        }
    }
    

    For older versions of grails/quartz, it would look similar, exception that properties were set with def instead of static:

    class MyJob {
    
        def concurrent = false
    
        void execute() {
            println "Job run!"
        }
    }
    

提交回复
热议问题