2

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.

4
  • Why aren't you simply defining: public Future<Boolean> runProcess(){ return stateLess.compute(); }, what's the purpose of this extra variable? Commented Aug 17, 2012 at 11:38
  • because i will be calling more of similar methods in runProcess() right now theres only one for testing purpose, same with that boolean in conversationScoped bean, there will be more Commented Aug 17, 2012 at 11:44
  • first find which value is null, looking at the stack trace: is at stateFull.runProcess() or at stateFull.getFuture().get()? Commented Aug 17, 2012 at 11:49
  • it is selected in code where nullpointer occures, at stateFull.getFuture().get() Commented Aug 17, 2012 at 11:56

1 Answer 1

3

NPE happens because starting new thread is a heavy operation, and when you call stateFull.getFuture().get(); new thread is not started yet(so feature is null).

Here the right way of using @Async with Future.

Sign up to request clarification or add additional context in comments.

4 Comments

nice idea but looks like not source of problem. I tryed to set Thread object like this thisThread = Thread.currentThread(); in SomeBean of this Asynchronous call and than check in while cycle till its alive in conversationScoped bean but nullPointer remains
future filed must be at least volatile - because it updated and obtained in different threads.
ok so now it stopped throwing NPE but IllegalStateException started. Object does not represent an acutal Future
great thanks you very much. The last exception was cause by me using get() with timeout attribute :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.