在基于RestEasy的服务中将服务器请求和响应过滤器用于ThreadLocal存储 [英] Using Server Request and Response filters for ThreadLocal storage in a RestEasy based service

查看:178
本文介绍了在基于RestEasy的服务中将服务器请求和响应过滤器用于ThreadLocal存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究基于RESTeasy的RESTful服务.我有一个过滤器类,可以用作服务器请求过滤器和服务器响应过滤器(即它实现ContainerRequestFilterContainerResponseFilter接口).

I am currently working on a RESTeasy based RESTful service. I have a filter class which serves as a server request filter as well as a server response filter (i.e. it implements ContainerRequestFilter and ContainerResponseFilter interfaces).

在请求开始时,我使用过滤器将对象放入ThreadLocal.整个请求中的资源都使用此对象.在请求结束时,在发送响应之前,过滤器会从ThreadLocal中删除对象.

At the beginning of the request, I use the filter to put an object into ThreadLocal. This object is used by the resources throughout the request. At the end of the request, before sending out the response, the filter removes the object from ThreadLocal.

我的问题是,是否可以保证请求过滤器,资源和响应过滤器都将在同一线程中执行?

My question is that is there a guarantee that the the request filter, the resource and the response filter will all execute in the same thread? Is there a possibility that after the request filter puts the object into ThreadLocal, a different thread will execute the request (and thus not have access to the object)?

我确定确实如此,但是随后我看到了

I was sure that this was the case but then I saw this http://jersey.576304.n2.nabble.com/Does-filter-method-of-ContainerRequestFilter-run-in-resource-method-thread-td7582648.html (official Jersey forum) and now I have doubts.

推荐答案

javax.ws.rs.container.ContainerRequestContext.setProperty(...)

javax.ws.rs.container.ContainerRequestContext.setProperty(...)

javax.ws.rs.container.ContainerRequestContext.getProperty(...)

javax.ws.rs.container.ContainerRequestContext.getProperty(...)

可能是正确的方法. Javadoc指出:

are probably the right approach. The javadoc states:

在Servlet容器中,属性与ServletRequest同步,并公开ServletRequest中可用的所有属性.对属性的任何修改也将反映在关联的ServletRequest的属性集中.

In a Servlet container, the properties are synchronized with the ServletRequest and expose all the attributes available in the ServletRequest. Any modifications of the properties are also reflected in the set of properties of the associated ServletRequest.

这篇关于在基于RestEasy的服务中将服务器请求和响应过滤器用于ThreadLocal存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆