简体   繁体   中英

Method with Spring @Async annotation not executed

I use spring @Async annotation to execute certain tasks. At some point (maybe because the pool-size was reached) I see in the debugger that the method with @Async is called but the break point in the method is not reached. I do not know it it will be called when another @Async method is finished or it is skipped forever.

I would like to know if it is possible to trigger an exception when there are not free @Async to execute it.

In addition, how can I know how many @Async are currently used at any given time

Thanks Avi

At some point (maybe because the pool-size was reached) I see in the debugger that the method with @Async is called but the break point in the method is not reached.

Are you sure that the code fragment if not blocked by some conditional? Without a valid code sample, one can only speculate at this point.

I would like to know if it is possible to trigger an exception when there are not free @Async to execute it.

A TimeoutException is usually thrown in this case. You can modify the time out value by adding the following to your Spring config XML.

<mvc:annotation-driven>
  <mvc:async-support default-timeout="180"/>
</mvc:annotation-driven>

Alternatively have a look at this answer: Providing a timeout value when using @Async for a method using Spring 3.0

In addition, how can I know how many @Async are currently used at any given time

Usually when you activate logging using eg sl4j inside the code executed inside an @Async method, you'll see the following output:

2017-03-10 10:27:41,910 [SimpleAsyncTaskExecutor-1] DEBUG
2017-03-10 10:27:43,282 [SimpleAsyncTaskExecutor-2] DEBUG

Hope that this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM