Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the concurrency problem caused by schedule timer in multiple deploy execution by springboot

Shulou Source: shulou.com Published: 2022-06-02 08:49:39 10月03日 Update

This article mainly explains "springboot how to solve the concurrency problem caused by multiple deploy execution schedule timers". The explanation in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "springboot how to solve the concurrency problem caused by multiple deploy execution schedule timers".

Springboot deploys multiple deploy of the same service. When there is a timer in the service, once the timer is triggered, the service will execute at the same time, resulting in concurrency problems. Solution:

1. Add @ Profile annotation to the timer class to specify that the instance executes the timer.

@ Component@Configuration@EnableScheduling / / 1 Start the timing task @ Profile ("deploy01") public class ScheduleTaskService {....}

It is easy to operate and flexible. You only need to add the @ Profile annotation, but its limitations are obvious: the timer processing class is loaded only when the service is specified as deploy01, and cannot be executed once the deploy01 is dead or not started. So it doesn't achieve high availability.

two。 Using redis caching mechanism

@ Scheduled (cron = "000 String deployKey= 1 * *?") / / execute public void insertPriRecoverCall () throws InterruptedException {String deployKey= "deployName" every hour; redisService.set (deployKey,deployId,1,TimeUnit.HOURS); Object o = redisService.get (deployKey); if (ostatic null) {String deployName = o.toString () If (deployId.equals (deployName)) {logger.info (deployName+ "is running."); / Business Code}}

First get the service instance Id and cache it in redis. Redis is the caching mechanism for key-value key-value pairs. Only one deployId is retained under the same key, and then the deployId in the cache is compared with the service instance id. The same will continue to execute the business code, while the difference will be filtered directly. This can not only prevent concurrency problems, but also achieve high availability.

Thank you for your reading, the above is the content of "springboot how to solve the concurrency problem caused by multiple deploy execution schedule timers". After the study of this article, I believe you have a deeper understanding of how springboot solves the concurrency problem caused by multiple deploy execution schedule timers, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Timer problem service multiple cache same instance learning business code content mechanism annotation different obvious task only at the same time hour is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB Linux Shulou Information Shulou Tech Info