從請求作用域線程中,CompletableFuture
必須由運行在執行程序中的任務完成。所提供的供應商使用具有會話範圍的域特定服務MessageService
。該服務由Guice注入。Guice在執行CompletableFuture時拋出OutOfScopeException
public class MessageProcessingPage {
private MessageService messageService;
@Inject
public MessagProcessingPage (MessageService messageService) {
this.messageService = messageService;
}
// Called by request scoped thread.
public void onProcessMessagesButton() {
ExecutorService executorService = Executors.newFixedThreadPool(3);
CompletableFuture.supplyAsync(
// Called from a thread from the threadpool.
() -> {return messageService.retrieveMessageMetadataSet(x, y);}
, executorService);
...
}
...
}
的MessageService
具有(會話範圍)MessageRestClient
將其注入。
@SessionScoped
public class MessageService {
private MessageRestClient messageRestClient;
@Inject
public MessageRestClient (MessageRestClient messageRestClient) {
this.messageRestClient = messageRestClient;
}
public MessageMetaDataSet retrieveMessageMetadataSet(x, y) {
List<MessageMetaData> listOfMetaData = messageRestClient.retrieve(x, y, z);
...
}
...
}
@SessionScoped
public class MessageRestClient {
...
}
當它試圖注入MessageRestClient
時,Guice陷入困境。
java.util.concurrent.CompletionException: com.google.inject.ProvisionException: Unable to provision, see the following errors:
1) Error in custom provider, com.google.inject.OutOfScopeException: Cannot access scoped [MessageRestClient]. Either we are not currently inside an HTTP Servlet request, or you may have forgotten to apply com.google.inject.servlet.GuiceFilter as a servlet filter for this request.
我ServletScopes
閱讀的方法:public static <T> Callable<T> transferRequest(Callable<T> callable)
但我不明白的方式來使用它,因爲沒有可調用進來玩。你能幫我解決問題嗎?
其實它會值得一個新的線程,但因爲缺乏時間:我注意到在這種情況下應避免使用輔助注射。在'MessageService'中,我注入了一個'MessageFactory'(使用輔助注入),它被一個從任務調用的方法使用。吉斯也遇到了麻煩。由於我改變了普通工廠的輔助注射工廠,一切正常。 –