I'm working on a Spring Boot project. I'm trying to delay a sendSMS() function for 10 seconds. So I'have found ThreadPoolTaskScheduler which looks really helpful because it can handle capacity of API request by adjusting poolsize.
Here is my configuration,
@Configuration
@EnableScheduling
public class ThreadPoolTaskSchedulerConfig{
@Bean(name = "smsTaskScheduler")
public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(5);
threadPoolTaskScheduler.setThreadNamePrefix("ThreadPoolTaskScheduler::");
return threadPoolTaskScheduler;
}
}
And here is my code that implements it,
@Service
public class SmsSubscriptionService {
@Autowired
@Qualifier("smsTaskScheduler")
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
public void excuteReq(){
log.info("---- HELLO FROM excuteReq " + new Date() );
this.threadPoolTaskScheduler.scheduleWithFixedDelay(
(new Runnable() {
public void run(){
sendSMS();
}
}),
10*1000
);
}
public void sendSMS(){
log.info("---- HELLO FROM sendSMS() " + new Date() );
}
}
When I call the function excuteReq(), the sendSMS() will keep looping. Did I config something wrong ?
Thanks a lot.