2012-04-25 135 views
2

我不明白AsyncResult类的想法。 从tutorial我了解到它的工作原理与FutureTask类一样(但AsyncResult是可序列化的,所以它可以发送到本地或远程客户端)。 然而,doc说,此类方法不应该叫,所以我只能创建并返回这个类的一个实例:AsyncResult可以被认为是可序列化的FutureTask吗?

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

那么什么样的对象将客户端就取得?

我可以写

@Asynchronous 
public AsyncResult<String> processPayment... 

在调用AsyncResult/Future的cancel(false)方法后容器是否会取消异步任务?

编辑: 我找到了答案thread

回答

0

您可以找到AsyncResult类文档的理由:

注意,该对象未传递给客户端。对于向容器提供结果值仅仅是一个方便。 因此,它的实例方法都不应该由 应用程序调用。

使用在第一个片段中定义的(正确)的签名,客户端将收到一个简单的将来成为:

@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) 
    } 

} 

当然,如果容器还没有开始的方法调用(即asyncronous调用仍然在processig队列中),cancel(false)应取消调用(将其从队列中移除),否则应在最终处理循环中指定cancel(true)并检查SessionContext.wasCancelledPaymentProcessor.processPayment

相关问题