I'm new to java.util.concurrent package and i came to a problem with Future object.
This is a 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 looks like this.
@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);
}
}
And now to my problem. I call doSomething() method and i think that according to documentation of Future.get() it should call runProcess() and than wait at
comp = stateFull.getFuture().get();
until future in SomeBean is completed from AnotherBean, but it just keep throwing NullPointerException. Anyone knows why it can be be happening?
-------------------EDIT-----------------------
NullPointer has been corrected. Now i have another problem. Let's say that i set more Future objects in Somebean by calling more methods in runProcess(). And then i want to update my page everytime the Future object get a result to see the progress. How do i do that? Right now i use
private void updateView(){
RequestContext ctx = RequestContext.getCurrentInstance();
ctx.update("mainFrm:info");
}
under every Boolean in doSomething() method, but it doesnt do what i want. All booleans just appear all at once.
public Future<Boolean> runProcess(){ return stateLess.compute(); }, what's the purpose of this extra variable?