[jboss-user] [EJB3] - Cancelling an @Asynchronous method with Future.cancel

david_b do-not-reply at jboss.com
Wed Jan 26 18:12:54 EST 2011

david_b [http://community.jboss.org/people/david_b] created the discussion

"Cancelling an @Asynchronous method with Future.cancel"

To view the discussion, visit: http://community.jboss.org/message/583431#583431

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 at 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110126/e067940d/attachment.html 

More information about the jboss-user mailing list