Spring enabled cache who’s scope is set to Request need to be accessed by a singleton bean which is not under the request scope.
Spring enables you to create cache which keeps data for a request scope . For e.g.
And you can use this cache around any method which you would like to cache
Now it’s all good if you call this method from any controller which has a request context, i.e. this method is called from any other method of spring bean which is serving a web request.
But things become tricky if you need to call it from a thread pool or a fork join pool . Suppose you get a request and you need to spawn multiple threads to run concurrently to collect data to server the request.
These threads are running out side the context of your web request thread so any Thread Local value set on web request thread wont be available to these threads .
So if you end up calling the above method (annotated to use cache) from these pool threads you would get exceptions from spring like :
But there is an easy way to fix it :
- Get Request Attribute from the web request thread
2. Pass this attribute to your custom thread coming from pool or fork/join . Basically can be done by creating the runnable object with this attribute in its constructor
3. Set the request attribute before calling the method marked to use request scope cache
This would set the attributes in the thread local of current thread which can be used to call the above method.
Synthetic request in Test cases
Now if you are testing your method from junit, there may be a chance that you don’t have request object at all .
So you can create one and use it as above to populate attributes to test
Published on Java Code Geeks with permission by Abhijeet Iyengar, partner at our JCG program. See the original article here: Access spring Request scope cache in Singelton bean called from fork-join/thread pool
Opinions expressed by Java Code Geeks contributors are their own.