{"id":4044,"date":"2022-11-27T21:59:01","date_gmt":"2022-11-27T13:59:01","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4044"},"modified":"2022-11-27T21:59:12","modified_gmt":"2022-11-27T13:59:12","slug":"eventlistener","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4044","title":{"rendered":"EventListener"},"content":{"rendered":"\n<p>\u8fd9\u79cd\u65b9\u5f0f\u6ca1\u6709\u914d\u5408\u6301\u4e45\u5316\u7684\u8bdd\u4e0d\u80fd\u7528\u6765\u751f\u4ea7\u4f7f\u7528\uff0c\u53ea\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<p>\u539f\u6587\uff1a<a href=\"https:\/\/juejin.cn\/post\/7130777841632804871\">Spring Event + \u5f02\u6b65\u5b9e\u73b0\u4e1a\u52a1\u7684\u89e3\u8026\uff08\u7ed3\u5408Spring Retry\u91cd\u8bd5\uff09 &#8211; \u6398\u91d1 (juejin.cn)<\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Spring Event + \u5f02\u6b65\u5b9e\u73b0\u4e1a\u52a1\u7684\u89e3\u8026\uff08\u7ed3\u5408Spring Retry\u91cd\u8bd5\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u573a\u666f<\/h3>\n\n\n\n<ul>\n<li>\u9879\u76ee\u5f00\u53d1\u4e2d\u7ecf\u5e38\u4f1a\u6d89\u53ca\u5230\u975e\u5e38\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u5982\u679c\u5168\u90e8\u8026\u5408\u5728\u4e00\u8d77\uff0c\u4e00\u4e2a\u7c7b\u7684\u4ee3\u7801\u5c31\u4f1a\u975e\u5e38\u957f\uff0c\u6240\u4ee5\u9700\u8981\u6211\u4eec\u5bf9\u4e1a\u52a1\u4ee3\u7801\u8fdb\u884c\u89e3\u8026\u3002\u901a\u5e38\u5728\u4e00\u4e2a\u590d\u6742\u7684\u903b\u8f91\u4e1a\u52a1\u91cc\u9762\uff0c\u4f1a\u5305\u542b\u4e00\u4e2a\u6838\u5fc3\u4e1a\u52a1\u548cN\u4e2a\u5b50\u4e1a\u52a1\uff0c\u6709\u4e9b\u5b50\u4e1a\u52a1\uff0c\u6211\u4eec\u662f\u4e0d\u9700\u8981\u5728\u5f53\u524d\u8bf7\u6c42\u4e2d\u540c\u6b65\u5b8c\u6210\u7684\uff0c\u4f8b\u5982\u77ed\u4fe1\u53d1\u9001\u7b49\u3002\u8fd9\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u60f3\u5230MQ\u3002\u4f46\u662f\u5f53\u6211\u4eec\u4e0d\u60f3\u5f15\u5165MQ\u65f6\uff0c\u5c31\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Spring Event\uff0c\u5b83\u76f8\u5f53\u4e8e\u4e00\u4e2a\u89c2\u5bdf\u8005\u6a21\u5f0f\uff0c\u5f53\u4e00\u4e2aBean\u5b8c\u6210\u4efb\u52a1\u4e4b\u540e\uff0c\u4f1a\u901a\u77e5\u53e6\u4e00\u4e2aBean\u53bb\u6267\u884c\u76f8\u5e94\u7684\u4efb\u52a1\u3002<\/li>\n\n\n\n<li>Spring Retry\u4e5f\u662fSpring\u91cc\u7684\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u4e3b\u8981\u5b9e\u73b0\u662f\u53d1\u751f\u5f02\u5e38\u540e\u91cd\u65b0\u8c03\u7528\u3002\u5f53\u4e00\u4e9b\u77ac\u65f6\u9519\u8bef\uff08\u4f8b\u5982\u7f51\u7edc\u95ee\u9898\uff09\u53d1\u751f\u65f6\uff0cSpring Retry\u7684\u91cd\u8bd5\u53ef\u4ee5\u5e2e\u6211\u4eec\u907f\u514d\u8fd9\u79cd\u5f02\u5e38\u9020\u6210\u7684\u670d\u52a1\u8c03\u7528\u5931\u8d25\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u6848\u4f8b<\/h3>\n\n\n\n<h5 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u4e8b\u4ef6<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>@Data\n@AllArgsConstructor\npublic class MsgEvent {\n    <strong>private<\/strong> <strong>String<\/strong> <strong>msgId<\/strong>;\n}\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u5b9a\u4e49\u76d1\u542c\u5668<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component\n@Slf4j\npublic class MsgListener {\n\n    <em>\/**\n     * value\u503c\u8868\u793a\u5f53\u54ea\u4e9b\u5f02\u5e38\u7684\u65f6\u5019\u89e6\u53d1\u91cd\u8bd5\uff0c\n     * maxAttempts\u8868\u793a\u6700\u5927\u91cd\u8bd5\u6b21\u6570\u9ed8\u8ba4\u4e3a3\uff0c\n     * delay\u8868\u793a\u91cd\u8bd5\u7684\u5ef6\u8fdf\u65f6\u95f4\uff0c\n     * multiplier\u8868\u793a\u4e0a\u4e00\u6b21\u5ef6\u65f6\u65f6\u95f4\u662f\u8fd9\u4e00\u6b21\u7684\u500d\u6570\u3002\n     * @param event\n     *\/<\/em>\n    @EventListener\n    @Async\n    @Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))\n    public void sendMsg(MsgEvent event) {\n        <strong>String<\/strong> <strong>msgId<\/strong> = <strong>event<\/strong>.getMsgId();\n        <strong>StopWatch<\/strong> <strong>watch<\/strong> = <strong>new<\/strong> <strong>StopWatch<\/strong>(msgId);\n        <strong>watch<\/strong>.start();\n        <strong>log<\/strong>.info(\"\u5f00\u59cb\u53d1\u77ed\u4fe1\");\n        <strong>try<\/strong> {\n            <strong>Thread<\/strong>.sleep(4000);\n        } <strong>catch<\/strong> (InterruptedException e) {\n            <strong>e<\/strong>.printStackTrace();\n        }\n        <strong>watch<\/strong>.stop();\n        <strong>log<\/strong>.info(\"\u77ed\u4fe1\u53d1\u9001\u6210\u529f, \u6d88\u606fid\uff1a\u3010{}\u3011 | \u8017\u65f6: ({})\", msgId, watch.getLastTaskTimeMillis());\n    }\n}\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u542f\u52a8\u7c7b<\/h5>\n\n\n\n<p>\u6dfb\u52a0@EnableAsync\u548c@EnableRetry\u6ce8\u89e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@SpringBootApplication\n@EnableAsync\n@EnableRetry\npublic class LabApplication {\n    <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> <strong>main<\/strong>(String&#91;] args) {\n        <strong>SpringApplication<\/strong>.run(LabApplication.class, args);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u5355\u5143\u6d4b\u8bd5<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>@SpringBootTest\n@Slf4j\npublic class EventTest {\n\n    @Autowired\n    private ApplicationContext applicationContext;\n\n    @Test\n    public void msgTest() {\n        <strong>applicationContext<\/strong>.publishEvent(new MsgEvent(\"123\"));\n        <strong>log<\/strong>.info(\"\u77ed\u4fe1\u53d1\u9001\u4e8b\u4ef6\u53d1\u5e03\u6210\u529f\");\n        <strong>try<\/strong> {\n            <strong>Thread<\/strong>.sleep(6000);\n        } <strong>catch<\/strong> (InterruptedException e) {\n            <strong>e<\/strong>.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u6267\u884c\u7ed3\u679c<\/h5>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/p6-juejin.byteimg.com\/tos-cn-i-k3u1fbpfcp\/4f98848c6a424ccdafcc77835d9dddf6~tplv-k3u1fbpfcp-zoom-in-crop-mark:4536:0:0:0.awebp?\" alt=\"1660262103972.jpg\"\/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u79cd\u65b9\u5f0f\u6ca1\u6709\u914d\u5408\u6301\u4e45\u5316\u7684\u8bdd\u4e0d\u80fd\u7528\u6765\u751f\u4ea7\u4f7f\u7528\uff0c\u53ea\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002 \u539f\u6587\uff1aSpring Event + \u5f02\u6b65\u5b9e\u73b0\u4e1a\u52a1\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,132],"tags":[261],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4044"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4044"}],"version-history":[{"count":2,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4044\/revisions"}],"predecessor-version":[{"id":4046,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4044\/revisions\/4046"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}