{"id":1425,"date":"2025-10-26T17:14:23","date_gmt":"2025-10-26T09:14:23","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=1425"},"modified":"2025-11-03T18:39:35","modified_gmt":"2025-11-03T10:39:35","slug":"xxl-job-%e7%9a%84%e5%ad%90%e7%ba%bf%e7%a8%8b%e4%bd%bf%e7%94%a8xxljobhelper-log%e3%80%81threadlocal-%e4%b8%8e-inheritablethreadlocal%ef%bc%9a%e5%8e%9f%e7%90%86%e3%80%81%e9%99%b7%e9%98%b1%e4%b8%8e","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=1425","title":{"rendered":"XXL-JOB \u7684\u5b50\u7ebf\u7a0b\u4f7f\u7528XxlJobHelper.log\u3001ThreadLocal \u4e0e InheritableThreadLocal\uff1a\u539f\u7406\u3001\u9677\u9631\u4e0e\u5b9e\u6218\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5173\u952e\u5b57\uff1a<\/strong>XXL-JOB\u3001XxlJobHelper\u3001XxlJobContext\u3001ThreadLocal\u3001InheritableThreadLocal\u3001\u7ebf\u7a0b\u6c60\u3001\u65e5\u5fd7\u4e0a\u4e0b\u6587\u3001TransmittableThreadLocal<\/p>\n<\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\">0.\u524d\u8a00\u3001\u95ee\u9898\u80cc\u666f\uff1a<\/h1>\n\n\n\n<p>\u5728\u5f00\u53d1\u751f\u4ea7\u6570\u636e\u6a21\u5757\u7684\u67d0\u4e2a\u5b9a\u65f6\u4efb\u52a1\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u7ebf\u7a0b\u6c60\u6765\u6309\u5de5\u5382\u5e76\u53d1\u5904\u7406\u3002\u8fd9\u91cc\u7531\u4e8e\u672c\u5730\u65e5\u5fd7\u4e0d\u4fbf\u4e8e\u67e5\u8be2\uff0c\u4f7f\u7528XxlJobHelper.<em>log<\/em>\u4f5c\u65e5\u5fd7\u8bb0\u5f55\u3002\u5982\u4e0b\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u82e5\u8be5\u5de5\u5382\u5728\u4e0a\u4e00\u5c0f\u65f6\u786e\u5b9e\u6709\u7ed3\u675f\u7684\u73ed\u6b21\uff0c\u5219\u63d0\u4ea4\u4efb\u52a1\n                if (shift != null) {\n                    Future&lt;Boolean&gt; future = productionRecordPerHourTaskPool.submit(() -&gt; {\n                        try {\n                            log.info(\"&#91;recordDataPerShift] \u5de5\u5382 {} \u68c0\u6d4b\u5230\u7ed3\u675f\u73ed\u6b21 {}\uff08end_time={}\uff09\uff0c\u5f00\u59cb\u8bb0\u5f55\u751f\u4ea7\u6570\u636e...\", locationName, shift.getShiftName(), shift.getEndTime());\n                            XxlJobHelper.log(\"FROM XXLJOB-LOG:: &#91;recordDataPerShift] \u5de5\u5382 {} \u68c0\u6d4b\u5230\u7ed3\u675f\u73ed\u6b21 {}\uff08end_time={}\uff09\uff0c\u5f00\u59cb\u8bb0\u5f55\u751f\u4ea7\u6570\u636e...\", locationName, shift.getShiftName(), shift.getEndTime());\n\n                            \/\/ \u5173\u952e\u8c03\u7528\uff1a\u5dee\u503c\u6cd5 + \u5bb9\u5fcd\u7a97\u53e3\u8ba1\u7b97\n                            return monitorSenseService.recordDataWithShift(locationName, factoryId, shift.getId(), shift.getEndTime());\n                        } catch (Exception e) {\n                            log.error(\"&#91;recordDataPerShift] \u5de5\u5382 {} \u6570\u636e\u8bb0\u5f55\u5f02\u5e38\uff1a{}\", locationName, e.getMessage(), e);\n                            return false;\n                        }\n                    });\n                    futures.add(future);\n                }<\/code><\/pre>\n\n\n\n<p>\u90a3\u4e48\u8fd9\u91cc\u5c31\u6709\u4e86\u4ee5\u4e0b\u95ee\u9898\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>XxlJobHelper.log()<\/code> \u662f\u5982\u4f55\u77e5\u9053\u201c\u5f53\u524d\u4efb\u52a1\u201d\u7684\uff1f<\/li>\n\n\n\n<li>\u5728\u5b50\u7ebf\u7a0b\u91cc\u8fd8\u80fd\u76f4\u63a5\u4f7f\u7528\u5417\uff1f\u6709\u4ec0\u4e48\u9690\u60a3\/\u95ee\u9898\uff1f<\/li>\n<\/ol>\n\n\n\n<p>\u4ee5\u8fd9\u4e24\u4e2a\u95ee\u9898\u4e3a\u5f15\uff0c\u5c31\u8981\u5f15\u51fa\u5173\u4e8e\u7ebf\u7a0b\u6c60\u3001\u7ebf\u7a0b\u590d\u7528\u3001<code>XxlJob<\/code>\u4e0a\u4e0b\u6587\u539f\u7406\u3001ThreadLocal\u3001InheritableThreadLocal\u3001TransmittableThreadLocal\u76f8\u5173\u7684\u4e00\u7cfb\u5217\u8ba8\u8bba\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u9645\u884c\u4e3a\uff1a<\/h2>\n\n\n\n<p>\u867d\u7136\u6587\u7ae0\u548c\u76f8\u5173\u77e5\u8bc6\u662f\u8fd9\u4e48\u5199\u7684\uff0c\u4f46\u662f\u4efb\u52a1\u8dd1\u4e86\u4e00\u5468\u4e86\uff0c\u76ee\u524d\u8fd8\u6ca1\u6709\u53d1\u73b0\u6709\u76f8\u5173\u95ee\u9898\u3002\u3002\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u786e\u5b9e\u5bf9\u201c\u4e0a\u4e0b\u6587\u201d\u3001ThreadLocal\u3001InheritableThreadLocal\u7b49\u77e5\u8bc6\u6709\u4e86\u66f4\u6df1\u7684\u7406\u89e3\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">1. \u6982\u89c8\uff08\u5148\u770b\u7ed3\u8bba\uff09<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>XxlJobHelper.log()<\/code> \u662f\u901a\u8fc7 <code>XxlJobContext.getXxlJobContext()<\/code> \u4ece<strong>\u5f53\u524d\u7ebf\u7a0b<\/strong>\u7684 <code>ThreadLocal<\/code>\uff08 <code>InheritableThreadLocal<\/code>\uff09\u8bfb\u53d6\u4e0a\u4e0b\u6587\uff0c\u518d\u5c06\u65e5\u5fd7\u5199\u5165\u5bf9\u5e94 job \u7684\u65e5\u5fd7\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><code>InheritableThreadLocal<\/code> \u4f1a\u5728<strong>\u7ebf\u7a0b\u521b\u5efa\u65f6<\/strong>\u628a\u7236\u7ebf\u7a0b\u7684\u503c\u590d\u5236\u5230\u5b50\u7ebf\u7a0b\uff0c\u56e0\u6b64 <code>new Thread(...)<\/code> \u521b\u5efa\u7684\u7ebf\u7a0b\u53ef\u4ee5\u7ee7\u627f\u7236\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u3002<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u6c60\uff08Executor\uff09\u91cc\u7684\u7ebf\u7a0b\u901a\u5e38\u662f <strong>\u9884\u521b\u5efa\u5e76\u590d\u7528\u7684<\/strong>\uff0c\u56e0\u6b64\u5b83\u4eec<strong>\u4e0d\u4f1a\u5728\u6bcf\u6b21 submit \u65f6\u91cd\u65b0\u590d\u5236\u7236\u7ebf\u7a0b\u7684 InheritableThreadLocal \u503c<\/strong>\uff0c\u8fd9\u4f1a\u9020\u6210\u4e24\u7c7b\u95ee\u9898\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5b50\u7ebf\u7a0b\u62ff\u4e0d\u5230 context\uff08<code>null<\/code> \u2192 \u65e5\u5fd7\u4e22\u5931\u6216\u56de\u9000\u5230\u666e\u901a logger\uff09<\/li>\n\n\n\n<li>\u5b50\u7ebf\u7a0b\u62ff\u5230\u7684\u662f\u4e0a\u4e00\u4e2a\u4efb\u52a1\u9057\u7559\u7684 context\uff08\u6c61\u67d3 \u2192 \u65e5\u5fd7\u5199\u5230\u522b\u7684 job \u7684\u6587\u4ef6\uff09<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u5de5\u7a0b\u4e0a\u6700\u7a33\u59a5\u7684\u505a\u6cd5\uff1a<strong>\u663e\u5f0f\u5728\u63d0\u4ea4\u7684 Runnable\/Callable \u5185\u6ce8\u5165\u7236\u7ebf\u7a0b\u7684 XxlJobContext\uff08submit\u524d\u624b\u52a8get\u7236\u7ebf\u7a0b\uff09\uff0c\u5e76\u5728\u4efb\u52a1\u7ed3\u675f\u65f6\u6e05\u7406\u6216\u6062\u590d<\/strong>\u3002\u53e6\u53ef\u8003\u8651\u4f7f\u7528 Alibaba TTL\uff08TransmittableThreadLocal\uff09\u6765\u81ea\u52a8\u4f20\u64ad context\uff08\u9700\u5f15\u5165\u4f9d\u8d56\u5e76\u5305\u88c5 Executor\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">2. \u80cc\u666f\u4e0e\u6838\u5fc3\u4ee3\u7801\uff08XXL-JOB \u7684\u65e5\u5fd7\u5b9e\u73b0\u8981\u70b9\uff09<\/h1>\n\n\n\n<p>XXL-JOB \u5728\u4efb\u52a1\u6267\u884c\u4e0a\u4e0b\u6587\u548c\u65e5\u5fd7\u5199\u5165\u4e0a\uff0c\u63d0\u4f9b\u4e86\u4e24\u5c42\u91cd\u8981\u7684\u7c7b\uff08\u7b80\u5316\u8bf4\u660e\uff09\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>XxlJobContext<\/code>\uff1a\u4fdd\u5b58 jobId\u3001jobParam\u3001jobLogFileName\u3001shard \u4fe1\u606f\u7b49\u3002\u73b0\u4ee3\u5b9e\u73b0\u4e2d\u7531 <code>InheritableThreadLocal&lt;XxlJobContext&gt;<\/code> \u6301\u6709\uff08\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684 ThreadLocalMap\uff09\u3002<\/li>\n\n\n\n<li><code>XxlJobHelper<\/code>\uff1a\u5de5\u5177\u7c7b\uff0c\u4efb\u52a1\u4ee3\u7801\u5e38\u76f4\u63a5\u8c03\u7528 <code>XxlJobHelper.log(...)<\/code>\u3002\u6838\u5fc3\u903b\u8f91\u5c31\u662f\u4ece <code>XxlJobContext.getXxlJobContext()<\/code> \u83b7\u53d6\u4e0a\u4e0b\u6587\uff0c\u62ff\u5230 <code>jobLogFileName<\/code>\uff0c\u7136\u540e\u628a\u65e5\u5fd7\u5199\u5165 job \u7684\u65e5\u5fd7\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u793a\u610f\uff08\u5173\u952e\u7247\u6bb5\uff0c\u975e\u5b8c\u6574\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ XxlJobContext\nprivate static InheritableThreadLocal&lt;XxlJobContext&gt; contextHolder = new InheritableThreadLocal&lt;&gt;();\n\npublic static void setXxlJobContext(XxlJobContext ctx) {\n    contextHolder.set(ctx);\n}\npublic static XxlJobContext getXxlJobContext() {\n    return contextHolder.get();\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ XxlJobHelper.log(...)\nXxlJobContext ctx = XxlJobContext.getXxlJobContext();\nif (ctx != null) {\n    XxlJobFileAppender.appendLog(ctx.getJobLogFileName(), formattedLog);\n} else {\n    \/\/ fallback: logger.info(...)\n}\n<\/code><\/pre>\n\n\n\n<p>\u91cd\u70b9\uff1a<code>get()<\/code> \u53ea\u662f\u8bfb\u53d6<strong>\u5f53\u524d\u7ebf\u7a0b<\/strong>\u91cc ThreadLocalMap \u7684\u503c\uff1b\u5b83\u672c\u8eab<strong>\u4e0d\u4f1a<\/strong>\u8de8\u7ebf\u7a0b\u53bb\u201c\u62c9\u53d6\u201d\u522b\u7684\u7ebf\u7a0b\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">3. ThreadLocal \u4e0e InheritableThreadLocal\uff1a\u5de5\u4f5c\u673a\u5236\u5bf9\u6bd4<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>ThreadLocal<\/th><th>InheritableThreadLocal<\/th><\/tr><\/thead><tbody><tr><td>\u5b58\u50a8\u4f5c\u7528\u57df<\/td><td>\u4ec5\u5f53\u524d\u7ebf\u7a0b<\/td><td>\u7236\u7ebf\u7a0b\u4e0e\u5176\u5b50\u7ebf\u7a0b\uff08\u5b50\u7ebf\u7a0b\u5728\u521b\u5efa\u65f6\u590d\u5236\uff09<\/td><\/tr><tr><td>\u590d\u5236\u65f6\u673a<\/td><td>\u65e0<\/td><td>\u5728\u5b50\u7ebf\u7a0b\u521b\u5efa\uff08new Thread\uff09\u65f6 JVM \u4f1a\u590d\u5236\u7236\u7ebf\u7a0b\u7684 inheritable map<\/td><\/tr><tr><td>\u7ebf\u7a0b\u6c60\u5f71\u54cd<\/td><td>\u65e0\u5dee\u5f02<\/td><td>\u7ebf\u7a0b\u6c60\u590d\u7528\u4f1a\u5bfc\u81f4\u7ee7\u627f\u5931\u6548\u6216\u9057\u7559\u6c61\u67d3<\/td><\/tr><tr><td>\u5178\u578b\u9002\u7528\u573a\u666f<\/td><td>\u8bf7\u6c42\u4e0a\u4e0b\u6587\u3001\u7ebf\u7a0b\u5185\u90e8\u7f13\u5b58<\/td><td>\u9700\u8981\u8de8\u5b50\u7ebf\u7a0b\u4e00\u6b21\u6027\u7ee7\u627f\u7684\u4e0a\u4e0b\u6587\uff08\u4f46\u6ce8\u610f\u7ebf\u7a0b\u6c60\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u91cd\u8981\uff1a<strong>InheritableThreadLocal \u7684\u590d\u5236\u53ea\u5728\u521b\u5efa\u7ebf\u7a0b\u65f6\u53d1\u751f\u4e00\u6b21<\/strong>\u3002\u5982\u679c\u7ebf\u7a0b\u5df2\u7ecf\u5b58\u5728\uff08\u7ebf\u7a0b\u6c60\u590d\u7528\uff09\uff0c\u590d\u5236\u4e0d\u4f1a\u53d1\u751f\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">4. \u7ebf\u7a0b\u6c60\u5982\u4f55\u7834\u574f\u7ee7\u627f\u8bed\u4e49\uff08\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u95ee\u9898\uff09<\/h1>\n\n\n\n<p>\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u4e00\u822c\u662f\u63d0\u524d\u521b\u5efa\u5e76\u5728\u6c60\u4e2d\u5faa\u73af\u4f7f\u7528\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a <code>ThreadPoolTaskExecutor<\/code>\uff08Spring \u7684\u5c01\u88c5\uff09\u6216 <code>Executors.newFixedThreadPool<\/code>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u6b21\u63d0\u4ea4\u4efb\u52a1\uff0c\u5982\u679c\u7ebf\u7a0b\u662f\u9996\u6b21\u521b\u5efa\uff0c\u5e76\u4e14\u7236\u7ebf\u7a0b\u5728\u63d0\u4ea4\u524d\u5df2\u7ecf <code>set<\/code> \u4e86 <code>XxlJobContext<\/code>\uff0c\u90a3\u4e48 JVM \u5728\u521b\u5efa\u7ebf\u7a0b\u65f6\u4f1a\u590d\u5236\u7236\u7ebf\u7a0b\u7684 <code>InheritableThreadLocal<\/code>\uff0c\u6b64\u540e\u8be5\u7ebf\u7a0b\u4f1a\u6301\u6709 context\uff08\u5373\u770b\u8d77\u6765\u201c\u81ea\u52a8\u7ee7\u627f\u201d\u6210\u529f\uff09\u3002<\/li>\n\n\n\n<li>\u4f46\u5982\u679c\u8be5\u7ebf\u7a0b\u5728\u9996\u6b21\u521b\u5efa\u65f6\u7236\u7ebf\u7a0b\u6ca1\u6709 context\uff0c\u7ebf\u7a0b\u5185 map \u4e3a\u7a7a\u3002\u540e\u9762\u67d0\u6b21 jobA \u5728\u4e3b\u7ebf\u7a0b set \u4e86 context \u5e76\u63d0\u4ea4\u4efb\u52a1\uff0c\u4f46\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u662f\u5df2\u5b58\u5728\u7684\uff08\u4e0d\u4f1a\u7ee7\u627f\uff09\uff0c\u8fd9\u6b21\u4efb\u52a1\u4f1a\u89c2\u5bdf\u5230 <code>null<\/code>\uff0c\u5bfc\u81f4\u65e5\u5fd7\u4e22\u5931\u6216 fallback\u3002<\/li>\n\n\n\n<li>\u66f4\u5371\u9669\u7684\u662f\uff1a\u5982\u679c\u7ebf\u7a0b\u66fe\u7ecf\u6267\u884c\u8fc7 jobX \u5e76\u88ab\u8bbe\u4e86 context#X\uff0c\u4e14\u4efb\u52a1\u7ed3\u675f\u540e\u672a\u6e05\u7406\uff0c\u90a3\u4e48\u7ebf\u7a0b\u518d\u88ab\u5176\u5b83 jobY \u590d\u7528\u65f6\uff0c\u4ecd\u7136\u4f1a\u5e26\u7740 context#X\u3002\u82e5\u6ca1\u6709\u5728\u6267\u884c\u524d\u8986\u76d6\u6216\u6e05\u7406\uff0c\u5c31\u4f1a\u628a jobY \u7684\u65e5\u5fd7\u5199\u8fdb jobX \u7684\u65e5\u5fd7\u6587\u4ef6 \u2192 \u4e32\u5199\/\u6c61\u67d3\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u56e0\u6b64\uff1a<strong>\u7ebf\u7a0b\u6c60\u4f7f\u7528\u4e0b\uff0cInheritableThreadLocal \u5e76\u4e0d\u80fd\u63d0\u4f9b\u53ef\u9760\u7684\u4e0a\u4e0b\u6587\u8bed\u4e49<\/strong>\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">5. \u5178\u578b\u73b0\u8c61\uff1a\u65e5\u5fd7\u4e22\u5931 vs \u65e5\u5fd7\u4e32\u5199\uff08\u6c61\u67d3\uff09<\/h1>\n\n\n\n<p><strong>\u573a\u666f A\uff1a\u65e5\u5fd7\u4e22\u5931\uff08<code>ctx == null<\/code>\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f55\u65f6\u53d1\u751f\uff1a\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u5728\u521b\u5efa\u65f6\u6ca1\u6709\u7ee7\u627f\u5230 context\uff08\u7236\u7ebf\u7a0b\u5728\u521b\u5efa\u7ebf\u7a0b\u4e4b\u524d\u6ca1\u6709 set\uff09\uff0c\u4e14\u5f53\u524d\u7ebf\u7a0b map \u4e2d\u6ca1\u6709 context\uff1b\u53c8\u6ca1\u6709\u624b\u52a8 set\u3002<\/li>\n\n\n\n<li>\u8868\u73b0\uff1a<code>XxlJobHelper.log()<\/code> \u7684\u8f93\u51fa\u6ca1\u6709\u8fdb\u5165 XXL-JOB \u7684 job \u65e5\u5fd7\uff08\u53ef\u80fd\u53ea\u51fa\u73b0\u5728\u666e\u901a logger\uff09\uff0c\u8c03\u5ea6\u4e2d\u5fc3\u65e5\u5fd7\u4e2d\u770b\u4e0d\u5230\u5b50\u7ebf\u7a0b\u7684\u65e5\u5fd7\u5185\u5bb9\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u573a\u666f B\uff1a\u65e5\u5fd7\u4e32\u5199 \/ \u6c61\u67d3\uff08\u65e7 context \u672a\u6e05\u7406\uff09<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f55\u65f6\u53d1\u751f\uff1a\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u4e0a\u6b8b\u7559\u4e86\u4e0a\u4e00\u4e2a job \u7684 <code>XxlJobContext<\/code>\uff08\u672a\u88ab\u6e05\u7406\uff09\uff0c\u73b0\u5728\u590d\u7528\u8be5\u7ebf\u7a0b\u6765\u6267\u884c\u53e6\u4e00\u4e2a job\uff0c\u4f46\u672a\u8986\u76d6 context\u3002<\/li>\n\n\n\n<li>\u8868\u73b0\uff1a\u5f53\u524d job \u7684\u65e5\u5fd7\u88ab\u5199\u5165\u5230\u524d\u4e00\u4e2a job \u7684\u65e5\u5fd7\u6587\u4ef6\uff08\u4e25\u91cd\u8bef\u5bfc\u3001\u96be\u4ee5\u6392\u67e5\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7236\u7ebf\u7a0b\uff08JobThread\uff09\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nThreadLocalMap:\n    XxlJobContext -&gt; context#A (jobId=1001, logFile=job1001.log)\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\u63d0\u4ea4\u4efb\u52a1: threadPool.submit( Runnable )\n\n         \u2502\n         \u25bc\n\n\u60c5\u51b51\uff1a\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u9996\u6b21\u521b\u5efa\uff08\u65b0\u5efa\u7ebf\u7a0b\uff09\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\u5b50\u7ebf\u7a0b\uff08pool-1-thread-1\uff09\u9996\u6b21\u521b\u5efa\uff1a\n    InheritableThreadLocal \u81ea\u52a8\u7ee7\u627f\u7236\u7ebf\u7a0b ThreadLocalMap\nThreadLocalMap:\n    XxlJobContext -&gt; context#A  \u2705 (\u7ee7\u627f\u81ea\u7236\u7ebf\u7a0b)\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\u6267\u884c\u4efb\u52a1\uff1a\n    XxlJobHelper.log()  -&gt; get() \u62ff\u5230 context#A\n\u65e5\u5fd7\u5199\u5165 job1001.log\n\n         \u2502\n         \u25bc\n\u4efb\u52a1\u7ed3\u675f\n\uff08\u5b50\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u4ecd\u4fdd\u7559 context#A\uff0c\u7ebf\u7a0b\u6c60\u672a\u6e05\u7406\uff09\n\n\u60c5\u51b52\uff1a\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u590d\u7528\uff08\u5df2\u5b58\u5728\u7ebf\u7a0b\uff09\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\u5b50\u7ebf\u7a0b\uff08pool-1-thread-1\uff09\u590d\u7528\uff1a\nThreadLocalMap:\n    XxlJobContext -&gt; context#B (\u53ef\u80fd\u662f\u4e0a\u4e00\u4e2a job \u7684\u6b8b\u7559\uff0c\u6216 null)\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\u6267\u884c\u4efb\u52a1\uff1a\n    XxlJobHelper.log() -&gt; get() \u62ff\u5230 context#B \u274c \uff08\u53ef\u80fd\u9519\u8bef\uff09\n\u9700\u8981\u624b\u52a8\uff1a\n    XxlJobContext.setXxlJobContext(context#A)\n\u65e5\u5fd7\u5199\u5165 job1001.log \u2705\n\u4efb\u52a1\u7ed3\u675f\u540e\uff1a\n    XxlJobContext.setXxlJobContext(null) \u6216\u6062\u590d previous\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">6. \u5b9e\u6218\uff1a\u4e24\u79cd\u7a33\u59a5\u89e3\u51b3\u65b9\u6848<\/h1>\n\n\n\n<p>\u4e0b\u9762\u6309\u590d\u6742\u5ea6\u4e0e\u4f9d\u8d56\u5217\u51fa\u53ef\u9009\u65b9\u6848\uff0c\u7ed9\u51fa\u4f18\u7f3a\u70b9\u4e0e\u793a\u4f8b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b9\u6848 A\uff08\u63a8\u8350\uff09\uff1a\u5728\u63d0\u4ea4\u65f6<strong>\u663e\u5f0f\u6ce8\u5165\u5e76\u6e05\u7406<\/strong> XxlJobContext\uff08\u6700\u7a33\u59a5\u3001\u517c\u5bb9\u6240\u6709\u7248\u672c\/\u73af\u5883\uff09<\/h2>\n\n\n\n<p>\u5c01\u88c5\u4e00\u4e2a\u5de5\u5177\uff0c\u628a\u4f20\u5165\u7684 Callable\/Runnable \u5305\u88f9\u4e00\u5c42\uff0c\u628a\u7236\u7ebf\u7a0b\u7684 context \u6ce8\u5165\u5b50\u7ebf\u7a0b\u5e76\u5728 finally \u4e2d\u6e05\u7406\/\u6062\u590d\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u73b0\uff08Java\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public final class XxlJobContextPropagator {\n\n    public static Runnable wrap(Runnable task) {\n        XxlJobContext parentCtx = XxlJobContext.getXxlJobContext();\n        return () -&gt; {\n            XxlJobContext previous = XxlJobContext.getXxlJobContext();\n            try {\n                XxlJobContext.setXxlJobContext(parentCtx);\n                task.run();\n            } finally {\n                \/\/ restore previous (may be null or previous value)\n                if (previous == null) {\n                    XxlJobContext.setXxlJobContext(null);\n                } else {\n                    XxlJobContext.setXxlJobContext(previous);\n                }\n            }\n        };\n    }\n\n    public static &lt;T&gt; Callable&lt;T&gt; wrap(Callable&lt;T&gt; task) {\n        XxlJobContext parentCtx = XxlJobContext.getXxlJobContext();\n        return () -&gt; {\n            XxlJobContext previous = XxlJobContext.getXxlJobContext();\n            try {\n                XxlJobContext.setXxlJobContext(parentCtx);\n                return task.call();\n            } finally {\n                if (previous == null) {\n                    XxlJobContext.setXxlJobContext(null);\n                } else {\n                    XxlJobContext.setXxlJobContext(previous);\n                }\n            }\n        };\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u793a\u4f8b\uff08\u4f60\u7684\u4ee3\u7801\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Future&lt;Boolean&gt; future = productionRecordPerHourTaskPool.submit(\n    XxlJobContextPropagator.wrap(() -&gt; {\n        XxlJobHelper.log(\"FROM XXLJOB-LOG:: \u5de5\u5382 {} \u5f00\u59cb...\", locationName);\n        return monitorSenseService.recordDataWithShift(locationName, factoryId, shift.getId(), shift.getEndTime());\n    })\n);\n<\/code><\/pre>\n\n\n\n<p>\u6216\u5bf9\u4e8e Callable\uff08\u8fd4\u56de\u503c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>futures.add(productionRecordPerHourTaskPool.submit(\n    XxlJobContextPropagator.wrap(() -&gt; {\n        XxlJobHelper.log(\"...\");\n        return Boolean.TRUE;\n    })\n));\n<\/code><\/pre>\n\n\n\n<p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u517c\u5bb9\u6027\u5f3a\uff08\u4e0d\u4f9d\u8d56\u6846\u67b6\u7248\u672c\u3001\u7ebf\u7a0b\u5de5\u5382\u3001\u5bb9\u5668\u884c\u4e3a\uff09\u3002<\/li>\n\n\n\n<li>\u53ef\u907f\u514d\u7ebf\u7a0b\u6c60\u6c61\u67d3\uff08\u663e\u5f0f\u6e05\u7406\uff09\u3002<\/li>\n\n\n\n<li>\u5b9e\u73b0\u7b80\u5355\u3001\u53ef\u96c6\u4e2d\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5fc5\u987b\u5728\u6240\u6709 submit\/execute \u7684\u5730\u65b9\u4f7f\u7528\u5305\u88c5\uff08\u53ef\u5c01\u88c5\u5728\u5de5\u5177\u5c42\u6216\u7edf\u4e00 Executor \u5c01\u88c5\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b9\u6848 B\uff1a\u4f7f\u7528 Alibaba TransmittableThreadLocal\uff08TTL\uff09\u81ea\u52a8\u4f20\u64ad\uff08\u66f4\u81ea\u52a8\uff0c\u4f46\u9700\u5f15\u5165\u4f9d\u8d56\uff09<\/h2>\n\n\n\n<p><code>TransmittableThreadLocal<\/code>\uff08TTL\uff09\u662f\u963f\u91cc\u5f00\u6e90\u7684\u4e00\u4e2a\u5e93\uff0c\u5b83\u5728 <code>Executor<\/code> \u63d0\u4ea4\u4efb\u52a1\u65f6\u4f1a\u81ea\u52a8 capture \u7236\u7ebf\u7a0b\u7684 TTL \u503c\u5e76\u628a\u5b83\u4f20\u7ed9\u6267\u884c\u7ebf\u7a0b\uff08\u901a\u8fc7\u5305\u88c5 Runnable\/Callable\uff09\u3002\u5b83\u5bf9\u7ebf\u7a0b\u6c60\u590d\u7528\u573a\u666f\u6bd4 <code>InheritableThreadLocal<\/code> \u66f4\u53cb\u597d\u3002<\/p>\n\n\n\n<p><strong>\u4f7f\u7528\u6b65\u9aa4<\/strong>\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5f15\u5165\u4f9d\u8d56\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency&gt;\n  &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;\n  &lt;artifactId&gt;transmittable-thread-local&lt;\/artifactId&gt;\n  &lt;version&gt;2.12.3&lt;\/version&gt; &lt;!-- \u793a\u4f8b\u7248\u672c --&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u5728\u5408\u9002\u7684\u70b9\u628a context \u653e\u8fdb TTL\uff08\u6216\u7528 TTL \u66ff\u6362 XxlJobContext \u7684 ThreadLocal\uff09\uff1a\n<ul class=\"wp-block-list\">\n<li>\u65b9\u5f0f\u4e00\uff1a\u628a XxlJobContext \u6539\u4e3a <code>TransmittableThreadLocal<\/code>\uff08\u9700\u4fee\u6539\u4ee3\u7801\/\u5e93\uff09\uff0c\u6216<\/li>\n\n\n\n<li>\u65b9\u5f0f\u4e8c\uff1a\u5728\u4f60\u7684\u4efb\u52a1\u63d0\u4ea4\u65f6\u7528 <code>TtlExecutors.getTtlExecutorService(executor)<\/code> \u6765\u5305\u88c5 Executor\uff0c\u4f7f\u63d0\u4ea4\u7684 Runnable \u81ea\u52a8\u4f20\u64ad Inheritable\/TTL \u5185\u5bb9\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>\u793a\u4f8b\uff08\u5305\u88c5 executor\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ExecutorService ttlExecutor = TtlExecutors.getTtlExecutorService(productionRecordPerHourTaskPool.getThreadPoolExecutor());\nttlExecutor.submit(() -&gt; {\n    XxlJobHelper.log(\"Using TTL propagated context\");\n});\n<\/code><\/pre>\n\n\n\n<p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u52a8\u4f20\u64ad\uff0c\u5c11\u91cf\u4ee3\u7801\u6539\u52a8\u5373\u53ef\uff08\u5c24\u5176\u9002\u5408\u5927\u89c4\u6a21\u4ee3\u7801\u57fa\uff09\u3002<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u6c60\u590d\u7528\u65f6\u4e5f\u80fd\u6b63\u786e\u4f20\u64ad\uff08\u56e0\u4e3a\u5728 submit \u65f6\u590d\u5236\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9700\u5f15\u5165\u7b2c\u4e09\u65b9\u4f9d\u8d56\uff1b<\/li>\n\n\n\n<li>\u9700\u8981\u786e\u4fdd\u6240\u6709\u4f7f\u7528\u7684\u7ebf\u7a0b\u6c60\u90fd\u88ab TTL \u5305\u88c5\uff0c\u4e14 TTL \u7684\u751f\u547d\u5468\u671f\u4e0e\u4e0a\u4e0b\u6587\u7ba1\u7406\u9700\u8981\u5c0f\u5fc3\uff08\u907f\u514d\u5185\u5b58\u6cc4\u9732\uff09\uff1b<\/li>\n\n\n\n<li>\u53ef\u80fd\u5bf9\u5e93\u5185\u5176\u5b83 ThreadLocal \u884c\u4e3a\u4ea7\u751f\u5f71\u54cd\uff0c\u9700\u6d4b\u8bd5\u3002<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">\u7ed3\u8bed<\/h1>\n\n\n\n<p><code>XxlJobHelper.log()<\/code> \u770b\u4f3c\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65e5\u5fd7 API\uff0c\u4f46\u5176\u80cc\u540e\u4f9d\u8d56\u7684\u662f ThreadLocal \u7684\u4e0a\u4e0b\u6587\u8bed\u4e49\u3002<code>InheritableThreadLocal<\/code> \u5728\u201c\u65b0\u5efa\u7ebf\u7a0b\u201d\u573a\u666f\u4e0b\u80fd\u81ea\u52a8\u63d0\u4f9b\u4fbf\u5229\uff0c\u4f46\u5728\u7ebf\u7a0b\u6c60\u8fd9\u79cd\u5e38\u89c1\u7684\u5e76\u53d1\u6267\u884c\u6a21\u578b\u4e2d\uff0c\u5b83<strong>\u4e0d\u80fd<\/strong>\u4fdd\u8bc1\u5b89\u5168\u4e0e\u4e00\u81f4\u6027\u3002\u7406\u89e3\u8fd9\u4e00\u70b9\u5e76\u91c7\u53d6<strong>\u663e\u5f0f\u6ce8\u5165\/\u6e05\u7406<\/strong>\u6216\u4f7f\u7528 <strong>TTL<\/strong>\uff0c\u80fd\u663e\u8457\u964d\u4f4e\u65e5\u5fd7\u6df7\u4e71\u4e0e\u6545\u969c\u6392\u67e5\u6210\u672c\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u9644\uff1a<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u5e38\u89c1\u95ee\u7b54\uff08FAQ\uff09<\/h2>\n\n\n\n<p><strong>Q1\uff1a\u5982\u679c\u6211\u770b\u5230\u5b50\u7ebf\u7a0b\u80fd\u76f4\u63a5\u6253\u5370\uff0c\u662f\u5426\u5c31\u4e0d\u7528\u6539\uff1f<\/strong><br>A1\uff1a\u4e0d\u80fd\u56e0\u4e3a\u4e00\u6b21\u89c2\u5bdf\u800c\u51b3\u5b9a\u3002\u53ef\u80fd\u662f\u201c\u6070\u597d\u7ebf\u7a0b\u9996\u6b21\u521b\u5efa\u65f6\u7ee7\u627f\u4e86\u7236\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u201d\u3002\u751f\u4ea7\u4ee5\u7a33\u59a5\u4e3a\u8981\uff0c\u5e94\u5f53\u663e\u5f0f\u6ce8\u5165\u6216\u4f7f\u7528 TTL\u3002<\/p>\n\n\n\n<p><strong>Q2\uff1a\u80fd\u5426\u5728 XxlJobHelper.log \u5185\u505a\u7279\u6b8a\u5904\u7406\uff0c\u5728\u6ca1\u6709 context \u65f6\u4ece\u7236\u7ebf\u7a0b\u53d6\uff1f<\/strong><br>A2\uff1a\u4e0d\u53ef\u80fd\u76f4\u63a5\u4ece\u201c\u7236\u7ebf\u7a0b\u201d\u53d6\uff0c\u56e0\u4e3a <code>log()<\/code> \u8fd0\u884c\u5728\u5f53\u524d\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u4e2d\uff0c\u4e0d\u77e5\u9053\u54ea\u4e2a\u662f\u201c\u7236\u7ebf\u7a0b\u201d\u3002\u5982\u679c\u60f3\u6846\u67b6\u5c42\u9762\u4fdd\u8bc1\uff0c\u9700\u8981\u5728\u63d0\u4ea4\u4efb\u52a1\u65f6\u8ba9\u6267\u884c\u7ebf\u7a0b\u7684 ThreadLocal \u6709\u503c\uff08\u5373 wrap\/TTL\uff09\u3002\u6539 <code>log()<\/code> \u7684\u884c\u4e3a\u4e0d\u73b0\u5b9e\u4e5f\u4e0d\u5b89\u5168\u3002<\/p>\n\n\n\n<p><strong>Q3\uff1a\u6e05\u7406\u4e0a\u4e0b\u6587\u771f\u7684\u5fc5\u8981\u5417\uff1f<\/strong><br>A3\uff1a\u5fc5\u8981\u3002\u7ebf\u7a0b\u6c60\u590d\u7528\u4e0b\u82e5\u4e0d\u6e05\u7406\uff0c\u4f1a\u5bfc\u81f4\u65e5\u5fd7\u4e32\u5199\u3001\u4efb\u52a1\u72b6\u6001\u6df7\u4e71\uff0c\u96be\u4ee5\u6392\u67e5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u76f8\u5173issue\u3001\u8d44\u6e90\u94fe\u63a5\uff1a<\/h2>\n\n\n\n<p><a href=\"https:\/\/github.com\/xuxueli\/xxl-job\/issues\/2925?utm_source=chatgpt.com\">2.3.0\u591a\u7ebf\u7a0b\u65e5\u5fd7\u6253\u5370\u6df7\u4e71 \u00b7 Issue #2925 \u00b7 xuxueli\/xxl-job<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/juejin.cn\/post\/7537593417100115977\">XXL-JOB\u53c2\u6570\u9519\u4e71\u6839\u56e0\u5256\u6790\uff1aInheritableThreadLocal\u5728\u591a\u7ebf\u7a0b\u4e0b\u7684\u9690\u85cf\u5371\u673a\u591a\u673a\u5206\u5e03\u5f0f\u4efb\u52a1\u4e2d\uff0cxxl &#8211; \u6398\u91d1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u952e\u5b57\uff1aXXL-JOB\u3001XxlJobHelper\u3001XxlJobContext\u3001ThreadLocal\u3001Inhe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,37],"tags":[],"class_list":["post-1425","post","type-post","status-publish","format-standard","hentry","category-bug","category-javaweblist"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/1425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1425"}],"version-history":[{"count":7,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/1425\/revisions"}],"predecessor-version":[{"id":1433,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/1425\/revisions\/1433"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1425"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}