Skip to content
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

Jersey creates multiple provider instances if a class implements more than one provider interface #3796

Closed
jerseyrobot opened this issue Mar 30, 2018 · 4 comments

Comments

@jerseyrobot
Copy link
Contributor

JAX-RS Specification 2.1 Chapter 4.1 Line 1: "By default a single instance of each provider class is instantiated for each JAX-RS application."

Jersey clearly violates this rule as it creates one instance of such a class per provider interface this class implements. For example, in case a class is annotated with @Provider and implements both ContainerRequestFilter and ContainerResponseFilter then Jersey creates two instances, instead one instance. In case this class additionally implements Feature then Jersey even creates a third instance.

Can be reproduce using the following code:

@Provider
public class MyProvider implements Feature, ContainerRequestFilter, ContainerResponseFilter {
	@Override
	public boolean configure(FeatureContext context) {
		System.out.println("Feature.this = " + this);
		return true;
	}
	@Override
	public void filter(ContainerRequestContext requestContext) {
		System.out.println("ContainerRequestFilter.this = " + this);
	}
	@Override
	public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
		System.out.println("ContainerResponseFilter.this = " + this);
	}

}

Here is the result it prints after the first invocation finished:

Feature.this = eu.headcrashing.bugs.MyProvider@6cee903a
...
ContainerRequestFilter.this = eu.headcrashing.bugs.MyProvider@575a86ae
ContainerResponseFilter.this = eu.headcrashing.bugs.MyProvider@69767b65

To be compliant to the mentioned chapter of the spec, the result MUST be this instead:

Feature.this = eu.headcrashing.bugs.MyProvider@6cee903a
...
ContainerRequestFilter.this = eu.headcrashing.bugs.MyProvider@6cee903a
ContainerResponseFilter.this = eu.headcrashing.bugs.MyProvider@6cee903a
@jerseyrobot
Copy link
Contributor Author

@mkarg Commented
Please see this JAX-RS API issue for reference: jakartaee/rest#605.

@jerseyrobot
Copy link
Contributor Author

@mkarg Commented
FYI: Using jVisualVM I noticed that the three mentioned instances actually are kept in memory even after a GC got performed. This looks like a memory leak, as at least the Feature instance is of no use at runtime (the other two instances actually get used by every invocation).

@jerseyrobot
Copy link
Contributor Author

@mkarg
Copy link
Member

mkarg commented May 10, 2018

I am the original reporter.

@senivam senivam added the 2.29 label Mar 7, 2019
@senivam senivam closed this as completed Mar 7, 2019
@senivam senivam added this to the 2.29 milestone Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants