user9421031
user9421031

Reputation: 11

How to Restrict Quartz Job to run only once even application is in multiple nodes?

There is QuartzJob in spring boot scheduled to run couple of times a Day, to send out report which generated from Data Base.

My application is in 4 servers. 4 reports being generated for each schedule.

I want to restrict that and send a single report.

I cannot send only from particular node.

Upvotes: 0

Views: 3772

Answers (1)

Cata
Cata

Reputation: 585

One option is clusterize your configuration. Basically, clustering only works with the JDBC-Jobstore (JobStoreTX or JobStoreCMT), and essentially works by having each node of the cluster share the same database.

Load-balancing occurs automatically, with each node of the cluster firing jobs as quickly as it can. When a trigger’s firing time occurs, the first node to acquire it (by placing a lock on it) is the node that will fire it.

Only one node will fire the job for each firing. What I mean by that is, if the job has a repeating trigger that tells it to fire every 10 seconds, then at 12:00:00 exactly one node will run the job, and at 12:00:10 exactly one node will run the job, etc. It won’t necessarily be the same node each time - it will more or less be random which node runs it.

Source: http://www.quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigJDBCJobStoreClustering.html

Here are examples of the configuration: 1 - https://github.com/davidkiss/spring-boot-quartz-demo 2 - http://www.opencodez.com/java/quartz-scheduler-with-spring-boot.htm

At this point, you will have just one node processing your report. If you need to process the information from all of the nodes, you can request from that node the report data to the others nodes and mix them. All the nodes will have the same logic, so you will generate 1 report each time and you will get the data from all of them.

Other option is keep the nodes working like now but send the report data to a common place before to send the report and gather all the information together. With this approach you will need one more process.

Let me know if it helps you!

Upvotes: 5

Related Questions