Jersey client filter thread safety
I am using a singleton instance of the jersey client configured through spring, with multiple threads that each set different authentication filters on the client. According to the docs
http://jersey.java.net/nonav/apidocs/1.3/jersey/com/sun/jersey/api/client/Client.html.
setting filters is not guaranteed to be thread safe, which means thread A is not guaranteed to be authent开发者_StackOverflow中文版icated as itself when other threads set filters simultaneously.
Is there a work around to this issue, other than creating a new client for each thread? I would like to avoid this as it is an expensive operation.
I don't believe you will be able to use a single client safely with multiple threads in this manner. I would suggest creating a pool of clients, and have each thread borrow a client from the pool, set the appropriate authentication filter, and then clear the filter before returning the client to the pool.
Apache commons has a pool component that would work well for this.
精彩评论