简体   繁体   中英

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?

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.

as example you can use this one

    <dependency>
        <groupId>net.javacrumbs.shedlock</groupId>
        <artifactId>shedlock-spring</artifactId>
    </dependency>
    <dependency>
        <groupId>net.javacrumbs.shedlock</groupId>
        <artifactId>shedlock-provider-jdbc-template</artifactId>
    </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM