我是java.util.concurrent
软件包的新手,我遇到了Future
对象的问题。
这是一个conversationScoped bean。
@Inject SomeBean stateFull;
Boolean comp = false, comp1 = false;
public void doSomething(){
stateFull.runProcess();
try {
comp = stateFull.getFuture().get();
System.out.println("Future "+syncEJB.getFuture().get());
updateView();
comp1 = stateFull.getFuture1().get();
System.out.println("Future "+syncEJB.getFuture().get());
updateView();
} catch (InterruptedException ex) {
Logger.getLogger(SynchronizationMB.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(SynchronizationMB.class.getName()).log(Level.SEVERE, null, ex);
}
}
SomeBean看起来像这样。
@Stateful
public class SomeBean{
@Inject AnotherBean stateLess;
private volatile Future<Boolean> future, future1;
@Asynchronous
public void runProcess(){
future = stateLess.compute();
future1 = stateLess.compute();
}
public Future<Boolean> getFuture() {
return future;
}
public Future<Boolean> getFuture1() {
return future1;
}
}
And AnotherBean:
@Stateless
public class AnotherBean{
@Asynchronous
public Future<Boolean> compute() {
boolean result;
System.out.println("--------------");
System.out.println("completed sync");
System.out.println("--------------");
result = true;
return new AsyncResult<Boolean>(result);
}
}
现在我的问题。我打电话给doSomething()
方法,我认为根据Future.get()
的文档,它应该调用runProcess()
而不是等待
comp = stateFull.getFuture().get();
直到SomeBean中的未来从AnotherBean完成,但它只是继续抛出NullPointerException
。任何人都知道为什么会发生这种情况?
------------------- EDIT -----------------------
NullPointer已得到纠正。现在我有另一个问题。假设我通过在runProcess()中调用更多方法来在Somebean中设置更多Future对象。然后我希望每次Future对象获得结果以查看进度时更新我的页面。我怎么做?现在我用
private void updateView(){
RequestContext ctx = RequestContext.getCurrentInstance();
ctx.update("mainFrm:info");
}