4

I'm writing a RESTful web service where I'm trying to inject a request scoped service class into a filter. I have gone through Paul Samsotha's blog on how to inject request scoped services using proxies.

Here's my implementation:

The supplier for this:

public class FileServiceSupplier implements Supplier<FileService> {

    @Inject
    ContainerRequestContext context;

    public FileService get() {
        String something = context.get("something");
        new FileService(something);
    }
}

and I'm binding the supplier here

import org.glassfish.jersey.internal.inject.AbstractBinder;
...
public class CustomDependencyBinder extends AbstractBinder {
    @Override
    protected void configure() {
        bindFactory(FileServiceSupplier.class)
            .proxy(true)
            .proxyForSameScope(false)
            .to(FileService.class)
            .in(RequestScoped.class);
    }
}

public class MyWebService extends ResourceConfig {
    public MyWebService() {
        register(new CustomDependencyBinder());
    }
}

But now when I'm injecting this into a filter:

public class FileScanningFilter implements ContainerRequestFilter {

    @Inject
    private FileService fileService;

    @Inject
    private ResourceInfo resourceInfo;

    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        boolean status = fileService.assertStatus();
    }
}

@Provider
public class FileScanningFeature implements DynamicFeature {
    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
        featureContext.register(FileScanningFilter.class);
    }
}

But now when I send a request I get the following error:

"There is more than one active context for org.glassfish.jersey.process.internal.RequestScoped"  "java.lang.IllegalStateException: There is more than one active context for org.glassfish.jersey.process.internal.RequestScoped
    at org.jvnet.hk2.internal.ServiceLocatorImpl._resolveContext(ServiceLocatorImpl.java:2193)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.access$000(ServiceLocatorImpl.java:105)
    at org.jvnet.hk2.internal.ServiceLocatorImpl$3.compute(ServiceLocatorImpl.java:165)
    at org.jvnet.hk2.internal.ServiceLocatorImpl$3.compute(ServiceLocatorImpl.java:161)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:74)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:131)
    at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:176)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.resolveContext(ServiceLocatorImpl.java:2207)
    at org.jvnet.hk2.internal.MethodInterceptorImpl.internalInvoke(MethodInterceptorImpl.java:64)
    at org.jvnet.hk2.internal.MethodInterceptorImpl.invoke(MethodInterceptorImpl.java:101)
    at org.jvnet.hk2.internal.MethodInterceptorInvocationHandler.invoke(MethodInterceptorInvocationHandler.java:39)
    at com.sun.proxy.$Proxy88.getResourceMethod(Unknown Source)
    at com.test.FileScanningFilter.filter(AuthFilter.java:56)

Even though I inserted it once in the filter, it is saying there are multiple contexts.

I'm using Jersey 2.28

P.S: For the AbstractBinder , I followed this answer

Termin4t0r
  • 199
  • 3
  • 10
  • Can you put (reproduce) this into a single copy/paste/runnable JerseyTest? [For example](https://gist.github.com/psamsotha/d7f782fff4a9be7b04f80fdde416294e) – Paul Samsotha Jul 31 '19 at 08:46
  • @PaulSamsotha I'm not able to reproduce the exact error, but can only produce the proxy creation error [test](https://gist.github.com/the-boring-dev/a4efed38cb3dbf14214dc23fdfcf18f3) – Termin4t0r Aug 01 '19 at 03:37

0 Answers0