New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot inject ContainerRequestContext to interceptor, reader, writer , exception mapper. #2627
Comments
@glassfishrobot Commented |
@glassfishrobot Commented can you put stack trace to the comments? The injection in your code should work. thanks |
@glassfishrobot Commented This is stack trace of this issue(WriterInterceptor). A MultiException has 3 exceptions. They are:|1. java.lang.IllegalStateException: Not inside a request scope.|2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.iijgio.gss.storage.api.rest.provider.interceptor.PolicyConvertToAmazonInterceptor errors were found|3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.iijgio.gss.storage.api.rest.provider.interceptor.PolicyConvertToAmazonInterceptor| {/,null,STARTING} : javax.servlet.ServletException: org.glassfish.jersey.servlet.ServletContainer-38dd01a5@cd6fd568==org.glassfish.jersey.servlet.ServletContainer,-1,false {HTTP/1.1}{0.0.0.0:80} added {sun.nio.ch.ServerSocketChannelImpl[/0:0:0:0:0:0:0:0:80],POJO} 2014-01-23 10:27:56.482:DBUG:oejuc.AbstractLifeCycle:main: oejuc.AbstractLifeCycle#setStarting(AbstractLifeCycle.java:184):starting org.eclipse.jetty.util.thread.ScheduledExecutorScheduler@4c01acb7 {HTTP/1.1} 2014-01-23 10:27:56.483:DBUG:oejuc.AbstractLifeCycle:main: oejuc.AbstractLifeCycle#setStarting(AbstractLifeCycle.java:184):starting org.eclipse.jetty.server.ServerConnector$ServerConnectorManager@6b667437 {HTTP/1.1}{0.0.0.0:80} 2014-01-23 10:27:56.498:WARN:oejuc.AbstractLifeCycle:main: oejuc.AbstractLifeCycle#setFailed(AbstractLifeCycle.java:209):FAILED org.eclipse.jetty.server.Server@38d8713: javax.servlet.ServletException: org.glassfish.jersey.servlet.ServletContainer-38dd01a5@cd6fd568==org.glassfish.jersey.servlet.ServletContainer,-1,false at org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:88) |
@glassfishrobot Commented I think this issue contains 2 problems. 1. cannot inject some request scope object to MessageBodyWrite, Interceptor, etc. The trigger of 2. are 1) inject ResourceContext by using @context annotaion 2) get HttpServletRequest from ResourceContext. like below.
HttpServletRequest can inject by @context but I wrote getting it from a ResourceContext. I captured GC activity by using Java VisualVM. PermGen is increased and decrease frequently and GC Time is too large(always run). heap dump(Java VisualVM) Is this code wrong implementation? or bug of jersey? Thanks. //Michitaka |
@glassfishrobot Commented Problem 1: Injection of ContainerRequestContext
I have a question before fixing the issue. Why do you need to inject ContainerRequestContext? What information do you need from the request? Is not the information already available in WriterInterceptorContext? I am interested if there is really a need to proxy ContainerRequestContext as such a solution could have negative performance impact. thanks. As a workaround it is better to use ServiceLocator from HK2 to inject services. ResourceContext should be used only for resource classes (but internally it does the same thing currently). The code will look like the following:
Another (nicer) solution is to use javax.inject.Provider:
MyWriterInterceptorContext is singleton (all providers are singletons) and injections are done only once at the startup time. The code above will work as Provider is not bound to any request but always return actual value for the current request by method get(). Problem 2: Too frequent Full GC Please check the issue #2576"). It deals with GC problem. Is your problem similar? Firstly please try to use different workaround for your case and if you still find a performance problems with GC then please create a separate issue (you can link it to this issue). We would need in this case more detailed test case (such a problems are usually difficult to reproduce). Simple maven project reproducing the issue would be the best. thanks |
@glassfishrobot Commented Question :Why do you need to inject ContainerRequestContext? I use ContainerRequestContext to share some data. My application has two behaviors(mode), these are same logic, but headers & a response body has a little bit difference(prefix, namespace). So, In pre-matching filter, decide mode by reading some headers, and set this decision (and some additional info) to ContainerRequestContext by using the setProperty() method. Filters, Interceptor and Writer/Reder read values by ContainerRequestContext#getPropery(), and modify headers and a body in specific mode. Error response is depend on that information , so I want to call ContainerRequestContext #getProperty() in ExceptionMapper. Problem 2: Too frequent Full GC I had already checked #2576"), and these are very similar. thanks. |
@glassfishrobot Commented Resolving this as invalid. Please feel free to re-open or comment on this if you think otherwise. Thanks. |
@glassfishrobot Commented |
@glassfishrobot Commented |
|
Hi,
We use Jetty 2.5.1 in our service.
Our application create some information(ex request id, flag, etc) at a pre-matching filter.
This information is read by a filter, resource, interceptor, exception mapper and entity reader/writer.
So, I set this info to ContainerRequestContext that I think it is suitable for.
First, I wrote @context annotation at field variable of a interceptor.
But error writing below happened.
| 1. java.lang.IllegalStateException: Not inside a request scope. |
So, I use @context to inject ResourceContext and get ContainerRequestContext from it.
In version 2.3,1, this implement is work well, but in 2.5.1 is not.
Too frequently Full GC is happened(is it bug??).
Our understanding is the request scope is begin at first filter and end with last filter.
WriterInterceptor,RederInterceptor,MessageBodyReader, ExceptionMapper and MessageBodyWriter is in a request scope.
I think below code should be work well, without throwing exception by jersey.
(But now, occur exception. Other class in mail title is as well.)
@Provider
public class Interceptor implements WriterInterceptor {
@context
ContainerRequestContext requestContext;
@OverRide
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
}
}
If this code is bad.. that is, not match JAX-RS spec or jersey design policy,
I hope that someone would give me other idea.
Thanks.
//Michitaka
Environment
CentOS 6.4, Java 1.7.0_40, jetty 9.1.1.v20140108(embedded)
Affected Versions
[2.5.1]
The text was updated successfully, but these errors were encountered: