简体   繁体   中英

Spring Scheduled Services in multi module project

I have a multi-module project which is set up like this:

commons-module
  -CommonsApp.java (Main class)
  -CommonsService.java
api-module
  -ApiApp.java (Main class)
main-moudle
  -MainApp.java (Main class)
  -MainService.java

Both CommonsService and MainService have some business logic and both have a corresponding @Scheduled annotated method to perform some batch processing.

MainService has a dependency on CommonsService and it autowires it inside main-module.

My app will run on seperate VM instances, with each module's jar running on its own instance. If I make the commons-module a dependency of main-module to autowire the beans from it, I worry that the MainApp.java will also start running the scheduled methods of commons-module(CommonsService.java).

This way there would be a huge wastage of resources as those scheduled methods will be running on both the commons-module machine and the main-module machine.

So how can I ensure that I use the CommonsService.java in main-module without letting Spring execute the @Scheduled annotated methods in it?

I suggest you use ShedLock
ShedLock does one and only one thing. It makes sure your scheduled tasks are executed at most once at the same time.
github link

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