gpt4 book ai didi

java - 尝试将 AWS X-Ray 与 java spring @Scheduled 一起使用时收到 SubsegmentNotFoundException

转载 作者:行者123 更新时间:2023-12-05 06:55:25 24 4
gpt4 key购买 nike

我在我的 Java Spring 项目中实现 AWS X-Ray,一般来说,它工作正常。记录请求,我可以在 AWS 控制台上看到跟踪。

但是,在特定情况下,它会在启动应用程序时产生异常。

我调试了一下代码,问题出在一个任务调度类,这个类是@EnableScheduling注解的。如果该类中有任何带有 @Scheduled 注释的方法,应用程序将返回以下错误:

2020-12-17 22:50:51.427 ERROR 24379 --- [pool-2-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.

com.amazonaws.xray.exceptions.SubsegmentNotFoundException: Failed to end subsegment: subsegment cannot be found.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_265]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_265]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_265]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_265]
at com.amazonaws.xray.strategy.RuntimeErrorContextMissingStrategy.contextMissing(RuntimeErrorContextMissingStrategy.java:32) ~[aws-xray-recorder-sdk-core-2.8.0.jar:na]
at com.amazonaws.xray.contexts.ThreadLocalSegmentContext.endSubsegment(ThreadLocalSegmentContext.java:93) ~[aws-xray-recorder-sdk-core-2.8.0.jar:na]
at com.amazonaws.xray.AWSXRayRecorder.endSubsegment(AWSXRayRecorder.java:627) ~[aws-xray-recorder-sdk-core-2.8.0.jar:na]
at com.amazonaws.xray.AWSXRay.endSubsegment(AWSXRay.java:125) ~[aws-xray-recorder-sdk-core-2.8.0.jar:na]
at com.amazonaws.xray.spring.aop.BaseAbstractXRayInterceptor.processXRayTrace(BaseAbstractXRayInterceptor.java:58) ~[aws-xray-recorder-sdk-spring-2.8.0.jar:na]
at com.amazonaws.xray.spring.aop.BaseAbstractXRayInterceptor.traceAroundMethods(BaseAbstractXRayInterceptor.java:43) ~[aws-xray-recorder-sdk-spring-2.8.0.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_265]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_265]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_265]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_265]
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627) ~[spring-aop-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616) ~[spring-aop-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) ~[spring-aop-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168) ~[spring-aop-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671) ~[spring-aop-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at com.domain.common.ToolBox$$EnhancerBySpringCGLIB$$a2712230.convertStackTraceToString(<generated>) ~[classes/:na]
at com.domain.event.ScheduledEventHandler.runsEvery1Minute(ScheduledEventHandler.java:111) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_265]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_265]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_265]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_265]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) ~[spring-context-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_265]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_265]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_265]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_265]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_265]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_265]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_265]

我尝试使用自定义子分段,如:Generating custom subsegments with the X-Ray SDK for Java什么都没有。

代码看起来像这样:

@XRayEnabled
@Component
@EnableScheduling
public class ScheduledEventHandler {

...

@Scheduled(fixedDelay = 1*60*1000)
protected void runsEvery1Minute() {
...
}

...
}

在我看来,@XRayEnabled 注释与@Scheduled 注释有些不兼容,或者对于这种情况有特定的配置。

谁能帮我解决这个问题?

谢谢!

最佳答案

我遇到了同样的问题。您可以将预定方法移动到未使用 @XRayEnabled 注释的不同类。然后你可以添加

AWSXRay.beginSegment("youSegmentName");
...
call class annotated with @XRayEnabled
...
AWSXRay.endSegment();

关于java - 尝试将 AWS X-Ray 与 java spring @Scheduled 一起使用时收到 SubsegmentNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65350982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com