-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (xxljob) 定时任务执行时在 MDC 中添加 traceId,方便追踪上下文日志
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ob/src/main/java/org/ballcat/autoconfigure/xxljob/trace/TraceXxlJobAnnotationAdvisor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.ballcat.autoconfigure.xxljob.trace; | ||
|
||
import com.xxl.job.core.handler.annotation.XxlJob; | ||
import org.aopalliance.aop.Advice; | ||
import org.springframework.aop.Pointcut; | ||
import org.springframework.aop.support.AbstractPointcutAdvisor; | ||
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; | ||
|
||
/** | ||
* XxlJob Trace 增强器,切点为添加了 {@link XxlJob} 注解的方法。 | ||
* | ||
* @author Hccake | ||
* @see TraceXxlJobAnnotationInterceptor | ||
*/ | ||
public class TraceXxlJobAnnotationAdvisor extends AbstractPointcutAdvisor { | ||
|
||
private final Advice advice; | ||
|
||
private final Pointcut pointcut; | ||
|
||
public TraceXxlJobAnnotationAdvisor(Advice advice) { | ||
this.advice = advice; | ||
this.pointcut = new AnnotationMatchingPointcut(null, XxlJob.class, true); | ||
} | ||
|
||
@Override | ||
public Pointcut getPointcut() { | ||
return this.pointcut; | ||
} | ||
|
||
@Override | ||
public Advice getAdvice() { | ||
return this.advice; | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...rc/main/java/org/ballcat/autoconfigure/xxljob/trace/TraceXxlJobAnnotationInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.ballcat.autoconfigure.xxljob.trace; | ||
|
||
import java.util.UUID; | ||
|
||
import com.xxl.job.core.context.XxlJobHelper; | ||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
import org.slf4j.MDC; | ||
|
||
/** | ||
* 增强 XxlJob Trace 能力的拦截器,在 jobHandler execute 前在 MDC 中置入 traceId, 方便追踪上下文日志。 | ||
* | ||
* @author Hccake | ||
*/ | ||
public class TraceXxlJobAnnotationInterceptor implements MethodInterceptor { | ||
|
||
private final static String TRACE_ID = "traceId"; | ||
|
||
@Override | ||
public Object invoke(MethodInvocation invocation) throws Throwable { | ||
MDC.put(TRACE_ID, generateTraceId()); | ||
try { | ||
return invocation.proceed(); | ||
} | ||
finally { | ||
MDC.remove(TRACE_ID); | ||
} | ||
} | ||
|
||
/** | ||
* 生成 traceId, 默认使用 jobId 拼接无下划线的 UUID | ||
* @return traceId | ||
*/ | ||
protected String generateTraceId() { | ||
long jobId = XxlJobHelper.getJobId(); | ||
String simpleUUID = UUID.randomUUID().toString().replace("-", ""); | ||
return jobId + "-" + simpleUUID; | ||
} | ||
|
||
} |