• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

What is wrong with @Async

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to call a method asynchronously. This is what I did, by studying from internet.
Similar code is working in our project, with the same annotations and all, but due to some reason, I am not able to make it work in standalone.

Can anybody help me on this, as to what I have done wrong.

 
Yogesh Gandhi
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THe output is

"Entered test"
Then waits for 10 seconds
"HELLO"
and then
I am done.
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See this tutorial, you have to run your application using Spring not directly by instantiating the class
 
Yogesh Gandhi
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:See this tutorial, you have to run your application using Spring not directly by instantiating the class



I wonder, what is it that it is only required to run my application using spring. Why I can't use it on a method in plain java?
 
Marshal
Posts: 27531
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to use an annotation on a class like that, you have to use the class in a context (or framework) which is programmed to use the annotation. It isn't the JVM code which is going to use the annotation, it's some application code somewhere.

You know already that Spring is an example of such an application, it uses that @Async annotation in the way you expect. It would be possible for you to write an application which used that annotation in the same way, but you would actually have to write the code for that usage to happen.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a very common mistake that people make when they use Spring: if you create an instance of a Spring bean with new, then it is not a Spring-managed bean, and things like dependency injection (@Autowired) and other Spring features (such as @Async) will not work.

You need to let Spring manage the instance of the Util class, instead of creating it yourself using new.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic