tomcat异步线程javaAgent

最近需要做一个日志框架,针对controller 做一个Agent 切面;

在处理spring mvc 异步的时候,获取不了他的返回值

于是参考下面博客的一篇文章,对我很有启发

说一下,现在进程,

Tomcat Http A

Tomcat Http SyncB

Tomcat Http C

org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter.postProcess()

这个是异步线程回调的时候,执行的方法

Tomcat Http A,Tomcat Http C 他们的Request 和Reponse 是可以共用的,所以他们两个之间通信是不存在问题

但是日志实现是通过MDC,的线程变量实现的,所以A和B,或者B和C不能通过MDC 传递数据

参考以前项目的老代码,他只通过注册一个,CallableProcessingInterceptorAdapter子类,

通过复写org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter.beforeConcurrentHandling()

这个方法,保存到一个临时变量,在postProcess方法时候,再把它取出来,这个就是A,B,C的数据得到有效的传递

-------

说一下我的实现:

@Before(method = {"beforeConcurrentHandling"})
public void before(NativeWebRequest request, Callable<Object> task) {
request.setAttribute(youFlag, id, RequestAttributes.SCOPE_REQUEST);
  ..... 还正在 Tomcat Http A 线程中
}

@After(method = {"postProcess"})
public void after(NativeWebRequest request, Callable<Object> task, Object concurrentResult, @Thrown Throwable throwable) {
String data= (String) request.getAttribute(youFlag, RequestAttributes.SCOPE_REQUEST);
log(data+concurrentResult)
//还正在 Tomcat Http B 线程中
}

我的借用框架是promagent,通过NativeWebRequest request,来传递数据,

成功解决tomcat 异步线程数据传递

参考:http://blog.chinaunix.net/uid-27767798-id-3806685.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章