问题
I have configured quartz in my application. It works on cluster and i can see cluster nodes are triggering jobs one at a time. However, i have a question about how load balancing algorithm works in quartz. I want to know at what point in jdbcJobstore code, a node get information that it can not acquire a lock.How a particular cluster node acquire a lock to trigger a job in code?
回答1:
Quartz uses no explicit load-balancing. What happens is that individual clustered Quartz scheduler instances compete to execute jobs. When the time comes and a job should be executed, individual scheduler instances attempt to create a DB row lock in the QRTZ_LOCKS table in the configured job store database. The instance that manages to create the DB row lock wins and executes the job. The other scheduler gets an SQL exception and gives up.
As you can deduce, load-balancing in Quartz is very simple as nodes with higher load are "somewhat less likely" to be the first ones to successfully create the DB lock. This obviously assumes the time is properly synchronized on all clustered Quartz instances.
If you are interested in how the DB row locking works in Quartz, you can check the DBSemaphore API.
来源:https://stackoverflow.com/questions/30521134/quartz-clustering-load-balancing-algorithm-internal-implementation-in-jdbcjobsto