JBoss Community

Cancelling an @Asynchronous method with Future.cancel

created by david_b in EJB3 - View the full discussion

I am writing an enterprise Java application that uses aynchronous EJB 3.1 methods to execute a number of tasks in parallel. The app is currently running on JBoss AS 6.0.0 Final.

 

To support cancelling a long running task I have been attempting to use the Future interface. Unfortunately calling future.cancel(true)appears to have no effect on the session context of the bean executing the task, despite the fact that cancel is returning true.

 

I have a simple interface:

 

public interface AsyncInterface

{

    Future run() throws Exception;

}

 

With a bean implementation as follows:

 

@Stateless@Remote(AsyncInterface.class)public class AsyncBean{    @Resource SessionContext myContext;    @Asynchronous    public Future(result);     }}

 

 

The asynchronous method is called from another Stateless EJB like so:

 

InitialContext ctx = new InitialContext();AsyncInterface async = (AsyncInterface)ctx.lookup("AsyncBean/remote");Future future = async.run();if( future.cancel(true) ){     System.out.println("future.cancel() returned true");}else{     System.out.println("future.cancel() returned false");}

 

 

The output from the AsyncBean is an endless stream of "Working"; it never detects the cancellation.

 

I haven't found much sample code using Futures to cancel an @Asynchronous EJB call. Is using the session context the correct way of checking for cancellation? Is there a better method for cancelling the asynchronous call?

Reply to this message by going to Community

Start a new discussion in EJB3 at Community