我有一个长期运行的资源任务,我想将其转换为异步请求。收到请求后,我从该请求中收集了一些元数据,然后构造了一个ApiRequest
对象并将其注入到ContainerRequestContext
中。
@PreMatching
@Provider
public class RequestBootstrap implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
String meta = //collect meta
ApiRequest apiRequest = new ApiRequest(meta);
requestContext.setProperty("request", apiRequest);
}
}
我已经定义了Factory
以便从资源类的上下文中检索此类。
这是Factory
类:
public class ApiRequestFactory implements Factory<ApiRequest> {
private final ContainerRequestContext requestContext;
@Inject
public ApiRequestFactory(ContainerRequestContext context) {
this.requestContext = context;
}
@Override
public ApiRequest provide() {
return (ApiRequest) requestContext.getProperty("request");
}
@Override
public void dispose(ApiRequest apiRequest) {
}
}
我将其绑定为RequestScoped
public class MyAppBinder extends AbstractBinder {
@Override
protected void configure() {
bindFactory(ApiRequestFactory.class)
.proxy(true)
.proxyForSameScope(false)
.to(ApiRequest.class)
.in(RequestScoped.class);
}
}
现在,我从上下文中将其注入资源类中:
@RequestScoped
@Path("/async/file/{fileName}") //No I18N
public class AsyncGetFile {
@Context
private ApiRequest apiRequest;
@GET
public void asyncGetFile(@Suspended final AsyncResponse asyncResponse,
@Context Request httpRequest,
@Context ContainerRequestContext requestContext,
@Context HttpHeaders httpHeaders) throws Exception {
new Thread(() -> {
try {
Response response = readFile(apiRequest, httpRequest, requestContext, httpHeaders);
asyncResponse.resume(response);
} catch (Exception e) {
asyncResponse.resume(e);
}
}).start();
}
private Response getObject(ApiRequest apiRequest,
Request httpRequest,
ContainerRequestContext requestContext,
HttpHeaders httpHeaders) throws Exception {
// get the file
}
}
但是我收到此错误:java.lang.IllegalStateException: Not inside a request scope.
此注入对于同步请求一直运行良好,但是对于异步调用,我只收到此错误。
我尝试在构造函数中执行@Inject
,但仍然遇到相同的错误。
这是完整的堆栈跟踪:
The following warnings have been detected: WARNING: HK2 failure has been detected in a code that does not run in an active Jersey Error scope.
WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
java.lang.IllegalStateException: Not inside a request scope.
at org.glassfish.jersey.internal.guava.Preconditions.checkState(Preconditions.java:169)
at org.glassfish.jersey.process.internal.RequestScope.current(RequestScope.java:153)
at org.glassfish.jersey.inject.hk2.RequestContext.findOrCreate(RequestContext.java:55)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2102)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:92)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:675)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:54)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:188)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:205)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:334)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:46)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2102)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:92)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:66)
at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:151)
at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:518)
at org.glassfish.jersey.inject.hk2.RequestContext.lambda$findOrCreate$0(RequestContext.java:60)
at org.glassfish.jersey.internal.inject.ForeignDescriptorImpl.dispose(ForeignDescriptorImpl.java:63)
at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.remove(Hk2RequestScope.java:126)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.release(Hk2RequestScope.java:143)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:894)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:848)
at com.devresource.routes.AsyncGetFile.lambda$asyncGetObject$1(AsyncGetFile.java:74)
at java.lang.Thread.run(Thread.java:748)
MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.devresource.core.ApiRequest errors were found
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:224)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:334)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:46)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2102)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:92)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:66)
at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:151)
at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:518)
at org.glassfish.jersey.inject.hk2.RequestContext.lambda$findOrCreate$0(RequestContext.java:60)
at org.glassfish.jersey.internal.inject.ForeignDescriptorImpl.dispose(ForeignDescriptorImpl.java:63)
at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.remove(Hk2RequestScope.java:126)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.release(Hk2RequestScope.java:143)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:894)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:848)
at com.devresource.routes.AsyncGetFile.lambda$asyncGetObject$1(AsyncGetFile.java:74)
at java.lang.Thread.run(Thread.java:748)
MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on com.devresource.core.ApiRequest
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:363)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:463)
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:46)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2102)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:92)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:66)
at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:151)
at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:518)
at org.glassfish.jersey.inject.hk2.RequestContext.lambda$findOrCreate$0(RequestContext.java:60)
at org.glassfish.jersey.internal.inject.ForeignDescriptorImpl.dispose(ForeignDescriptorImpl.java:63)
at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.remove(Hk2RequestScope.java:126)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.glassfish.jersey.inject.hk2.Hk2RequestScope$Instance.release(Hk2RequestScope.java:143)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:894)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:848)
at com.devresource.routes.AsyncGetFile.lambda$asyncGetObject$1(AsyncGetFile.java:74)
at java.lang.Thread.run(Thread.java:748)
我正在使用Tomcat 9和Jersey 2.29