1

I've annotated a method with @Async in the hope that it will be run asynchronously. However, when I print out the thread name from the thread, it is still 'main' just as it is when I run it without the @Async annotation. It also returns about as slowly (700ms) which makes me think that it's not getting run asynchronously at all.

The bean is annotated with @Component, is getting its dependencies successfully wired in with @Autowired. I'm creating the bean with

ApplicationContext context = new AnnotationConfigApplicationContext(MyClassConfig.class);
MyClass myClass = context.getBean(MyClass.class);

Is there something I'm doing wrong?

Many thanks.

EDIT: added more code

The config class looks like this:

@Configuration
@ComponentScan
@EnableAsync
public class MyClassConfig {

    @Bean
    public MyClass myClass(){
        return new MyClass();
    }
}

The async method is:

@Async
public void doSth(){
...
}

Another thought is, does it have to return a Future? I don't care about the result - it's being evaluated for its side effects - but I also read that it should return a Future?

The code that calls doSth() is just

doSth()

called from another method in the class.

user384842
  • 1,946
  • 3
  • 17
  • 24
  • What does `MyClassConfig` look like in this example? Do you have `@EnableAsync` on the config class as well? – mkobit Mar 05 '15 at 19:14
  • I am calling it from within the same class. I read on one SO answer that that could be a factor, but I'm not sure? – user384842 Mar 05 '15 at 19:14
  • Provide your entire code please, atleast how the `@Async` is configured. – Aninda Bhattacharyya Mar 05 '15 at 19:16
  • @Mike - thanks. I've added EnableAsync but it doesn't seem to have made any difference to the behaviour. – user384842 Mar 05 '15 at 19:17
  • 3
    If you're calling the @Async method from within the same class it won't work. – John R Mar 05 '15 at 19:17
  • @John - ah, thanks. Do you know why that restriction is there? Does that mean there's also a restriction on calling back into this class? (Cos I was thinking I could work around it by calling an external method that calls back in). – user384842 Mar 05 '15 at 19:20
  • Can you post the code that calls `doSth()` – John R Mar 05 '15 at 19:20
  • Take a look at [my answer here](http://stackoverflow.com/questions/28863927/cache-is-empty-after-setting-up-and-using-ehcache-in-spring/28864629#28864629). You have the same problem, just with a different annotation. – John R Mar 05 '15 at 19:22
  • @John - thanks - added it, but it's simply a call to doSth() from another (non-async) method in the class – user384842 Mar 05 '15 at 19:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72363/discussion-between-john-r-and-user384842). – John R Mar 05 '15 at 19:22
  • `public void doSth` I hope this is on MyClass? – Aninda Bhattacharyya Mar 05 '15 at 19:24
  • Many thanks to John R who resolved it for me in chat. It's because I was calling it from the same class, thereby bypassing the spring proxy. I took a simple solution and called out from the class into another spring class that was wired in (I called it 'AsyncHelper'), and then that class called back in to the async method so that Spring would see it. Worked perfectly - 2msec instead of 2000! – user384842 Mar 05 '15 at 19:37

0 Answers0