Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

java - Spring: how to lock the scheduler in one of several instances of the application

My application has a scheduler that requests data from the database every 10 seconds and processes it. I can have 2 instances of the application running: the first is a constantly running dev server, and the second is my local one. Both instances take data from the same database. The problem is that when I make edits locally and want to test them, because the dev server is running, it can intercept and process my data before the local server does. This can happen several times and it is very disturbing. Is there any way to make it so that when only my local instance performs a task in the scheduler , and the dev server just skips it? I mean that I need my local scheduler to run as planned every 10 seconds, and the scheduler on the dev server skipped the task while my local instance is running. Is it possible? Or is there a library for that?

question from:https://stackoverflow.com/questions/65894726/spring-how-to-lock-the-scheduler-in-one-of-several-instances-of-the-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are many possible solutions to this. The "cleanest" one is to separate the databases so that your local application won't connect to the database of the dev server at all and will maintain some database running locally.

Another possible solution is to refactor the scheduled job to run only upon some kind of custom configuration so that on dev profile (you might use spring profiles or 'conditions' in code) the scheduler won't run at all. On the dev server this configuration will be enabled by default and the scheduling will run as usual.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...