I am working with a Java web application backend. We have a Servlet that in the init() method has
public static final ThreadLocal<HttpServletRequest> REQUEST_HOLDER = new ThreadLocal<HttpServletRequest>();
In the service() method
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
the Servlet then sets the value of REQUEST_HOLDER with the request.
REQUEST_HOLDER.set(request);
Later in the servlet, I want to do some heavy processing on another thread, but still reference the request from the ThreadLocal REQUEST_HOLDER, which is technically on a different thread. Is this possible? how? (new to multithreading)
Although this general concept has largely been addressed in ThreadLocal value access across different threads , I want to know if there is anything unique regarding servlets / http requests that might provide a different solution. If not, not.
Also is it possible to take the REQUEST_HOLDER ThreadLocal and override the get() method so that if it is null (as in when called from a different thread) it returns a different value? - thinking of storing the request somewhere else and have the get() return that value.