These are chat archives for spring-cloud/spring-cloud

10th
Dec 2017
mohanrao
@mohanrao
Dec 10 2017 18:06
Hi, If I want to run a timer job in spring cloud. How can I achieve that happens only one instance. i.e kind of achieving leadership election. I am using consul as my service discovery/ service config/service registry
mohanrao
@mohanrao
Dec 10 2017 18:29
When i try to lookup spring cloud cluster. The spring cloud page redirects to the same spring cloud page. Also on the spring cloud github page it is suggested to use spring integration.
I am trying to find an example how to achieve distributed locking with consul
Can someone please help me in this?
John G
@jgolubenko
Dec 10 2017 21:44
mohanrao
@mohanrao
Dec 10 2017 21:45
If I use the scheduled annotation. And deploy the same spring boot application in two jvm instances then it will execute only in one jvm ?
Sorry for the confusion @jgolubenko
John G
@jgolubenko
Dec 10 2017 21:51
@mohanrao of course not. each jvm+application is independent of the others. Perhaps you schedule to run at say 00:00 hours so they will be executing your code around that time, but it all depends on your use case. If you need to guarantee that it’s executing your code at exactly the same microsecond on 1+ JVM’s then perhaps you need another solution
@mohanrao as far as I know spring doesn’t provide something like that out of the box.
mohanrao
@mohanrao
Dec 10 2017 21:54
Yep. My use case is I have to run a job on a specific interval. So I would like to use spring leader election to ensure only one will execute the job
John G
@jgolubenko
Dec 10 2017 21:54
@mohanrao there is also of course question of making sure that time is synchronized and exactly the same on both JVM’s/operating systems which is not a simple thing actually
@mohanrao perhaps @Scheduled along with some flag in db or some *.pid file will be able to solve your problem. e.g. each time some method is invoked it will check the .pid file or flag in db to see if something else is already started to execute this particular job