2

从请求范围的线程中,CompletableFutures 必须由在执行程序中运行的任务完成。MessageService提供的供应商使用会话范围内的特定领域服务。该服务由 Guice 注入。

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 {
    ...
}

Guice 在尝试注入MessageRestClient.

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) 但我看不到使用它的方法,因为没有 Callables 可以发挥作用。你能帮我解决一下吗?

4

2 回答 2

3

在 中处理 servlet 请求时GuiceGuiceFilter已经注意设置适当的上下文(通过 a ThreadLocal),以便它可以知道您在哪个请求中,从而正确应用范围。带有注释的类的实例SessionScope实际上是可以从 Guice 访问请求和会话信息并采取相应行动的代理。

您发送到的任务CompletableFuture在失控的单独线程中运行Guice。没有ThreadLocal可用的地方Guice可以从哪里获得该信息,因此,没有Request或没有Session信息,这意味着,没有[SessionScope。由于代理对会话一无所知,因此会引发您遇到的错误。

ServletScopes.transferRequest方法应该注入所需的信息,以便范围工作。应该像这样工作(但从未尝试过):

   Callable<MessageMetaDataSet> c = ServletScopes.transferRequest(
               () -> messageService.retrieveMessageMetadataSet(x, y));

   CompletableFuture.supplyAsync(
    () -> c.call()
    , executorService);
于 2015-11-26T18:47:56.470 回答
0

尽早尝试:Guice @Inject 一切正常:



    import java.util.concurrent.CompletableFuture;

    public class AsyncFire {

        public static > void execAsync(Class asyncClass) {
            T asyncInstance = AsyncInjector.getInjector().getInstance(asyncClass); //magic
            CompletableFuture completableFuture = CompletableFuture.supplyAsync(asyncInstance); //business logic
            completableFuture.exceptionally(asyncInstance); //if error
            completableFuture.thenAccept(asyncInstance); //if success
        }

    }



    public class ExampleAsync extends Async {

        @Inject //it works
        private EntityManager entityManager;

        @Inject //it works
        private DAO dao; //your

    }



    import java.util.function.Consumer;
    import java.util.function.Function;
    import java.util.function.Supplier;

    public abstract class Async implements Supplier, Consumer, Function {
        //...
    }



    import com.google.inject.Guice;
    import com.google.inject.Injector;
    import com.google.inject.persist.PersistService;

    public final class AsyncInjector {

        private static Injector injector = null; 

        public static Injector getInjector() {
            if (injector == null) {
                synchronized (AsyncInjector.class) {
                    if (injector == null) {
                        injector = Guice.createInjector(new MyGuiceModule()); //your guice module
                        PersistService service = injector.getInstance(PersistService.class);
                        service.start();
                    }
                }
            }
            return injector;
        }

    }

希望这对某人有帮助!

于 2016-07-19T22:30:17.173 回答