fixes #735 Concurrency conflicts in Spring plugin

This commit is contained in:
YunaiV 2018-01-11 09:48:42 +08:00
parent df67393bff
commit 8854921f9e
2 changed files with 16 additions and 11 deletions

View File

@ -18,25 +18,26 @@
package org.apache.skywalking.apm.plugin.spring.mvc.commons;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.NativeWebRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
public class EnhanceRequireObjectCache {
private PathMappingCache pathMappingCache;
private NativeWebRequest nativeWebRequest;
private HttpServletResponse httpResponse;
private ThreadLocal<NativeWebRequest> nativeWebRequest = new ThreadLocal<NativeWebRequest>();
private ThreadLocal<HttpServletResponse> httpResponse = new ThreadLocal<HttpServletResponse>();
public void setPathMappingCache(PathMappingCache pathMappingCache) {
this.pathMappingCache = pathMappingCache;
}
public HttpServletResponse getHttpServletResponse() {
return httpResponse == null ? (HttpServletResponse)nativeWebRequest.getNativeResponse() : httpResponse;
return httpResponse.get() == null ? (HttpServletResponse) nativeWebRequest.get().getNativeResponse() : httpResponse.get();
}
public void setNativeWebRequest(NativeWebRequest nativeWebRequest) {
this.nativeWebRequest = nativeWebRequest;
this.nativeWebRequest.set(nativeWebRequest);
}
public String findPathMapping(Method method) {
@ -52,10 +53,11 @@ public class EnhanceRequireObjectCache {
}
public void setHttpResponse(HttpServletResponse httpResponse) {
this.httpResponse = httpResponse;
this.httpResponse.set(httpResponse);
}
public HttpServletResponse getHttpResponse() {
return httpResponse;
return httpResponse.get();
}
}

View File

@ -18,13 +18,14 @@
package org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletResponse;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
import org.apache.skywalking.apm.plugin.spring.mvc.commons.EnhanceRequireObjectCache;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
public class InvokeHandlerMethodInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
@ -37,11 +38,13 @@ public class InvokeHandlerMethodInterceptor implements InstanceMethodsAroundInte
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
if (allArguments[2] instanceof EnhancedInstance) {
((EnhanceRequireObjectCache)((EnhancedInstance)allArguments[2]).getSkyWalkingDynamicField()).setHttpResponse(null);
}
return ret;
}
@Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
Class<?>[] argumentsTypes, Throwable t) {
}
}