Spring @Async ignored

后端 未结 1 1850
北海茫月
北海茫月 2020-12-15 07:05

I am having troubles invoking a method asynchronously in Spring, when the invoker is an embedded library receiving notifications from an external system. The code looks as b

相关标签:
1条回答
  • 2020-12-15 07:33

    @Async (as well as @Transactional and other similar annotations) will not work when the method is invoked via this (on when @Async is used for private methods*), as long as you do not use real AspectJ compiletime or runtime weaving.

    *the private method thing is: when the method is private, then it must been invoked via this - so this is more the consequence then the cause

    So change your code:

    @Service
    public class DefaultNotificationProcessor implements NotificationProcessor {
    
    
        @Resource
        private DefaultNotificationProcessor selfReference;
    
    
        @Override
        public void process(Notification notification) {
            selfReference.processAsync(notification);
        }
    
    
        //the method must not been private
        //the method must been invoked via a bean reference
        @Async
        void processAsync(Notification notification) {
            // Heavy processing
        }
    }
    

    See also the answers for: Does Spring @Transactional attribute work on a private method? -- this is the same problem

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