2 Replies Latest reply on Aug 30, 2012 1:00 PM by Kent Watsen

    Common id between @Asynchronous method and a caller's Future<V>?

    Kent Watsen Newbie

      Is there any way to correlate the invocation of an @Asynchronous method and the Future<V> returned to the caller?

       

      I was thinking to call Thread.currentThread() in the @Asynchronous method and somehow get from the Future<V> what thread was running it, but my attempt to cast the Future<V> to a FutureTask<V> failed with the ClassCastException: "org.jboss.as.ejb3.component.interceptors.AsyncFutureInterceptorFactory$1$1 cannot be cast to java.util.concurrent.FutureTask".    Note: I don't know where the AsyncFutureInterceptorFactory came from, since the object I was casting was the Future<V> object (i.e. java.util.concurrent.Future) returned from the async call...

       

      As an alternative, I could have the synchronous EJB call construct and pass an identifier into the Asynchronous call (i.e. as a parameter), so they both then would it, but I was hoping to derive a value from the environment

       

      Any ideas?

        • 1. Re: Common id between @Asynchronous method and a caller's Future<V>?
          Wolf-Dieter Fink Master

          No, it will be against the EJB specification.

          What is the usecase behind?

          • 2. Re: Common id between @Asynchronous method and a caller's Future<V>?
            Kent Watsen Newbie

            The use-case behind is to support progress monitoring during the execution of a long running request (LRR).

             

            Specifically, I have a stateless session bean that provides an EJB/REST API to start a LRR:

             

                @POST

                @Path("/some-long-running-request")

                @Consumes({ "application/xml" })

                @Produces({ "application/xml" })

                public AsyncMethodOutput some_long_running_request(@Context SecurityContext context, @QueryParam("queue") String queueAddress, AsyncMethodInput asyncMethodInput) throws Exception {

             

                    // lookup JMS queue from queue-name

                    Queue queue = (Queue)HornetQDestination.fromAddress(queueAddress);

             

                    // extract list of targets from asyncMethodInput

                    List<Integer> targetList = asyncMethodInput.getTargets();

             

                    // execute the async version of self

                    Future<Void> future = ctx.getBusinessObject(MyTestService.class).some_long_running_request(queue, targetList);

             

                    // register Future with systemManager  [Note: systemManager is @Singleton]

                     // warning: systemManager needs to calculate a "taskId" that is same as what the async method calculates (see below)

                     taskId = systemManager.registerFutureObject(future);  // this is what I'm trying to figure out how to support!

             

                    // return associated taskId

                    return new AsyncMethodOutput(taskId);

                }

             

             

            Then, in the async version of the same method:

             

                @Asynchronous

                public Future<Void> some_long_running_request(Queue queue, List<Integer> targetList) throws Exception {

             

                    // determine the "taskId" progress-updates should use when posting to queue

                     taskId = ???

             

                    // alert system manager that this taskId is running

                    systemManager.registerActiveTask(taskId);

                   

                    // fall into work loop

                    while(myContext.wasCancelCalled() == false) {

                         // do some work

                         // post progress-update to queue

                    }

             

                    // alert system manager that this taskId is completed

                    systemManager.deregisterActiveTask(taskId);

             

                    // we only return a Future for client cancellation

                    return new AsyncResult<Void>(null);

                }

             

             

            Lastly, SystemManager would have API enabling clients to list actively running tasks and cancel anyone by its "taskId"

             

            Thanks