Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use redis to solve the problem of repeated execution of scheduled tasks in load balancing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use redis to solve the problem of Load Balancer scheduled task repetition. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something from the detailed introduction of this article.

Principle: When the setnx method in redis is set, if the key already exists, it is not added, and if the key does not exist, it is added; at the same time, because redis is single-threaded, there is no security problem.

Code part logic: 1. Take the task id of the timed task as the key, and any string value (use "true" here) as the value, set it into redis.

2. Determine whether the task is executed by setnx in redis (add a key, if the key already exists, do not add it). If result is true, it is not executed. If result is false, it has been executed. The timed task returns directly.

3. Set a cache survival time for the above key (there is a problem in the execution process of placing the code, resulting in the cache not being deleted, resulting in the timed task being executed only once. It is recommended to set the time much shorter than the timed task interval)

4. After the timed task content is executed, delete the previously set cache and close the redis connection (ensure that the next timed task is executed after the execution of one timed task).

The specific code is as follows

JobKey key=jobExecutionContext.getJobDetail().getKey();String taskId=key.getName();//setnx in redis (add a key, if the key already exists, do not add it) Judge whether the task is executed, if result is true, it is not executed, if result is false, it has been executed, timed task returnRedisConnection connection=redisTemplate.getConnectionFactory().getConnection();boolean setNXResult= connection.setNX(taskId.getBytes(),"true".getBytes());if(! setNXResult){ return;}run();connection.del(taskId.getBytes());connection.close(); The above content is how to use redis to solve the problem of repeated execution of scheduled tasks in Load Balancer. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report