I have a single instance class that implements ExceptionMapper. This is not a static class, but the class for which I know only one instance is created (I checked - the constructor is called only once).
My class uses @Context HttpServletRequest, and I can clearly notice that when calling ExceptionMapper.toResponse (), the @Context parameter "request" has a value that is relevant to the request in which the exception is thrown.
Doc says this is a really supported feature, and this is done using a "proxy".
I wonder how exactly this is implemented - how can one instance have different values โโof member variables at the same time?
Thanks,
AG
PS: here is the test code:
@Provider public class MyExceptionMapper implements ExceptionMapper<Exception> { public MyExceptionMapper() { System.out.println("CTOR!!"); } @Context HttpServletRequest req; public static boolean done = false; public Response toResponse(Exception ex) { if (!done) { done = true; Thread.sleep(10000); } System.out.println(req.getRequestURI()); return null; } }
The handler method of my REST throws an exception, so when I execute the following two requests "in parallel" (the above dream ensures that the first one will not be completed when the second one arrives, and IMHO must change the same "req" field):
- http://localhost/app/one - http://localhost/app/two
my program prints:
CTOR! http://localhost/app/one http://localhost/app/two
java rest jersey jax-rs
Wanna know all
source share