How to prevent concurrent execution of a job in Grails?

℡╲_俬逩灬. 提交于 2019-12-05 12:50:02

问题


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 dont't want to be executed while the previouse exection is not finished. How to configure this in grails?

(Of course the whole magic could be done with a static volatile flag, but is not a very elegant way to do this) (Also how can I configure for the job to be singleton?)

thx


回答1:


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 {

    static concurrent = false

    void execute() {
        println "Job run!"
    }
}



回答2:


Make your job class implement StatefulJob instead of Job




回答3:


For Quartz-based jobs concurrency is achieved using

static concurrent = false

in the job class.

Note the static in the definition, this is needed instead of def concurrent = false at least since version 2.0.12. See the Quartz documentation.




回答4:


At the and I implemented it without quartz, using spring tasks:

beans = { 
    xmlns task: "http://www.springframework.org/schema/task"

    task.'scheduler'('id':"myScheduler", 'pool-size':"1")

    task.'scheduled-tasks'('scheduler':"myScheduler") {
        task.'scheduled'(ref:"myBean", method:"myBeanMethodToExec", 'fixed-delay':5000)
    }
}

(pool-size 1 I think is even not necessary, but to be sure 100% ;))



来源:https://stackoverflow.com/questions/6453802/how-to-prevent-concurrent-execution-of-a-job-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!