我有以下代码片段:

private ValContainer<CompletableFuture<String>> valContainer = new ValContainer<>();

public void fetchAsync(final String attrName) {

        QueryAsyncCallback asyncCallback = new QueryAsyncCallback() {
            @Override
            public void onSuccess(Message response) {
                final Data value = response.results().get("value");
                String text = value.toString();
                CompletableFuture<String> result = CompletableFuture.completedFuture(text);
                valContainer.setVal(result);
            }

            @Override
            public void onError(CallbackError error) {
                LOG.info("Error occurred");
            }

            @Override
            public void onTimeout() {
                LOG.info("Timeout occurred");
            }
        };

        Message request = new Messsage("com.example.val", "local", 0, "fetch");
        request.parameters().put("name", attrName);

        MsgOptions options = new MsgOptions(5000);
        try {
            queue.query(request, options, asyncCallback);
        } catch (QueueAccessException e) {
            e.printStackTrace();
        }
    }


现在,我可以轻松获取在onSuccess()回调中查询的值。 ValContainer只是一个通用容器,因此我可以将值保存在匿名类中。不幸的是,在Main函数中,我试图访问此valContainer,但可悲的是,由于这是异步查询,因此我得到了NullPointerException。

...
((JavaServiceImpl) javaService).fetchAsync("test");
System.out.println("inMain value: " + ((JavaServiceImpl) javaService).getValContainer().getVal().get()); <-- this is going to be NullPointerException
...


然后,在100ms之后,我在onSuccess()方法中获取了值。在这里基本上做Thread.sleep(100)就足以获取我希望获得的值,但我真的不想阻塞线程。有没有办法整齐地做到这一点?

最佳答案

不要在val中保留未初始化的ValContainer。将其初始化为new CompletableFuture<>()

private ValContainer<CompletableFuture<String>> valContainer = new ValContainer<>(new CompletableFuture<>());


public class ValContainer<T> {
    T val;

    public ValContainer(T val) {
        this.val = val;
    }
}


然后将您的onSuccess方法更改为

String text = value.toString();
valContainer.getVal().complete(text);


您无需在CompletableFuture方法中创建onSuccess

这样,[..]getValContainer().getVal().get()会阻塞直到将来完成。

07-24 09:18