I have a spring boot application and want to run a simple Async background task by using @async annotation , however , this does not seem to be working (no new threads are created and I wait until the sleep fininshes after each call to the function .. *****my config class:**
@Configuration
@EnableAsync
public class AsyncConfig {
}
****My controller :**
@GetMapping(value = "/testAsync")
@ResponseBody
public String testAsync() {
TestClass testClass = new TestClass();
System.out.println("begin of test async dates is : " + new Date());
for (int i = 0; i < 5; i++) {
testClass.asyncMethod();
}
System.out.println("end of test async dates is : " + new Date());
return "end!";
}
*****My TestClass where the Async method :**
@Service
public class TestClass {
@Async
public void asyncMethod() {
System.out.println("Execute method asynchronously. in thread : "
+ Thread.currentThread().getName());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("after sleep : " + Thread.currentThread().getName());
}
}
****output after running the application :**
begin of test async dates is : Sat Jul 14 19:35:43 EET 2018
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
2018-07-14 19:36:22.299 INFO 3120 --- [MessageBroker-1] o.s.w.s.c.WebSocketMessageBrokerStats : WebSocketSession[0 current WS(0)-HttpStream(0)-HttpPoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompSubProtocol[processed CONNECT(0)-CONNECTED(0)-DISCONNECT(0)], stompBrokerRelay[null], inboundChannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundChannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockJsScheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
after sleep : http-nio-8080-exec-1
Execute method asynchronously. in thread : http-nio-8080-exec-1
after sleep : http-nio-8080-exec-1
end of test async dates is : Sat Jul 14 19:36:33 EET 2018
****note:** -I have tried annotating the TestClass with @Component it did not work -I have tried adding an executor on both the application level and the method level but I am getting the same results.
please help me , I have spent 5 hours on this with no luck