In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use AOP annotations to achieve redis distributed preemption locks", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use AOP annotations to implement redis distributed preemption locks.
Abstract
In many projects, there will be some task requirements that need to do scheduled batch running, most of which are achieved through spring annotations, but in the production environment, multi-node deployment may cause multiple nodes of scheduled tasks to trigger at the same time and dirty data may occur. The previous solution was to specify the production node to handle scheduled tasks by configuring it in the dictionary. Although this method can also meet the requirements, it always feels very low, so I implement a set of distributed preemption locks by using redis through the AOP way of JAVA, and solve the scheduled task trigger caused by multi-node deployment in production environment by annotating.
Don't talk nonsense, just go to the code-- > 1. Customize a comment first.
Import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * @ author v_liuwen * @ date 2018-12-27 * / @ Target ({ElementType.METHOD,ElementType.TYPE}) / / acts on classes, methods, and interfaces @ Retention (RetentionPolicy.RUNTIME) / / you can get public @ interface RedisLock {} at run time
2. Create a new facet class
Import org.apache.commons.lang3.StringUtils;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import top.qrainly.bj_demo.job.springtask.scheduleJob.utils.RedisLockUtils;/** * @ author v_liuwen * @ date 2018-12-27 * / @ Aspect@Componentpublic class RedisLockAspect {private static Logger logger = LoggerFactory.getLogger (RedisLockAspect.class) / * * service method with RedisLock added to the section * / @ Pointcut ("@ annotation (redisLock)") private void cutMethod (RedisLock redisLock) {} @ Before ("cutMethod (redisLock)") public void doAccessCheck (JoinPoint point RedisLock redisLock) throws NoSuchMethodException {logger.info ("* * [Before] starts to enter AOP * *") } @ After ("cutMethod (redisLock)") public void after (JoinPoint point,RedisLock redisLock) {logger.info ("* * [after] AOP cut-in completion * *") } @ AfterThrowing ("cutMethod (redisLock)") public void doAfterThrow (RedisLock redisLock) {logger.info ("AfterThrowing...");} @ Around ("cutMethod (redisLock)") public void around (ProceedingJoinPoint point,RedisLock redisLock) {String name = point.getSignature (). GetName (); Class classTarget = point.getTarget (). GetClass () System.out.println ("--> AOP cut method name:" + name+ "AOP cut into the class name:" + classTarget.getSimpleName () + "
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.