java @Asynchronous Methods: not running async

后端 未结 2 1826
[愿得一人]
[愿得一人] 2021-01-22 14:58

I try to get an async process running. Based on this example: http://tomee.apache.org/examples-trunk/async-methods/README.html

But the method addWorkflow(Workflow

相关标签:
2条回答
  • 2021-01-22 15:33

    The issue is that Java can't decorate the implicit this pointer.

    In other words, the @Asynchronous annotation won't be processed and you're doing an ordinary method call.

    You can inject your so singleton with a reference to itself (call this e.g. "self"), then call self.addWorkflow.

    You might also want to consider running your async code in a stateless bean. You are using a read lock for addWorkflow, but runWorkflow still has a write lock. I think you have a dead lock now: you're holding the lock until work is done, but no work can be done until the write lock is released.

    0 讨论(0)
  • 2021-01-22 15:40

    So the normal way would be to have the @Asynchronous method in another bean from the caller method.

    @Stateless
    public class ComputationProcessor {
    
     @Asynchronous
     public Future<Data> performComputation {
       return new AsyncResult<Data>(null);
     }
    }
    
    @Stateless
    public class ComputationService {
    
     @Inject
     private ComputationProcessor mProcessor;
    
     public void ...() {
       Future<Data> result = mProcessor.performComputation();
       ...
     }
    }
    

    As you discovered, it won't work if the @Asynchronous method is in the same bean than the caller.

    0 讨论(0)
提交回复
热议问题