2

I don’t understand an idea of AsyncResult class. From the tutorial I have learned that it works just as FutureTask class (but AsyncResult is serializable, so it can be send to local or remote client). However, doc says that this class' methods should not be called so I can only create and return instance of this class:

@Asynchronous
public Future<String> processPayment(Order order) throws PaymentException {
  ...
  String status = ...;
  return new AsyncResult<String>(status);
}

So what kind of object will client get?

Can I write

@Asynchronous
public  AsyncResult<String> processPayment...

?.

Will container do something to cancel asynchronous task after invoking AsyncResult’s/Future's cancel(false) method?

EDIT: I have found answer in this thread.

Community
  • 1
  • 1
Damian
  • 2,930
  • 6
  • 39
  • 61

1 Answers1

0

You can find the the rationale on the AsyncResult class documentation:

Note that this object is not passed to the client. It is merely a convenience for providing the result value to the container. Therefore, none of its instance methods should be called by the application.

Using the (correct) signature defined in your first snippet, a client will receive a simple Future as in :

@Singleton
public class AsyncClient{
   @Inject PaymentProcessor proc;
   public String invokePaymentProcessor(Order order){
         Future<String> result=proc.processPayment(order);
         // should not block.... the container instantiates a Future and 
         // returns it immediately
         return result.get(); // gets the string (blocks until received)
   }

}

Surely, if the container has not begun the method invocation (i.e the asyncronous invocation is still in the processig queue), cancel(false) should cancel the invocation (remove it from the queue), otherwise you should specify cancel(true) and check SessionContext.wasCancelled in the eventual processing loop of PaymentProcessor.processPayment

Carlo Pellegrini
  • 5,656
  • 40
  • 45