{"id":1277,"date":"2021-02-24T15:48:44","date_gmt":"2021-02-24T07:48:44","guid":{"rendered":"http:\/\/diuut.com\/?p=1277"},"modified":"2021-02-24T15:54:15","modified_gmt":"2021-02-24T07:54:15","slug":"%e5%9f%ba%e4%ba%8ereids%e4%b8%8eaop%e5%ae%9e%e7%8e%b0%e7%9a%84%e5%ae%9a%e5%88%86%e5%b8%83%e5%bc%8f%e5%90%8c%e6%ad%a5%e4%bb%bb%e5%8a%a1%e9%94%81-scheduledlock","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=1277","title":{"rendered":"\u57fa\u4e8eReids\u4e0eAOP\u5b9e\u73b0\u7684\u5b9a\u65f6\u4efb\u52a1\u9501-ScheduledLock"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42\uff1a<\/p><cite>\u9879\u76ee\u4e2d\u7684\u5b9a\u65f6\u4efb\u52a1\u4f1a\u5b9a\u65f6\u6267\u884c\uff0c\u4f46\u5982\u679c\u90e8\u7f72\u591a\u4e2a\u670d\u52a1\u5668\u7684\u8bdd\u5c31\u4f1a\u5728\u540c\u4e00\u65f6\u95f4\uff0c\u6bcf\u4e2a\u670d\u52a1\u90fd\u4f1a\u6267\u884c\u4e00\u6b21\uff0c\u5982\u679c\u662f\u65b0\u5efa\u6216\u8005\u4fee\u6539\u7c7b\u7684\u64cd\u4f5c\u7684\u8bdd\u5c31\u4f1a\u6709\u51b2\u7a81\uff0c\u56e0\u6b64\u9700\u8981\u4e00\u4e2a\u65b9\u6848\u5904\u7406\u8fd9\u4e2a\u591a\u5904\u6267\u884c\u7684\u95ee\u9898\u3002<\/cite><\/blockquote>\n\n\n\n<p>       \u7b80\u5355\u6765\u8bf4\u5c31\u662f\u901a\u8fc7aop\u73af\u7ed5\u5207\u7247\u5c06\u9700\u8981\u52a0\u9501\u7684\u65b9\u6cd5\u5305\u8d77\u6765\uff0c\u7136\u540e\u5728\u6267\u884c\u524d\u5f80redis\u4e2d\u7528setIfAbsent\u5199\u5165\u4e00\u4e2akey,\u5982\u679c\u8fd4\u56detrue\uff0c\u8bf4\u660e\u6ca1\u6709\u5176\u4ed6\u670d\u52a1\u6b63\u5728\u6267\u884c\u8be5\u65b9\u6cd5\uff0c\u5c31\u7ee7\u7eed\u6267\u884c\uff0c\u5982\u679c\u8fd4\u56defalse\uff0c\u8bf4\u660e\u5df2\u7ecf\u6709\u5176\u4ed6\u670d\u52a1\u6b63\u5728\u6267\u884c\u8be5\u65b9\u6cd5\uff0c\u5c31\u4e0d\u6267\u884c\u3002<\/p>\n\n\n\n<p>        \u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\u91c7\u7528\u4e86\u81ea\u5b9a\u4e49\u6ce8\u89e3\u7684\u65b9\u5f0f\uff0c\u5982\u679c\u54ea\u4e2a\u5b9a\u65f6\u4efb\u52a1\u9700\u8981\u4f7f\u7528\u7684\u8bdd\u76f4\u63a5\u52a0\u4e00\u4e2a@ScheduledLock\u6ce8\u89e3\u5373\u53ef\u3002<\/p>\n\n\n\n<p>        \u8be5\u65b9\u6848\u4f7f\u7528\u5230\u4e86redis\uff0c\u4f7f\u7528\u4ee5\u53ca\u914d\u7f6e\u65b9\u6cd5\u7565\u8fc7\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>\u5f15\u5165\u76f8\u5173\u4f9d\u8d56<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n        &lt;dependency&gt;\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n            &lt;artifactId&gt;spring-boot-starter-aop&lt;\/artifactId&gt;\n        &lt;\/dependency&gt;\n        &lt;dependency&gt;\n            &lt;groupId&gt;org.aspectj&lt;\/groupId&gt;\n            &lt;artifactId&gt;aspectjrt&lt;\/artifactId&gt;\n        &lt;\/dependency&gt;\n        &lt;dependency&gt;\n            &lt;groupId&gt;org.aspectj&lt;\/groupId&gt;\n            &lt;artifactId&gt;aspectjweaver&lt;\/artifactId&gt;\n        &lt;\/dependency&gt;\n<\/pre><\/div>\n\n\n<p>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6ce8\u89e3\u63a5\u53e3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.miracle.qaodo.annotation;\nimport java.lang.annotation.*;\n\n\/**\n * @Author Diuut\n * @Date 2021\/2\/23  16:17\n *\/\n@Target(ElementType.METHOD)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Inherited\npublic @interface ScheduledLock {\n}\n<\/pre><\/div>\n\n\n<p>\u5b9e\u73b0AOP\u903b\u8f91<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.miracle.qaodo.aspect;\n\nimport com.miracle.qaodo.annotation.ScheduledLock;\nimport lombok.extern.slf4j.Slf4j;\nimport org.aspectj.lang.ProceedingJoinPoint;\nimport org.aspectj.lang.annotation.Around;\nimport org.aspectj.lang.annotation.Aspect;\nimport org.aspectj.lang.reflect.MethodSignature;\nimport org.springframework.beans.BeansException;\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.ApplicationContextAware;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.stereotype.Component;\n\nimport java.lang.reflect.Method;\nimport java.util.concurrent.TimeUnit;\n\n\/**\n * @Author Diuut\n * @Date 2021\/2\/23  16:20\n *\/\n@Aspect\n@Component\n@Slf4j\npublic class ScheduleLockAspect implements ApplicationContextAware {\n    \/\/\u76f4\u63a5autowired redistemplate\u4f1a\u62a5\u9519Unexpected error occurred in scheduled task\n    \/\/\u56e0\u4e3a@Scheduled\u6ce8\u89e3\u65b9\u5f0f\u7ea7\u522b\u9ad8\u4e8e\u8d44\u6e90\u6ce8\u5165\u7ea7\u522b,\u5bfc\u81f4\u4e86\u8d44\u6e90\u6ce8\u5165\u5931\u8d25\n    \/\/\u4f7f\u7528ApplicationContextAware,\u5b83\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u63a5\u53e3\u7684bean\uff0c\u5f53spring\u5bb9\u5668\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u4f1a\u81ea\u52a8\u7684\u5c06ApplicationContext\u6ce8\u5165\u8fdb\u6765\n\n    private static ApplicationContext context;\n\n    @Override\n    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {\n        context = applicationContext;\n    }\n\n    public static ApplicationContext getContext() {\n        return context;\n    }\n\n    public static Object getBean(String name) {\n        return getContext().getBean(name);\n    }\n\n    @Around(value = &quot;@annotation(scheduledLock)&quot;)\n    public Object around(ProceedingJoinPoint point, ScheduledLock scheduledLock) {\n        \/\/\u62e6\u622a\u7684\u7c7b\u540d\n        Class clazz = point.getTarget().getClass();\n        \/\/\u62e6\u622a\u7684\u65b9\u6cd5\n        Method method = ((MethodSignature) point.getSignature()).getMethod();\n        log.info(&quot;\u5b9a\u65f6\u4efb\u52a1\u9501 \u62e6\u622a\u4e86\u7c7b:&quot; + clazz + &quot; \u65b9\u6cd5:&quot; + method);\n        Object proceed = null;\n        RedisTemplate&lt;String,String&gt; redisTemplate = (RedisTemplate)getBean(&quot;redisTemplate&quot;);\n        if (redisTemplate.opsForValue().setIfAbsent(&quot;qdchess-SchdulesLock-&quot; + method.getName(), &quot;lock&quot;,10, TimeUnit.SECONDS)) {\n        \/\/\u6b64\u5904\u7684key\u662f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u4f7f\u7528\u60c5\u51b5\u8fdb\u884c\u8bbe\u7f6e\uff0c\u53ea\u8981\u65b9\u6cd5\u4e4b\u95f4\u4e0d\u91cd\u590d\u5373\u53ef\u3002\n            log.info(&quot;\u5176\u4ed6\u670d\u52a1\u672a\u6267\u884c\uff0c\u901a\u8fc7\u6267\u884c&quot;);\n            \/\/\u83b7\u53d6\u9501\uff0c\u5982\u679c\u4e3afalse\u8bf4\u660e\u6709\u5176\u4ed6\u670d\u52a1\u6b63\u5728\u6267\u884c\uff0c\u8df3\u8fc7\u6267\u884c\n            try {\n                proceed = point.proceed();          \/\/\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\n               redisTemplate.delete(&quot;qdchess-SchdulesLock-&quot; + method.getName());\n                return proceed;\n            } catch (Throwable throwable) {\n                redisTemplate.delete(&quot;qdchess-SchdulesLock-&quot; + method.getName());\n                throwable.printStackTrace();\n                return null;\n            }\n        }\n        log.info(&quot;\u5176\u4ed6\u670d\u52a1\u5df2\u6267\u884c\uff0c\u672a\u901a\u8fc7\u6267\u884c&quot;);\n        return proceed;\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u5728\u6d4b\u8bd5\u7f16\u5199\u671f\u95f4\u53cd\u590d\u62a5\u9519Unexpected error occurred in scheduled task\uff0c\u7ecf\u67e5\u8be2\u662f\u56e0\u4e3a@Scheduled\u6ce8\u89e3\u65b9\u5f0f\u7ea7\u522b\u9ad8\u4e8e\u8d44\u6e90\u6ce8\u5165\u7ea7\u522b,\u5bfc\u81f4\u4e86\u8d44\u6e90\u6ce8\u5165\u5931\u8d25,\u89e3\u51b3\u65b9\u6848\u662f\u7c7b\u5b9e\u73b0ApplicationContextAware,\u5b83\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u63a5\u53e3\u7684bean\uff0c\u5f53spring\u5bb9\u5668\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u4f1a\u81ea\u52a8\u7684\u5c06ApplicationContext\u6ce8\u5165\u8fdb\u6765\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u65b9\u6cd5\u5c31\u662f\u5728\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1\u7684\u5730\u65b9\u52a0\u4e00\u4e2a@ScheduledLock\u6ce8\u89e3\u5373\u53ef\u3002\u5982\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    @Scheduled(cron = &quot;0,20,40 * * * * ?&quot;)\n    @ScheduledLock\n    public void testLock(){\n        log.info(ServerTimer.getFull());\n    }\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a \u9879\u76ee\u4e2d\u7684\u5b9a\u65f6\u4efb\u52a1\u4f1a\u5b9a\u65f6\u6267\u884c\uff0c\u4f46\u5982\u679c\u90e8\u7f72\u591a\u4e2a\u670d\u52a1\u5668\u7684\u8bdd\u5c31\u4f1a\u5728\u540c\u4e00\u65f6\u95f4\uff0c\u6bcf\u4e2a\u670d\u52a1\u90fd\u4f1a\u6267\u884c\u4e00\u6b21\uff0c\u5982\u679c\u662f\u65b0\u5efa\u6216<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=1277\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">\u57fa\u4e8eReids\u4e0eAOP\u5b9e\u73b0\u7684\u5b9a\u65f6\u4efb\u52a1\u9501-ScheduledLock<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":1278,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[18,10,35],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1277"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1277"}],"version-history":[{"count":2,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1277\/revisions"}],"predecessor-version":[{"id":1281,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1277\/revisions\/1281"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/1278"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}